/** * Compares this field path with the given field path, returns true if the field path starts with the other. * * @param other The field path to compare with. * @return Returns true if this field path starts with the other field path, otherwise false */ public boolean startsWith(FieldPath other) { if (other.size() > size()) { return false; } for (int i = 0; i < other.size(); i++) { if (!other.get(i).equals(get(i))) { return false; } } return true; }
/** * note that the returned Field may not be in this Document * directly, but may refer to a field in a struct contained in it, * in which case the returned Field is only useful for obtaining * the field type; it can't be used for get() and set(). **/ @Override public Field getField(String fieldName) { if (fieldMap != null && fieldMap.containsKey(fieldName)) { fieldName = fieldMap.get(fieldName); } FieldPath path = getFieldPath(fieldName); Field ret = path.get(path.size() - 1).getFieldRef(); checkAccess(ret); return ret; }
/** * @return Returns the datatype we can expect this field path to return. */ public DataType getResultingDataType() { if (isEmpty()) { return null; } return get(size() - 1).getResultingDataType(); }
public static boolean isComplete(FieldPathUpdate update) { if (!(update instanceof AssignFieldPathUpdate)) { return false; } // Only consider field path updates that touch a top-level field as 'complete', // as these may be converted to regular field value updates. return ((update.getFieldPath().size() == 1) && update.getFieldPath().get(0).getType() == FieldPathEntry.Type.STRUCT_FIELD); }
@Override FieldPathIteratorHandler.ModificationStatus iterateNested(FieldPath fieldPath, int pos, FieldPathIteratorHandler handler) { if (pos < fieldPath.size()) { switch (fieldPath.get(pos).getType()) { case ARRAY_INDEX: final int elemIndex = fieldPath.get(pos).getLookupIndex(); return iterateSubset(elemIndex, elemIndex, fieldPath, null, pos + 1, handler); case VARIABLE: { FieldPathIteratorHandler.IndexValue val = handler.getVariables().get(fieldPath.get(pos).getVariableName()); if (val != null) { int idx = val.getIndex(); return iterateSubset(0, values.size() - 1, fieldPath, fieldPath.get(pos).getVariableName(), pos + 1, handler);
FieldPathIteratorHandler handler) { if (pos < fieldPath.size()) { if (fieldPath.get(pos).getType() == FieldPathEntry.Type.STRUCT_FIELD) { FieldValue fieldVal = getFieldValue(fieldPath.get(pos).getFieldRef()); if (fieldVal != null) { FieldPathIteratorHandler.ModificationStatus status = fieldVal.iterateNested(fieldPath, pos + 1, handler); if (status == FieldPathIteratorHandler.ModificationStatus.REMOVED) { removeFieldValue(fieldPath.get(pos).getFieldRef()); return FieldPathIteratorHandler.ModificationStatus.MODIFIED; } else { setFieldValue(fieldPath.get(pos).getFieldRef(), fieldVal); FieldValue newVal = fieldPath.get(pos).getFieldRef().getDataType().createFieldValue(); FieldPathIteratorHandler.ModificationStatus status = newVal.iterateNested(fieldPath, pos + 1, handler); if (status == FieldPathIteratorHandler.ModificationStatus.MODIFIED) { setFieldValue(fieldPath.get(pos).getFieldRef(), newVal); return status; throw new IllegalArgumentException("Illegal field path " + fieldPath.get(pos) + " for struct value"); } else { FieldPathIteratorHandler.ModificationStatus status = handler.modify(this);
switch (fieldPath.get(pos).getType()) { case MAP_KEY: FieldValue val = values.get(fieldPath.get(pos).getLookupKey()); if (val != null) { wasModified = checkAndRemove(fieldPath.get(pos).getLookupKey(), val.iterateNested(fieldPath, pos + 1, handler), wasModified, keysToRemove); } else if (handler.createMissingPath()) { val = getDataType().getValueType().createFieldValue(); FieldPathIteratorHandler.ModificationStatus status = val.iterateNested(fieldPath, pos + 1, handler); if (status == FieldPathIteratorHandler.ModificationStatus.MODIFIED) { put((K)fieldPath.get(pos).getLookupKey(), (V)val); return status; case VARIABLE: FieldPathIteratorHandler.IndexValue idx = handler.getVariables().get(fieldPath.get(pos).getVariableName()); if (idx != null) { FieldValue val = values.get(idx.getKey()); handler.getVariables().put(fieldPath.get(pos).getVariableName(), new FieldPathIteratorHandler.IndexValue(entry.getKey())); wasModified = checkAndRemove(entry.getKey(), entry.getValue().iterateNested(fieldPath, pos + 1, handler), wasModified, keysToRemove); handler.getVariables().remove(fieldPath.get(pos).getVariableName());
FieldPath updatePath = update.getFieldPath(); if (idx < updatePath.size()) { FieldPathEntry pathEntry = updatePath.get(idx); FieldPathEntry.Type type = pathEntry.getType(); if (type == FieldPathEntry.Type.STRUCT_FIELD) {