Class TreeTables

java.lang.Object
org.apache.sis.util.Static
org.apache.sis.util.collection.TreeTables

public final class TreeTables extends Static
Static methods working on TreeTable objects and their nodes. This class provides methods for some tasks considered generic enough, and example codes for more specialized tasks that developers can customize.

The remaining of this class javadoc contains example codes placed in public domain. Developers can copy and adapt those examples as they see fit.

Example 1: Reduce the depth of a tree

For every branch containing exactly one child, the following method concatenates in-place that branch and its child together. This method can be used for simplifying depth trees into something less verbose. For example, given the tree on the left side, this method transforms it into the tree on the right side:
Example of tree depth reduction
BeforeAfter
There is no predefined method for this task because there is too many parameters that developers may want to customize (columns to merge, conditions for accepting the merge, kind of objects to merge, name separator, etc.). In the following code snippet, the content of the NAME columns are concatenated only if the VALUE column has no value (for avoiding data lost when the node is discarded) and use the system file separator as name separator:
Since:
0.3
Version:
0.3
See Also:
  • Constructor Details

    • TreeTables

      private TreeTables()
      Do not allow instantiation of this class.
  • Method Details

    • nodeForPath

      public static TreeTable.Node nodeForPath(TreeTable.Node from, TableColumn<? super String> column, Path path)
      Finds the node for the given path, or creates a new node if none exists. First, this method searches in the node children collection for the root element of the given path. If no such node is found, a new child is created. Then this method repeats the process (searching in the children of the child for the second path element), until the last path element is reached.

      For example if the given path is "users/alice/data", then this method finds or creates the nodes for the following tree, where "from" is the node given in argument to this method:

      Parameters:
      from - the root node from which to start the search.
      column - the column containing the file name.
      path - the path for which to find or create a node.
      Returns:
      the node for the given path, either as an existing node or a new node.
    • nodeForPath

      public static TreeTable.Node nodeForPath(TreeTable.Node from, TableColumn<? super String> column, File path)
      Finds the node for the given file, or creates a new node if none exists. This method performs the same work than the above variant, but working on File instances rather than Path.
      Parameters:
      from - the root node from which to start the search.
      column - the column containing the file name.
      path - the file for which to find or create a node.
      Returns:
      the node for the given file, either as an existing node or a new node.
    • replaceCharSequences

      public static int replaceCharSequences(TreeTable table, Locale locale)
      For every columns having values of type CharSequence or String, converts the values to localized Strings. During conversions, this method also replaces duplicated String instances by references to the same singleton instance.

      This method may be invoked before to serialize the table in order to reduce the serialization stream size.

      Parameters:
      table - the table in which to replace values by their string representations.
      locale - the locale to use when replacing InternationalString instances. Can be null.
      Returns:
      number of replacements done.
    • replaceCharSequences

      private static int replaceCharSequences(TreeTable.Node node, TableColumn<? super String>[] columns, Locale locale, Map<String,String> pool)
      Implementation of the public replaceCharSequences(TreeTable, Locale) method.
      Parameters:
      node - the node in which to replace values by their string representations.
      columns - the columns where to perform the replacements.
      locale - the locale to use when replacing InternationalString instances. Can be null.
      pool - an initially empty pool of string representations, to be filled by this method.
      Returns:
      number of replacements done.
    • toString

      public static String toString(TreeTable table)
      Returns a string representation of the given tree table. The current implementation uses a shared instance of TreeTableFormat. This is okay for debugging or occasional usages. However for more extensive usages, developers are encouraged to create and configure their own TreeTableFormat instance.
      Parameters:
      table - the tree table to format.
      Returns:
      a string representation of the given tree table.
    • parse

      public static TreeTable parse(String tree, TableColumn<?> labelColumn, TableColumn<?>... otherColumns) throws ParseException
      Parses the given string as tree. This helper method is sometimes useful for quick tests or debugging purposes. For more extensive use, consider using TreeTableFormat instead.
      Parameters:
      tree - the string representation of the tree to parse.
      labelColumn - the columns where to store the node labels. This is often TableColumn.NAME.
      otherColumns - optional columns where to store the values, if any.
      Returns:
      a tree parsed from the given string.
      Throws:
      ParseException - if an error occurred while parsing the tree.