/** * Write out the value of array field * * @param field - field description (name and data type) * @param array - field value */ public <T extends FieldValue> void write(FieldBase field, Array<T> array) { buf.putInt1_2_4Bytes(array.size()); List<T> lst = array.getValues(); for (FieldValue value : lst) { value.serialize(this); } }
@Override public void assign(Object o) { if (!checkAssign(o)) { return; } if (o instanceof Array) { if (o == this) return; Array a = (Array) o; values.clear(); addAll(a.values); } else if (o instanceof List) { values = new ListWrapper<T>((List) o); } else { throw new IllegalArgumentException("Class " + o.getClass() + " not applicable to an " + this.getClass() + " instance."); } }
@SuppressWarnings({ "unchecked" }) private List<ValueUpdate> createAddValueUpdateForArray(Array arr, int weight) { List<ValueUpdate> ret = new ArrayList<>(arr.size()); for (Iterator<FieldValue> it = arr.fieldValueIterator(); it.hasNext(); ) { ret.add(new AddValueUpdate(it.next(), weight)); } return ret; }
public <T extends FieldValue> void read(FieldBase field, Array<T> array) { int numElements = getNumCollectionElems(); ArrayList<T> list = new ArrayList<T>(numElements); ArrayDataType type = array.getDataType(); for (int i = 0; i < numElements; i++) { if (version < 7) { getInt(null); // We don't need size for anything } FieldValue fv = type.getNestedType().createFieldValue(); fv.deserialize(null, this); list.add((T) fv); } array.clear(); array.addAll(list); }
@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); }
public <T extends FieldValue> void read(FieldBase field, Array<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(); deserializeFieldValue(field, fv); // noinspection unchecked value.add((T)fv); skipToEnd("item"); } } else if (type == XMLStreamReader.END_ELEMENT) { return; } } } catch (XMLStreamException e) { throw newException(field, e); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private FieldValue convertArray(Array val) { List<FieldValue> next = new LinkedList<FieldValue>(); DataType nextType = null; for (Iterator<FieldValue> it = val.fieldValueIterator(); it.hasNext();) { FieldValue prevVal = it.next(); FieldValue nextVal = convert(prevVal); if (nextVal == null) { continue; } if (nextType == null) { nextType = nextVal.getDataType(); } else if (!nextType.isValueCompatible(nextVal)) { throw new IllegalArgumentException("Expected " + nextType.getName() + ", got " + nextVal.getDataType().getName() + "."); } next.add(nextVal); } if (nextType == null) { return null; } Array ret = DataType.getArray(nextType).createFieldValue(); for (FieldValue nextVal : next) { ret.add(nextVal); } return ret; }
private void split(Processing processing, Array<Document> innerDocuments) { processing.setVariable(contextFieldName, processing.getDocumentOperations().get(0)); processing.getDocumentOperations().clear(); processing.getDocumentOperations().addAll(innerDocuments.stream() .map(DocumentPut::new) .collect(Collectors.toList())); innerDocuments.clear(); }
@Override public Progress process(Processing processing) { if (processing.getDocumentOperations().size() != 1) { //we were given more than one document, return log.log(LogLevel.DEBUG, "More than one document given, returning. (Was given " + processing.getDocumentOperations().size() + " documents)."); return Progress.DONE; } if (!doProcessOuterDocument(processing.getDocumentOperations().get(0), documentTypeName)) { return Progress.DONE; } Document outerDoc = ((DocumentPut)processing.getDocumentOperations().get(0)).getDocument();; @SuppressWarnings("unchecked") Array<Document> innerDocuments = (Array<Document>) outerDoc.getFieldValue(arrayFieldName); if (innerDocuments == null) { //the document does not have the field, return log.log(LogLevel.DEBUG, "The given Document does not have a field value for field " + arrayFieldName + ", returning. (Was given " + outerDoc + ")."); return Progress.DONE; } if (innerDocuments.size() == 0) { //the array is empty, return log.log(LogLevel.DEBUG, "The given Document does not have any elements in array field " + arrayFieldName + ", returning. (Was given " + outerDoc + ")."); return Progress.DONE; } split(processing, innerDocuments); return Progress.DONE; }
public Array createFieldValue() { return new Array(this); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private static FieldValue asArray(ArrayDataType arrType, List<FieldValue> values) { Array out = arrType.createFieldValue(); for (FieldValue val : values) { if (val == null) { continue; } out.addAll((Array)val); } return out; }
@SuppressWarnings({ "unchecked" }) private List<ValueUpdate> createMapValueUpdatesForArray(Array arr, MapValueUpdate upd) { List<ValueUpdate> ret = new ArrayList<>(); for (Iterator<FieldValue> it = arr.fieldValueIterator(); it.hasNext();) { FieldValue childVal = it.next(); for (ValueUpdate childUpd : createValueUpdates(childVal, upd.getUpdate())) { ret.add(new MapValueUpdate(childVal, childUpd)); } } return ret; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; IteratorHandler that = (IteratorHandler) o; if (!newValues.equals(that.newValues)) return false; return true; }
public <T extends FieldValue> void read(FieldBase field, Array<T> array) { int numElements = getNumCollectionElems(); ArrayList<T> list = new ArrayList<T>(numElements); ArrayDataType type = array.getDataType(); for (int i = 0; i < numElements; i++) { if (version < 7) { getInt(null); // We don't need size for anything } FieldValue fv = type.getNestedType().createFieldValue(); fv.deserialize(null, this); list.add((T) fv); } array.clear(); array.addAll(list); }
} 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()); } 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);
@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())); }
@Override protected void doExecute(ExecutionContext ctx) { String input = String.valueOf(ctx.getValue()); Array<StringFieldValue> output = new Array<>(DataType.getArray(DataType.STRING)); if (!input.isEmpty()) { String[] splits = splitPattern.split(input); for (String split : splits) { output.add(new StringFieldValue(split)); } } ctx.setValue(output); }
/** * Write out the value of array field * * @param field - field description (name and data type) * @param array - field value */ public <T extends FieldValue> void write(FieldBase field, Array<T> array) { buf.putInt1_2_4Bytes(array.size()); List<T> lst = array.getValues(); for (FieldValue value : lst) { value.serialize(this); } }
@Override public Progress process(Processing processing) { if ( ! doProcessOuterDocument(processing.getVariable(contextFieldName), documentTypeName)) { return Progress.DONE; } DocumentPut outerDoc = (DocumentPut)processing.getVariable(contextFieldName); @SuppressWarnings("unchecked") Array<Document> innerDocuments = (Array<Document>) outerDoc.getDocument().getFieldValue(arrayFieldName); if (innerDocuments == null) { @SuppressWarnings("unchecked") Array<Document> empty = (Array<Document>) outerDoc.getDocument().getDataType().getField(arrayFieldName).getDataType().createFieldValue(); innerDocuments = empty; } for (DocumentOperation op : processing.getDocumentOperations()) { if (op instanceof DocumentPut) { innerDocuments.add(((DocumentPut)op).getDocument()); } else { log.log(LogLevel.DEBUG, "Skipping: " + op); } } processing.getDocumentOperations().clear(); processing.getDocumentOperations().add(outerDoc); processing.removeVariable(contextFieldName); return Progress.DONE; }