Class FieldMaskTree


  • final class FieldMaskTree
    extends java.lang.Object
    A tree representation of a FieldMask. Each leaf node in this tree represent a field path in the FieldMask.

    For example, FieldMask "foo.bar,foo.baz,bar.baz" as a tree will be:

       [root] -+- foo -+- bar
               |       |
               |       +- baz
               |
               +- bar --- baz
     

    By representing FieldMasks with this tree structure we can easily convert a FieldMask to a canonical form, merge two FieldMasks, calculate the intersection to two FieldMasks and traverse all fields specified by the FieldMask in a message tree.

    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • FIELD_PATH_SEPARATOR_REGEX

        private static final java.lang.String FIELD_PATH_SEPARATOR_REGEX
        See Also:
        Constant Field Values
    • Constructor Detail

      • FieldMaskTree

        FieldMaskTree()
        Creates an empty FieldMaskTree.
      • FieldMaskTree

        FieldMaskTree​(FieldMask mask)
        Creates a FieldMaskTree for a given FieldMask.
    • Method Detail

      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • addFieldPath

        @CanIgnoreReturnValue
        FieldMaskTree addFieldPath​(java.lang.String path)
        Adds a field path to the tree. In a FieldMask, every field path matches the specified field as well as all its sub-fields. For example, a field path "foo.bar" matches field "foo.bar" and also "foo.bar.baz", etc. When adding a field path to the tree, redundant sub-paths will be removed. That is, after adding "foo.bar" to the tree, "foo.bar.baz" will be removed if it exists, which will turn the tree node for "foo.bar" to a leaf node. Likewise, if the field path to add is a sub-path of an existing leaf node, nothing will be changed in the tree.
      • mergeFromFieldMask

        @CanIgnoreReturnValue
        FieldMaskTree mergeFromFieldMask​(FieldMask mask)
        Merges all field paths in a FieldMask into this tree.
      • removeFieldPath

        @CanIgnoreReturnValue
        FieldMaskTree removeFieldPath​(java.lang.String path)
        Removes path from the tree.
          When removing a field path from the tree:
        • All sub-paths will be removed. That is, after removing "foo.bar" from the tree, "foo.bar.baz" will be removed.
        • If all children of a node have been removed, the node itself will be removed as well. That is, if "foo" only has one child "bar" and "foo.bar" only has one child "baz", removing "foo.bar.barz" would remove both "foo" and "foo.bar". If "foo" has both "bar" and "moo" as children, removing "foo.bar" would leave the path "foo.moo" intact.
        • If the field path to remove is a non-exist sub-path, nothing will be changed.
      • removeFieldPath

        @CanIgnoreReturnValue
        private static boolean removeFieldPath​(FieldMaskTree.Node node,
                                               java.util.List<java.lang.String> parts,
                                               int index)
        Removes parts from node recursively.
        Returns:
        a boolean value indicating whether current node should be removed.
      • removeFromFieldMask

        @CanIgnoreReturnValue
        FieldMaskTree removeFromFieldMask​(FieldMask mask)
        Removes all field paths in mask from this tree.
      • toFieldMask

        FieldMask toFieldMask()
        Converts this tree to a FieldMask.
      • getFieldPaths

        private static void getFieldPaths​(FieldMaskTree.Node node,
                                          java.lang.String path,
                                          java.util.List<java.lang.String> paths)
        Gathers all field paths in a sub-tree.
      • intersectFieldPath

        void intersectFieldPath​(java.lang.String path,
                                FieldMaskTree output)
        Adds the intersection of this tree with the given path to output.