/** * 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)); }
/** * Creates a new value update that multiplies a weight in a weighted set. Note that this method is just a convenience * method, it simply creates a multiply value update by calling createMultiply() and then creates a map value * update by calling createMap() with the key and the multiply value update as parameters. * * @param key the key whose weight in the weighted set to multiply * @param factor the number to multiply by * @return a ValueUpdate specifying the multiplication * @see ValueUpdate#createMultiply(Number) * @see ValueUpdate#createMap(FieldValue, ValueUpdate) */ public static ValueUpdate createMultiply(FieldValue key, Number factor) { return createMap(key, createMultiply(factor)); }
/** * Creates a new field update, with one encapsulated value update, which * is able to map an update to a value to a subvalue in an array or a * weighted set. If this update is to be applied to an array, the value parameter must be an integer specifying * the index in the array that the update parameter is to be applied to, and the update parameter must be * compatible with the sub-datatype of the array. If this update is to be applied on a weighted set, the value * parameter must be the key in the set that the update parameter is to be applied to, and the update parameter * must be compatible with the INT data type. * * @param field the field to modify the subvalue of * @param value the index in case of array, or key in case of weighted set * @param update the update to apply to the target sub-value * @throws IllegalArgumentException in case data type is an array type and value is not an Integer; in case data type is a weighted set type and value is not equal to the nested type of the weighted set; or the encapsulated update throws such an exception * @throws UnsupportedOperationException if superType is a single-value type, or anything else than array or weighted set; or the encapsulated update throws such an exception * @return a FieldUpdate specifying the sub-update * @see ValueUpdate#createMap(FieldValue, ValueUpdate) */ public static FieldUpdate createMap(Field field, FieldValue value, ValueUpdate update) { return new FieldUpdate(field, ValueUpdate.createMap(value, update)); }
@SuppressWarnings({ "rawtypes", "unchecked" }) public static ValueUpdate createMapUpdate(TokenBuffer buffer, DataType currentLevel, FieldValue keyParent, FieldValue topLevelKey) { TokenBuffer.Token element = buffer.prefetchScalar(UPDATE_ELEMENT); if (UPDATE_ELEMENT.equals(buffer.currentName())) { buffer.next(); } FieldValue key = keyTypeForMapUpdate(element, currentLevel); if (keyParent != null) { ((CollectionFieldValue) keyParent).add(key); } // structure is: [(match + element)*, (element + action)] // match will always have element, and either match or action if (!UPDATE_MATCH.equals(buffer.currentName())) { // we have reached an action... if (topLevelKey == null) { return ValueUpdate.createMap(key, readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName())); } else { return ValueUpdate.createMap(topLevelKey, readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName())); } } else { // next level of matching if (topLevelKey == null) { return createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), key, key); } else { return createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), key, topLevelKey); } } }