@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; }
@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; }
/** * 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); } }
/** * 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; }
/** * 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 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(); }); }
@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; }
@SuppressWarnings({ "unchecked" }) private List<ValueUpdate> createMapValueUpdatesForWset(WeightedSet wset, MapValueUpdate upd) { List<ValueUpdate> ret = new ArrayList<>(); for (Iterator<FieldValue> it = wset.fieldValueIterator(); it.hasNext();) { FieldValue childVal = it.next(); for (ValueUpdate childUpd : createValueUpdates(new IntegerFieldValue(wset.get(childVal)), upd.getUpdate())) { ret.add(new MapValueUpdate(childVal, childUpd)); } } return ret; } }
public static void printWeightedSetXml(WeightedSet ws, XmlStream xml) { Iterator<FieldValue> it = ws.fieldValueIterator(); while (it.hasNext()) { FieldValue val = it.next(); xml.beginTag("item"); xml.addAttribute("weight", ws.get(val)); val.printXml(xml); xml.endTag(); } }
List<FieldPathEntry> nextPath = new ArrayList<>(path); nextPath.add(FieldPathEntry.newMapLookupEntry(wsetEntry, DataType.INT)); createUpdatesAt(nextPath, new IntegerFieldValue(wset.get(wsetEntry)), idx + 1, out);