Class LocationTree<D,T>

java.lang.Object
eu.bandm.tools.location.LocationTree<D,T>
Type Parameters:
D - the type of document identifiers
T - the type of subjects
All Implemented Interfaces:
Locatable<D>

public class LocationTree<D,T> extends Object implements Locatable<D>
A tree of labeled location information about subjects.

Objects of this class hold an arbitrary number of subjects, their locations and parameters, as well as hierarchical relationships among them. This information is suitable to track the genealogy of complex aggregate object structures, where components may hail from different contexts; e.g., the parts of a distributed document, or various call sites using an embedded domain-specific language.

Proper subtrees (i.e., other objects of this class) are organized in a local tree structure composed from leaves and branches. Leaves hold a proper subtree each. In a branch, there may be arbitrarily many alternatives which are distinguished by a label string. Labels should be valid Java identifiers. Every leaf is addressed uniquely by a sequence of zero or more label identifiers.

Additionally to subtrees, each tree root may hold a string-indexed map of parameter values. The keys should be valid Java identifiers. Parameters values may be arbitrary objects. They need not be distinct from the subjects of a tree by nature. However, most parameter values are likely simple data: wrapper objects for primitive values or strings.

Since:
1.1
  • Field Details

    • PATH_SEPARATOR

      public static final String PATH_SEPARATOR
      The string used to separate path elements.
      See Also:
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getMainDescription

      public String getMainDescription()
      Returns the main description of this tree.

      The main description should describe the root subject and/or the whole aggregate in a user-friendly way.

      Returns:
      the main description of this tree
    • getLocation

      public @Opt Location<D> getLocation()
      Returns the root location of this tree if available.
      Specified by:
      getLocation in interface Locatable<D>
      Returns:
      the root location of this tree, or null if not available
    • getSubject

      public @Opt T getSubject()
      Returns the root subject of this tree if available.
      Returns:
      the root subject of this tree, or null if not available
    • parameters

      public Map<String,Object> parameters()
      Returns the map of root parameters of this tree.

      The returned map should be treated as immutable. The caller should not attempt to modify its content, but may assume that the content does not change concurrently either.

      Returns:
      the map of root parameters of this tree
    • getRoot

      public LocationTree.Node<D,T> getRoot()
      Returns the root node of this tree.
      Returns:
      the root node of this tree
    • flatContent

      public Map<String,LocationTree<D,T>> flatContent()
      Returns a flat map of the proper subtrees of this tree.

      The returned map should be treated as immutable. The caller should not attempt to modify its content, but may assume that the content does not change concurrently either.

      Returns:
      a flat map of the subtrees of this tree
    • of

      public static <D, T> LocationTree<D,T> of(String mainDescription, @Opt Location<D> location, @Opt T subject)
      Returns a new tree without proper subtrees.
      Type Parameters:
      D - the type of document identifiers
      T - the type of subjects
      Parameters:
      mainDescription - the main description of the new tree
      location - the root location of the new tree, or null if not available
      subject - the root subject of the new tree, or null if not available
      Returns:
      a new leaf
      Throws:
      NullPointerException - if mainDescription is null
    • of

      public static <D, T> LocationTree<D,T> of(String mainDescription, @Opt Location<D> location, T subject, Supplier<LocationTree<D,T>> content)
      Returns a new tree with a single unlabeled proper subtree.

      The subtree will be computed lazily on demand after this method has returned.

      Type Parameters:
      D - the type of document identifiers
      T - the type of subjects
      Parameters:
      mainDescription - the main description of the new tree
      location - the root location of the new tree, or null if not available
      subject - the root subject of the new tree, or null if not available
      content - a functional object that provides the subtree
      Returns:
      a new tree
      Throws:
      NullPointerException - if mainDescription or content is null
    • of

      public static <D, T> LocationTree<D,T> of(String mainDescription, @Opt Location<D> location, T subject, Consumer<? super Collections.MapBuilder<String,Object>> parameters, Supplier<LocationTree<D,T>> content)
      Returns a new tree with parameters and a single unlabeled proper subtree.

      The subtree will be computed lazily on demand after this method has returned.

      Type Parameters:
      D - the type of document identifiers
      T - the type of subjects
      Parameters:
      mainDescription - the main description of the new tree
      location - the root location of the new tree, or null if not available
      subject - the root subject of the new tree, or null if not available
      parameters - a functional object that populates a map builder with parameter entries
      content - a functional object that provides the subtree
      Returns:
      a new tree
      Throws:
      NullPointerException - if mainDescription, parameters or content is null
    • of

      public static <D, T> LocationTree<D,T> of(String mainDescription, @Opt Location<D> location, T subject, Consumer<? super Collections.MapBuilder<String,Object>> parameters, Consumer<? super LocationTree.Builder<D,T>> branching)
      Returns a new tree with parameters and some proper subtrees.
      Type Parameters:
      D - the type of document identifiers
      T - the type of subjects
      Parameters:
      mainDescription - the main description of the new tree
      location - the root location of the new tree, or null if not available
      subject - the root subject of the new tree, or null if not available
      parameters - a functional object that populates a map builder with parameter entries
      branching - a functional object that populates the subtrees
      Returns:
      a new tree
      Throws:
      NullPointerException - if mainDescription, parameters or branching is null
    • empty

      public static <T> Consumer<T> empty()
      Returns a functional object that does nothing.

      The resulting object is useful to populate a map builder with no entries.

      Type Parameters:
      T - the type of objects to consume while doing nothing
      Returns:
      a functional object that does nothing