/** * Creates a new value update * specifying an addition of all key/weight pairs in a weighted set to a weighted set. If this method * is used on an array data type, the weights will be omitted. Note that this method is just a convenience method, * it simply iterates through the set and creates value updates by calling createAdd() for each element. * * @param set a WeightedSet containing the key/weight pairs to add * @return a ValueUpdate specifying the addition * @throws IllegalArgumentException if the runtime type of values does not match the type required * @throws UnsupportedOperationException if the field type is not weighted set or array * @see ValueUpdate#createAdd(FieldValue, Integer) */ public static List<ValueUpdate> createAddAll(WeightedSet<? extends FieldValue> set) { List<ValueUpdate> vupds = new ArrayList<>(); Iterator<? extends FieldValue> it = set.fieldValueIterator(); while (it.hasNext()) { FieldValue key = it.next(); vupds.add(ValueUpdate.createAdd(key, set.get(key))); } return vupds; }
@Override public void assign(Object o) { if (!checkAssign(o)) { return; } if (o instanceof WeightedSet) { WeightedSet wset = (WeightedSet) o; if (getDataType().equals(wset.getDataType())) { map.assign(wset.map); } else { throw new IllegalArgumentException("Cannot assign a weighted set of type " + wset.getDataType() + " to a weighted set of type " + getDataType()); } } else if (o instanceof Map) { map = new WeightedSetWrapper((Map)o, map.getDataType()); } else { throw new IllegalArgumentException("Class " + o.getClass() + " not applicable to an " + this.getClass() + " instance."); } }
public boolean add(K value) { put(value, 1); return true; }
@SuppressWarnings({ "unchecked" }) private List<ValueUpdate> createAddValueUpdateForWset(WeightedSet wset) { List<ValueUpdate> ret = new ArrayList<>(wset.size()); for (Iterator<FieldValue> it = wset.fieldValueIterator(); it.hasNext(); ) { FieldValue key = it.next(); ret.add(new AddValueUpdate(key, wset.get(key))); } return ret; }
/** * Write out the value of weighted set field * * @param field - field description (name and data type) * @param ws - field value */ public <T extends FieldValue> void write(FieldBase field, WeightedSet<T> ws) { WeightedSetDataType type = ws.getDataType(); putInt(null, type.getNestedType().getId()); putInt(null, ws.size()); Iterator<T> it = ws.fieldValueIterator(); while (it.hasNext()) { FieldValue key = it.next(); java.lang.Integer value = ws.get(key); int sizePos = buf.position(); putInt(null, 0); int startPos = buf.position(); key.serialize(this); putInt(null, value); int finalPos = buf.position(); int size = finalPos - startPos; buf.position(sizePos); putInt(null, size); buf.position(finalPos); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private FieldValue convertWset(WeightedSet val) { Map<FieldValue, Integer> next = new LinkedHashMap<FieldValue, Integer>(); DataType nextType = null; for (Iterator<FieldValue> it = val.fieldValueIterator(); it.hasNext();) { FieldValue prevKey = it.next(); Integer prevVal = val.get(prevKey); FieldValue nextKey = convert(prevKey); if (nextKey == null) { continue; } if (nextType == null) { nextType = nextKey.getDataType(); } else if (!nextType.isValueCompatible(nextKey)) { throw new IllegalArgumentException("Expected " + nextType.getName() + ", got " + nextKey.getDataType().getName() + "."); } next.put(nextKey, prevVal); } if (nextType == null) { return null; } WeightedSet ret = DataType.getWeightedSet(nextType, val.getDataType().createIfNonExistent(), val.getDataType().removeIfZero()).createFieldValue(); for (Map.Entry<FieldValue, Integer> entry : next.entrySet()) { ret.put(entry.getKey(), entry.getValue()); } return ret; }
@Override public FieldValue applyTo(FieldValue fval) { if (fval instanceof Array) { Array array = (Array) fval; FieldValue element = array.getFieldValue(((IntegerFieldValue) value).getInteger()); element = update.applyTo(element); array.set(((IntegerFieldValue) value).getInteger(), element); } else if (fval instanceof WeightedSet) { WeightedSet wset = (WeightedSet) fval; WeightedSetDataType wtype = wset.getDataType(); Integer weight = wset.get(value); if (weight == null) { if (wtype.createIfNonExistent() && update instanceof ArithmeticValueUpdate) { weight = 0; } else { return fval; } } weight = (Integer) update.applyTo(new IntegerFieldValue(weight)).getWrappedValue(); wset.put(value, weight); if (wtype.removeIfZero() && update instanceof ArithmeticValueUpdate && weight == 0) { wset.remove(value); } } return fval; }
public <T extends FieldValue> void read(FieldBase field, WeightedSet<T> ws) { WeightedSetDataType type = ws.getDataType(); getInt(null); // Have no need for type int numElements = getInt(null); if (numElements < 0) { throw new DeserializationException("Bad number of weighted set elements, " + numElements); } ws.clearAndReserve(numElements * 2); // Avoid resizing for (int i = 0; i < numElements; i++) { int size = getInt(null); FieldValue value = type.getNestedType().createFieldValue(); value.deserialize(null, this); IntegerFieldValue weight = new IntegerFieldValue(getInt(null)); ws.putUnChecked((T) value, weight); } }
public <T extends FieldValue> void read(FieldBase field, WeightedSet<T> value) { try { while (reader.hasNext()) { int type = reader.next(); if (type == XMLStreamReader.START_ELEMENT) { if ("item".equals(reader.getName().toString())) { FieldValue fv = value.getDataType().getNestedType().createFieldValue(); int weight = 1; for (int i = 0; i < reader.getAttributeCount(); i++) { if ("weight".equals(reader.getAttributeName(i).toString())) { weight = Integer.parseInt(reader.getAttributeValue(i)); } } deserializeFieldValue(field, fv); // noinspection unchecked value.put((T)fv, weight); skipToEnd("item"); } else { throw newDeserializeException(field, "Illegal tag " + reader.getName() + " expected 'item'"); } } else if (type == XMLStreamReader.END_ELEMENT) { return; } } } catch (XMLStreamException e) { throw newException(field, e); } }
public static <T extends FieldValue> void serializeWeightedSet(JsonGenerator generator, FieldBase field, WeightedSet<T> value) { fieldNameIfNotNull(generator, field); wrapIOException(() -> { generator.writeStartObject(); for (T key : value.keySet()) { Integer weight = value.get(key); // key.toString() is according to spec generator.writeNumberField(key.toString(), weight); } generator.writeEndObject(); }); }
/** * Uses toString() from the encapsulated Map. * * @return the toString() of this set */ public String toString() { StringBuilder sb = new StringBuilder(); sb.append("WeightedSet(").append(getDataType()); for (Map.Entry entry : map.entrySet()) { sb.append("\n key: ").append(entry.getKey().getClass()).append(": ").append(entry.getKey()); sb.append("\n value: ").append(entry.getValue().getClass()).append(": ").append(entry.getValue()); } return sb.append("\n)").toString(); }
lst.addAll(createRemoveValueUpdateForEachElement(((Array)val).fieldValueIterator())); } else if (val instanceof WeightedSet) { lst.addAll(createRemoveValueUpdateForEachElement(((WeightedSet)val).fieldValueIterator())); } else {
public Collection<Integer> values() { return getPrimitiveMap().values(); }
/** * Creates a new weighted set with a given initial capacity. * * @param initialCapacity the initial capacity to use for the encapsulated Map */ public WeightedSet(DataType type, int initialCapacity) { super((WeightedSetDataType) type); clearAndReserve(initialCapacity); }
@Override public WeightedSet createFieldValue() { return new WeightedSet(this); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected void doExecute(ExecutionContext ctx) { FieldValue input = ctx.getValue(); DataType inputType = input.getDataType(); WeightedSetDataType outputType = DataType.getWeightedSet(inputType, createIfNonExistent, removeIfZero); WeightedSet output = outputType.createFieldValue(); output.add(input); ctx.setValue(output); }
/** * Write out the value of weighted set field * * @param field - field description (name and data type) * @param ws - field value */ public <T extends FieldValue> void write(FieldBase field, WeightedSet<T> ws) { WeightedSetDataType type = ws.getDataType(); putInt(null, type.getNestedType().getId()); putInt(null, ws.size()); Iterator<T> it = ws.fieldValueIterator(); while (it.hasNext()) { FieldValue key = it.next(); java.lang.Integer value = ws.get(key); int sizePos = buf.position(); putInt(null, 0); int startPos = buf.position(); key.serialize(this); putInt(null, value); int finalPos = buf.position(); int size = finalPos - startPos; buf.position(sizePos); putInt(null, size); buf.position(finalPos); } }
public <T extends FieldValue> void read(FieldBase field, WeightedSet<T> ws) { WeightedSetDataType type = ws.getDataType(); getInt(null); // Have no need for type int numElements = getInt(null); if (numElements < 0) { throw new DeserializationException("Bad number of weighted set elements, " + numElements); } ws.clearAndReserve(numElements * 2); // Avoid resizing for (int i = 0; i < numElements; i++) { int size = getInt(null); FieldValue value = type.getNestedType().createFieldValue(); value.deserialize(null, this); IntegerFieldValue weight = new IntegerFieldValue(getInt(null)); ws.putUnChecked((T) value, weight); } }
/** * Reserve space for this amount of keys in order to avoid resizing */ public void clearAndReserve(int count) { map = new MapFieldValue<>(new MapDataType(getDataType().getNestedType(), DataType.INT), count); }
public Set<Entry<K, Integer>> entrySet() { return getPrimitiveMap().entrySet(); }