@Override public DataType createdOutputType() { return value.getDataType(); }
@Override public int compareTo(FieldValue fieldValue) { return getDataType().compareTo(fieldValue.getDataType()); }
@Override public ModificationStatus doModify(FieldValue fv) { if (!fv.getDataType().equals(newValue.getDataType())) { throw new IllegalArgumentException("Trying to assign " + newValue + " of type " + newValue.getDataType() + " to an instance of " + fv.getDataType()); } else { if (removeIfZero && (newValue instanceof NumericFieldValue) && ((NumericFieldValue)newValue).getNumber().longValue() == 0) { return ModificationStatus.REMOVED; } fv.assign(newValue); } return ModificationStatus.MODIFIED; }
private void validateCompatibleTypes(DataType d, FieldValue v) { if (!d.isValueCompatible(v)) { throw new IllegalArgumentException( "Incompatible data types. Got " + v.getDataType() + ", expected " + d); } }
@Override protected final boolean shouldConvert(FieldValue value) { return value.getDataType().equals(DataType.STRING); }
@Override public boolean isValueCompatible(FieldValue value) { return value.getDataType().equals(this); }
@Override protected void doVerify(VerificationContext context) { context.setValue(value.getDataType()); }
@Override protected void checkCompatibility(DataType fieldType) { if (value != null && !value.getDataType().equals(fieldType)) { throw new IllegalArgumentException("Expected " + fieldType.getName() + ", got " + value.getDataType().getName()); } }
public static <T> T getFieldValue(FieldValue struct, DataType structType, String fieldName, Class<T> fieldType) { if (!(struct instanceof Struct)) { return null; } if (!struct.getDataType().equals(structType)) { return null; } FieldValue fieldValue = ((Struct)struct).getFieldValue(fieldName); if (!fieldType.isInstance(fieldValue)) { return null; } return fieldType.cast(fieldValue); }
public static <T> T getFieldValue(FieldValue struct, DataType structType, Field field, Class<T> fieldType) { if (!(struct instanceof Struct)) { return null; } if (!struct.getDataType().equals(structType)) { return null; } FieldValue fieldValue = ((Struct)struct).getFieldValue(field); if (!fieldType.isInstance(fieldValue)) { return null; } return fieldType.cast(fieldValue); }
/** * Using the given field path, digs through the document and returns the matching field value. * If the field path resolves to multiple values, returns an ArrayFieldValue containing the * values. */ public FieldValue getRecursiveValue(String path) { return getRecursiveValue(getDataType().buildFieldPath(path)); }
@Override protected void checkCompatibility(DataType fieldType) { if (!(fieldType instanceof CollectionDataType)) { throw new UnsupportedOperationException("Expected collection, got " + fieldType.getName() + "."); } fieldType = ((CollectionDataType)fieldType).getNestedType(); if (value != null && !value.getDataType().equals(fieldType)) { throw new IllegalArgumentException("Expected " + fieldType.getName() + ", got " + value.getDataType().getName()); } }
@Override protected void checkCompatibility(DataType fieldType) { if (!(fieldType instanceof CollectionDataType)) { throw new UnsupportedOperationException("Expected collection, got " + fieldType.getName() + "."); } fieldType = ((CollectionDataType)fieldType).getNestedType(); if (value != null && !value.getDataType().equals(fieldType)) { throw new IllegalArgumentException("Expected " + fieldType.getName() + ", got " + value.getDataType().getName()); } }
@SuppressWarnings({ "unchecked" }) @Override protected void doExecute(ExecutionContext ctx) { FieldValue input = ctx.getValue(); if (!(input instanceof Array)) { throw new IllegalArgumentException("Expected Array input, got " + input.getDataType().getName() + "."); } StringBuilder output = new StringBuilder(); for (Iterator<FieldValue> it = ((Array)input).fieldValueIterator(); it.hasNext(); ) { output.append(String.valueOf(it.next())); if (it.hasNext()) { output.append(delimiter); } } ctx.setValue(new StringFieldValue(output.toString())); }
public static <K extends FieldValue, V extends FieldValue> void serializeMapField(FieldWriter fieldWriter, JsonGenerator generator, FieldBase field, MapFieldValue<K, V> map) { fieldNameIfNotNull(generator, field); wrapIOException(() -> { generator.writeStartObject(); for (Map.Entry<K, V> entry : map.entrySet()) { K key = entry.getKey(); DataType keyType = key.getDataType(); if ( ! (keyType instanceof PrimitiveDataType)) { throw new IllegalArgumentException("Can't use complex types as keys for map fields. Type: " + keyType); } generator.writeFieldName(key.toString()); entry.getValue().serialize(null, fieldWriter); } generator.writeEndObject(); }); }
public void verifyElementCompatibility(T o) { if (!getDataType().getNestedType().isValueCompatible(o)) { throw new IllegalArgumentException( "Incompatible data types. Got " + o.getDataType() + ", expected " + getDataType().getNestedType()); } }
@Override protected void doExecute(ExecutionContext ctx) { FieldValue input = ctx.getValue(); if (!(input instanceof StructuredFieldValue)) { throw new IllegalArgumentException("Expected structured input, got " + input.getDataType().getName() + "."); } StructuredFieldValue struct = (StructuredFieldValue)input; Field field = struct.getField(fieldName); if (field == null) { throw new IllegalArgumentException("Field '" + fieldName + "' not found in struct type '" + struct.getDataType().getName() + "'"); } ctx.setValue(struct.getFieldValue(field)); }
@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); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected void doExecute(ExecutionContext ctx) { FieldValue input = ctx.getValue(); DataType inputType = input.getDataType(); ArrayDataType outputType = DataType.getArray(inputType); Array output = outputType.createFieldValue(); output.add(input); ctx.setValue(output); }
@Override public void onPrimitive(FieldValue fv) { if (retVal != null) { if (multiValue) { ((Array) retVal).add(fv); } else { Array afv = new Array(new ArrayDataType(retVal.getDataType())); afv.add(retVal); afv.add(fv); retVal = afv; multiValue = true; } } else { retVal = fv; } } }