/** * Creates a union of two or more FieldMasks. */ public static FieldMask union( FieldMask firstMask, FieldMask secondMask, FieldMask... otherMasks) { FieldMaskTree maskTree = new FieldMaskTree(firstMask).mergeFromFieldMask(secondMask); for (FieldMask mask : otherMasks) { maskTree.mergeFromFieldMask(mask); } return maskTree.toFieldMask(); }
/** * Calculates the intersection of two FieldMasks. */ public static FieldMask intersection(FieldMask mask1, FieldMask mask2) { FieldMaskTree tree = new FieldMaskTree(mask1); FieldMaskTree result = new FieldMaskTree(); for (String path : mask2.getPathsList()) { tree.intersectFieldPath(path, result); } return result.toFieldMask(); }
/** * Merges fields specified by a FieldMask from one message to another with the * specified merge options. */ public static void merge( FieldMask mask, Message source, Message.Builder destination, MergeOptions options) { new FieldMaskTree(mask).merge(source, destination, options); }
/** * Converts a FieldMask to its canonical form. In the canonical form of a * FieldMask, all field paths are sorted alphabetically and redundant field * paths are moved. */ public static FieldMask normalize(FieldMask mask) { return new FieldMaskTree(mask).toFieldMask(); }
if (node != root && node.children.isEmpty()) { output.addFieldPath(path); return; getFieldPaths(node, path, paths); for (String value : paths) { output.addFieldPath(value);
/** * Gathers all field paths in a sub-tree. */ private void getFieldPaths(Node node, String path, List<String> paths) { if (node.children.isEmpty()) { paths.add(path); return; } for (Entry<String, Node> entry : node.children.entrySet()) { String childPath = path.isEmpty() ? entry.getKey() : path + "." + entry.getKey(); getFieldPaths(entry.getValue(), childPath, paths); } }
/** * Merges all fields specified by this FieldMaskTree from {@code source} to {@code destination}. */ void merge(Message source, Message.Builder destination, FieldMaskUtil.MergeOptions options) { if (source.getDescriptorForType() != destination.getDescriptorForType()) { throw new IllegalArgumentException("Cannot merge messages of different types."); } if (root.children.isEmpty()) { return; } merge(root, "", source, destination, options); }
/** * Creates a FieldMaskTree for a given FieldMask. */ FieldMaskTree(FieldMask mask) { mergeFromFieldMask(mask); }
/** * Merges all field paths in a FieldMask into this tree. */ FieldMaskTree mergeFromFieldMask(FieldMask mask) { for (String path : mask.getPathsList()) { addFieldPath(path); } return this; }
@Override public String toString() { return FieldMaskUtil.toString(toFieldMask()); }
/** * Converts this tree to a FieldMask. */ FieldMask toFieldMask() { if (root.children.isEmpty()) { return FieldMask.getDefaultInstance(); } List<String> paths = new ArrayList<String>(); getFieldPaths(root, "", paths); return FieldMask.newBuilder().addAllPaths(paths).build(); }
merge( entry.getValue(), childPath,