Class DefaultTreeTable

java.lang.Object
org.apache.sis.util.collection.DefaultTreeTable
All Implemented Interfaces:
Serializable, Cloneable, TreeTable

public class DefaultTreeTable extends Object implements TreeTable, Cloneable, Serializable
A TreeTable implementation with a list of columns given at construction time. The list of columns is unmodifiable, but the root node can be modified. Example: The setRoot(…) method accepts arbitrary TreeTable.Node implementations. However, it is likely to be safer and more memory efficient when used together with the implementation provided in the DefaultTreeTable.Node inner class.
Since:
0.3
Version:
1.2
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      For cross-version compatibility.
      See Also:
    • root

      private TreeTable.Node root
      The root node, or null if not yet specified.
      See Also:
    • columns

      private transient List<TableColumn<?>> columns
      The table columns as an unmodifiable list, or null if not yet created. The content of this list is the columnIndices keys sorted by their index values.
      See Also:
    • columnIndices

      final Map<TableColumn<?>,Integer> columnIndices
      The index of values associated to each column. This is used by the DefaultTreeTable.Node implementation for storing values in a single flat array. After creation, this map shall be read-only since many Node instances may share it.
      Implementation note: This field and the columns field could be computed from each other. But we serialize this field anyway because children nodes will typically hold a reference to that map, and we want to preserve the references tree.
      See Also:
  • Constructor Details

    • DefaultTreeTable

      DefaultTreeTable(Map<TableColumn<?>,Integer> columnIndices)
      Creates a new table using the given columns.
    • DefaultTreeTable

      public DefaultTreeTable(TableColumn<?>... columns)
      Creates a new tree table with the given columns. The given array shall not be null or empty, and shall not contain null or duplicated elements.
      Parameters:
      columns - the list of table columns.
    • DefaultTreeTable

      public DefaultTreeTable(DefaultTreeTable.Node root)
      Creates a new tree table initialized to the given root. The list of columns is inferred from the given node.
      Parameters:
      root - the tree table root (cannot be null).
  • Method Details

    • createColumnIndices

      static Map<TableColumn<?>,Integer> createColumnIndices(TableColumn<?>[] columns)
      Creates a map of column indices from the given list of columns. This method is invoked for initializing the columnIndices field.
      Parameters:
      columns - the list of columns.
      Returns:
      the map of column indices.
    • getColumns

      static TableColumn<?>[] getColumns(Map<TableColumn<?>,Integer> columnIndices)
      Returns all columns in the given map, sorted by increasing index value. This method relies on LinkedHashMap preserving insertion order.
      Returns:
      the columns in an array of elements of type TableColumn, not a subtype for allowing usage in UnmodifiableArrayList.wrap(Object[]).
    • getColumns

      public final List<TableColumn<?>> getColumns()
      Returns the table columns given at construction time. The returned list is never null neither empty.
      Specified by:
      getColumns in interface TreeTable
      Returns:
      the union of all table columns in every tree node.
      See Also:
    • getRoot

      public TreeTable.Node getRoot()
      Returns the root node. This method returns the node specified at construction time or to the last call of the setRoot(TreeTable.Node) method.
      Specified by:
      getRoot in interface TreeTable
      Returns:
      the root node of the tree.
    • setRoot

      public void setRoot(TreeTable.Node root)
      Sets the root to the given node. If a root already existed prior this method call, then the previous root node will be discarded.
      Parameters:
      root - the new root node (cannot be null).
      Throws:
      IllegalArgumentException - if the table columns in the given node are inconsistent with the table columns in this DefaultTreeTable.
    • initialize

      protected void initialize(TreeTable.Node root)
      Invoked when getRoot() is invoked for the first time and no root had been specified to the constructor. The root argument is a newly created empty node to be returned by getRoot(). The default implementation does nothing. Subclasses can override for lazy initialization of tree table content.
      Parameters:
      root - a newly created tree table root.
      Since:
      1.2
    • clone

      Returns a clone of this table. This method clones the root node. If the root is an instance of DefaultTreeTable.Node, then cloning the root will recursively clone all its children.
      Overrides:
      clone in class Object
      Returns:
      a clone of this table.
      Throws:
      CloneNotSupportedException - if this table, the root node or one of its children cannot be cloned.
      See Also:
    • equals

      public boolean equals(Object other)
      Compares the given object with this tree table for equality. This method compares the columns and the root node. If the later is an instance of the DefaultTreeTable.Node inner class, then all node values and children will be compared recursively.
      Overrides:
      equals in class Object
      Parameters:
      other - the object to compare with this table.
      Returns:
      true if the two objects are equal.
      See Also:
    • hashCode

      public int hashCode()
      Returns a hash code value for this table. This method is defined for consistency with equals(Object) contract.
      Overrides:
      hashCode in class Object
      See Also:
    • toString

      public String toString()
      Returns a string representation of this 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.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this tree table.