/** * Adds a value update to the list of value updates. * * @param valueUpdate the ValueUpdate to add * @return a reference to itself * @throws IllegalArgumentException if the data type of the value update is not equal to the data type of this field */ public FieldUpdate addValueUpdate(ValueUpdate valueUpdate) { valueUpdate.checkCompatibility(field.getDataType()); //will throw exception valueUpdates.add(valueUpdate); return this; }
/** * Creates a new value update * specifying an addition of all values in a given list to an array. If this method is used on a weighted set data * type, the default weights will be 1. Note that this method is just a convenience method, it simply iterates * through the list and creates value updates by calling createAdd() for each element. * * @param values a List containing the values to add * @return a List of ValueUpdates 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 array or weighted set * @see ValueUpdate#createAdd(FieldValue) */ public static List<ValueUpdate> createAddAll(List<? extends FieldValue> values) { List<ValueUpdate> vupds = new ArrayList<>(); for (FieldValue value : values) { vupds.add(ValueUpdate.createAdd(value)); } return vupds; }
/** * Creates a new field update, with encapsulated value updates, * specifying an addition of all values in a given list to an array. If this method is used on a weighted set data * type, the default weights will be 1. * * @param field the Field to add an array of values to * @param values a List containing the values to add * @return a FieldUpdate specifying the addition * @throws IllegalArgumentException if the runtime type of values does not match the type required by field * @throws UnsupportedOperationException if the field type is not array or weighted set * @see com.yahoo.document.update.ValueUpdate#createAddAll(java.util.List) * @see ValueUpdate#createAdd(FieldValue) */ public static FieldUpdate createAddAll(Field field, List<? extends FieldValue> values) { return new FieldUpdate(field, ValueUpdate.createAddAll(values)); }
/** * Creates a new value update that divides a weight in a weighted set. Note that this method is just a convenience * method, it simply creates a divide value update by calling createDivide() and then creates a map value * update by calling createMap() with the key and the divide value update as parameters. * * @param key the key whose weight in the weighted set to divide * @param divisor the number to divide by * @return a ValueUpdate specifying the division * @see ValueUpdate#createDivide(Number) * @see ValueUpdate#createMap(FieldValue, ValueUpdate) */ public static ValueUpdate createDivide(FieldValue key, Number divisor) { return createMap(key, createDivide(divisor)); }
/** * Creates a new value update that increments a weight in a weighted set. Note that this method is just a convenience * method, it simply creates an increment value update by calling createIncrement() and then creates a map value * update by calling createMap() with the key and the increment value update as parameters. * * @param key the key whose weight in the weighted set to increment * @param increment the number to increment by * @return a ValueUpdate specifying the increment * @see ValueUpdate#createIncrement(Number) * @see ValueUpdate#createMap(FieldValue, ValueUpdate) */ public static ValueUpdate createIncrement(FieldValue key, Number increment) { return createMap(key, createIncrement(increment)); }
/** * Creates a new value update that decrements a weight in a weighted set. Note that this method is just a convenience * method, it simply creates a decrement value update by calling createDecrement() and then creates a map value * update by calling createMap() with the key and the decrement value update as parameters. * * @param key the key whose weight in the weighted set to decrement * @param decrement the number to decrement by * @return a ValueUpdate specifying the decrement * @see ValueUpdate#createDecrement(Number) * @see ValueUpdate#createMap(FieldValue, ValueUpdate) */ public static ValueUpdate createDecrement(FieldValue key, Number decrement) { return createMap(key, createDecrement(decrement)); }
@SuppressWarnings("rawtypes") public static ValueUpdate readSingleUpdate(TokenBuffer buffer, DataType expectedType, String action) { ValueUpdate update; switch (action) { case UPDATE_ASSIGN: update = (buffer.currentToken() == JsonToken.VALUE_NULL) ? ValueUpdate.createClear() : ValueUpdate.createAssign(readSingleValue(buffer, expectedType)); break; // double is silly, but it's what is used internally anyway case UPDATE_INCREMENT: update = ValueUpdate.createIncrement(Double.valueOf(buffer.currentText())); break; case UPDATE_DECREMENT: update = ValueUpdate.createDecrement(Double.valueOf(buffer.currentText())); break; case UPDATE_MULTIPLY: update = ValueUpdate.createMultiply(Double.valueOf(buffer.currentText())); break; case UPDATE_DIVIDE: update = ValueUpdate.createDivide(Double.valueOf(buffer.currentText())); break; default: throw new IllegalArgumentException("Operation \"" + buffer.currentName() + "\" not implemented."); } return update; }
if ("increment".equals(type)) { fu.addValueUpdate(ValueUpdate.createIncrement(key, by)); } if ("decrement".equals(type)) { fu.addValueUpdate(ValueUpdate.createDecrement(key, by)); } if ("multiply".equals(type)) { fu.addValueUpdate(ValueUpdate.createMultiply(key, by)); } if ("divide".equals(type)) { fu.addValueUpdate(ValueUpdate.createDivide(key, by)); } } else { if ("increment".equals(type)) { fu.addValueUpdate(ValueUpdate.createIncrement(by)); } if ("decrement".equals(type)) { fu.addValueUpdate(ValueUpdate.createDecrement(by)); } if ("multiply".equals(type)) { fu.addValueUpdate(ValueUpdate.createMultiply(by)); } if ("divide".equals(type)) { fu.addValueUpdate(ValueUpdate.createDivide(by)); }
public static boolean isComplete(Field field, ValueUpdate update) { if (update instanceof AssignValueUpdate) { return true; } if (!(update instanceof MapValueUpdate)) { return false; } DataType fieldType = field.getDataType(); if (!(fieldType instanceof StructuredDataType)) { return false; } field = ((StructuredDataType)fieldType).getField(String.valueOf(update.getValue())); if (field == null) { return false; } return isComplete(field, ((MapValueUpdate)update).getUpdate()); }
/** * Creates a new field update that clears the field. This operation removes the value/field completely. * * @param field the Field to clear * @return a FieldUpdate specifying the clearing * @see com.yahoo.document.update.FieldUpdate#createClear(Field) */ public static FieldUpdate createClearField(Field field) { return new FieldUpdate(field, ValueUpdate.createClear()); }
/** * Creates a new field update, with one encapsulated value update that increments a value. * Note that the data type must be a numeric type. * * @param field the field to increment the value of * @param increment the number to increment by * @return a FieldUpdate specifying the increment * @throws UnsupportedOperationException if the data type is non-numeric * @see ValueUpdate#createIncrement(Number) */ public static FieldUpdate createIncrement(Field field, Number increment) { return new FieldUpdate(field, ValueUpdate.createIncrement(increment)); }
/** * Creates a new field update, with one encapsulated value update that decrements a value. * Note that the data type must be a numeric type. * * @param field the field to decrement the value of * @param decrement the number to decrement by * @return a FieldUpdate specifying the decrement * @throws UnsupportedOperationException if the data type is non-numeric * @see ValueUpdate#createDecrement(Number) */ public static FieldUpdate createDecrement(Field field, Number decrement) { return new FieldUpdate(field, ValueUpdate.createDecrement(decrement)); }
/** * Creates a new field update, with one encapsulated value update that divides a value. * Note that the data type must be a numeric type. * * @param field the field to divide the value of * @param divisor the number to divide by * @return a FieldUpdate specifying the division * @throws UnsupportedOperationException if the data type is non-numeric * @see ValueUpdate#createDivide(Number) */ public static FieldUpdate createDivide(Field field, Number divisor) { return new FieldUpdate(field, ValueUpdate.createDivide(divisor)); }
/** * Applies this field update. * * @param doc the document to apply the update to * @return a reference to itself */ public FieldUpdate applyTo(Document doc) { for (ValueUpdate vupd : valueUpdates) { DataType dataType = field.getDataType(); FieldValue oldValue = doc.getFieldValue(field); boolean existed = (oldValue != null); if (!existed) { oldValue = dataType.createFieldValue(); } FieldValue newValue = vupd.applyTo(oldValue); if (newValue == null) { if (existed) { doc.removeFieldValue(field); } } else { doc.setFieldValue(field, newValue); } } return this; }
/** * Creates a new field update, with one encapsulated value update, * that assigns a new value, completely overwriting the previous value. Note that it is possible to pass * newValue=null to this method to remove the value completely. * * @param field the Field to assign a new value to * @param newValue the value to assign * @return a FieldUpdate specifying the assignment * @throws IllegalArgumentException if the runtime type of newValue does not match the type required by field * @see com.yahoo.document.update.ValueUpdate#createAssign(FieldValue) */ public static FieldUpdate createAssign(Field field, FieldValue newValue) { return new FieldUpdate(field, ValueUpdate.createAssign(newValue)); }
return val; } else if (upd instanceof AssignValueUpdate) { val.assign(upd.getValue()); return val; } else if (upd instanceof AddValueUpdate) { if (val instanceof Array) { ((Array)val).add(upd.getValue()); } else if (val instanceof WeightedSet) { ((WeightedSet)val).put(upd.getValue(), ((AddValueUpdate)upd).getWeight()); val.assign(upd.getValue()); return val; } else if (upd instanceof RemoveValueUpdate) { if (val instanceof Array) { ((Array)val).add(upd.getValue()); } else if (val instanceof WeightedSet) { ((WeightedSet)val).put(upd.getValue(), 1); throw new UnsupportedOperationException("Can not map into a " + val.getClass().getName() + "."); } else if (val instanceof StructuredFieldValue) { Field field = ((StructuredFieldValue)val).getField(String.valueOf(upd.getValue())); if (field == null) { throw new IllegalArgumentException("Field '" + upd.getValue() + "' not found."); ((WeightedSet)val).put(upd.getValue(), ((IntegerFieldValue)weight).getInteger()); return val; } else {
/** * Creates a new field update, with one encapsulated value update, * that clears the value; see documentation for ClearValueUpdate to see behavior * for the individual data types. Note that clearing the value is not the same * clearing the field; this method leaves an empty value, whereas clearing the * field will completely remove the value. * * @param field the field to clear the value of * @return a FieldUpdate specifying the clearing * @see com.yahoo.document.update.ClearValueUpdate * @see ValueUpdate#createClear() * @see FieldUpdate#createClearField(com.yahoo.document.Field) */ public static FieldUpdate createClear(Field field) { return new FieldUpdate(field, ValueUpdate.createClear()); }
/** * Creates a new field update, with one encapsulated value update that increments a weight in a weighted set. * * @param field the field to increment one of the weights of * @param key the key whose weight in the weighted set to increment * @param increment the number to increment by * @return a FieldUpdate specifying the increment * @throws IllegalArgumentException if key is not equal to the nested type of the weighted set * @see ValueUpdate#createIncrement(Number) * @see ValueUpdate#createMap(FieldValue, ValueUpdate) */ public static FieldUpdate createIncrement(Field field, FieldValue key, Number increment) { return new FieldUpdate(field, ValueUpdate.createIncrement(key, increment)); }
/** * Creates a new field update, with one encapsulated value update that decrements a weight in a weighted set. * * @param field the field to decrement one of the weights of * @param key the key whose weight in the weighted set to decrement * @param decrement the number to decrement by * @return a FieldUpdate specifying the decrement * @throws IllegalArgumentException if key is not equal to the nested type of the weighted set * @see ValueUpdate#createDecrement(Number) * @see ValueUpdate#createMap(FieldValue, ValueUpdate) */ public static FieldUpdate createDecrement(Field field, FieldValue key, Number decrement) { return new FieldUpdate(field, ValueUpdate.createDecrement(key, decrement)); }
/** * Creates a new field update, with one encapsulated value update that divides a weight in a weighted set. * * @param field the field to divide one of the weights of * @param key the key whose weight in the weighted set to divide * @param divisor the number to divide by * @return a FieldUpdate specifying the division * @throws IllegalArgumentException if key is not equal to the nested type of the weighted set * @see ValueUpdate#createDivide(Number) * @see ValueUpdate#createMap(FieldValue, ValueUpdate) */ public static FieldUpdate createDivide(Field field, FieldValue key, Number divisor) { return new FieldUpdate(field, ValueUpdate.createDivide(key, divisor)); }