Package org.apache.sis.util.collection
Class TreeNodeList
- All Implemented Interfaces:
Serializable
,Iterable<TreeTable.Node>
,Collection<TreeTable.Node>
,List<TreeTable.Node>
,SequencedCollection<TreeTable.Node>
,CheckedContainer<TreeTable.Node>
- Direct Known Subclasses:
DefaultTreeTable.Node.Children
abstract class TreeNodeList
extends AbstractList<TreeTable.Node>
implements CheckedContainer<TreeTable.Node>, Serializable
A list of children in a
TreeTable.Node
. This list accepts only nodes that either have no
parent
at addition time, or already have the parent for which
this list manages the children. The add
and remove
operations shall
update the parent when needed.
This list does not support duplicated elements. Attempts to add a node which is already an
element of another TreeNodeList
will cause an IllegalArgumentException
to be
thrown.
Operations receiving a single TreeTable.Node
argument are all or nothing
operations: in case of failure, the list will be left in the same state as if no operation were
attempted. If a failure occurs during a bulk operations, then the list may be left in a state
where some elements where processed and others not.
Subclasses need to define the setParentOf(TreeTable.Node, int)
method
because the way to set the parent is specific to the node implementation:
Implementation note
We do not extendArrayList
because:
- We want to use identity comparisons rather than
Object.equals(Object)
. - We don't want this list to be cloneable, because it would complexify the management of references to the parent node.
- Extending
AbstractList
reduce the number of methods to override, sinceArrayList
overrides bulk operations with optimized code which are not suitable toTreeNodeList
(we need the slower path implemented inAbstractList
).
Implementation note:
Being serializable may seem contradictory with the non-cloneable requirement.
But serializing
TreeNodeList
will also serialize the parent, thus
creating new copy on deserialization. So the parents should not be mixed.- Since:
- 0.3
- Version:
- 0.3
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate TreeTable.Node[]
The children, ornull
if none.protected static final int
Enumeration constant forsetParentOf(TreeTable.Node, int)
.protected static final int
Enumeration constant forsetParentOf(TreeTable.Node, int)
.protected final TreeTable.Node
The parent of all children managed by this list.private static final long
For cross-version compatibility.private int
Number of valid elements in thechildren
array.protected static final int
Enumeration constant forsetParentOf(TreeTable.Node, int)
.Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
TreeNodeList
(TreeTable.Node parent) Creates an initially empty list. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(int index, TreeTable.Node node) Adds the given node at the given index in this list, shifting all nodes currently at and after the given index.(package private) final void
addChild
(int index, TreeTable.Node node) Adds the given node at the given index in this list, without any check for the parent.final boolean
Returnstrue
if this list contains the given node.private void
Ensures the the given node is not already present in this list.get
(int index) Returns the node at the specified index in this list.final Class
<TreeTable.Node> Returns the type of elements in this list.final int
Returns the index of the first occurrence of the specified node in this list.private boolean
isParentOf
(TreeTable.Node node) Returnstrue
if the node associated to this list is already the parent of the given node,false
if the given node has no parent, or throws an exception otherwise.final int
lastIndexOf
(Object node) Returns the index of the last occurrence of the specified node in this list.final TreeTable.Node
remove
(int index) Removes from this list the node at the given index.boolean
Removes the first occurrence of the given node from this list, if presents.protected void
removeRange
(int lower, int upper) Removes all children in the given range of this list.set
(int index, TreeTable.Node node) Sets the node at the specified index in this list.protected abstract void
setParentOf
(TreeTable.Node node, int mode) Sets or clears the parent of the given node.final int
size()
Returns the number of nodes in this list.Object[]
toArray()
Returns an array containing all the children in this list.Methods inherited from class java.util.AbstractList
add, addAll, clear, equals, hashCode, iterator, listIterator, listIterator, subList
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, removeAll, retainAll, toArray, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
addAll, addFirst, addLast, containsAll, getFirst, getLast, isEmpty, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, sort, spliterator, toArray
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDFor cross-version compatibility.- See Also:
-
NULL
protected static final int NULLEnumeration constant forsetParentOf(TreeTable.Node, int)
.- See Also:
-
THIS
protected static final int THISEnumeration constant forsetParentOf(TreeTable.Node, int)
.- See Also:
-
DRY_RUN
protected static final int DRY_RUNEnumeration constant forsetParentOf(TreeTable.Node, int)
.- See Also:
-
parent
The parent of all children managed by this list. -
children
The children, ornull
if none. This array will be created when first needed. -
size
private int sizeNumber of valid elements in thechildren
array.
-
-
Constructor Details
-
TreeNodeList
Creates an initially empty list.
-
-
Method Details
-
isParentOf
Returnstrue
if the node associated to this list is already the parent of the given node,false
if the given node has no parent, or throws an exception otherwise.- Parameters:
node
- the node for which to check the parent.- Returns:
true
if the given node already has its parent set, orfalse
otherwise.- Throws:
IllegalArgumentException
- if the given node is the children of another list.
-
setParentOf
Sets or clears the parent of the given node. This method doesn't need to care about the current node parent, sinceTreeNodeList
will take care of removing the tree node from its previous parent before to invoke this method.The
mode
argument specifies the parent value to set, as one of the following values:- Parameters:
node
- the node on which to set the parent (nevernull
).mode
- one of theNULL
,THIS
orDRY_RUN
constants.- Throws:
IllegalArgumentException
- if this method cannot set the parent of the given node.
-
getElementType
Returns the type of elements in this list.- Specified by:
getElementType
in interfaceCheckedContainer<TreeTable.Node>
- Returns:
- fixed to
TreeTable.Node
.
-
size
public final int size()Returns the number of nodes in this list.- Specified by:
size
in interfaceCollection<TreeTable.Node>
- Specified by:
size
in interfaceList<TreeTable.Node>
- Specified by:
size
in classAbstractCollection<TreeTable.Node>
- Returns:
- the number of nodes.
-
get
Returns the node at the specified index in this list.- Specified by:
get
in interfaceList<TreeTable.Node>
- Specified by:
get
in classAbstractList<TreeTable.Node>
- Parameters:
index
- the index of the node to fetch.- Returns:
- the node at the given index (never
null
).
-
set
Sets the node at the specified index in this list.- Specified by:
set
in interfaceList<TreeTable.Node>
- Overrides:
set
in classAbstractList<TreeTable.Node>
- Parameters:
index
- the index of the node to set.node
- the node to store at the given index (cannot benull
).- Returns:
- the node which was previously stored at the given index (never
null
). - Throws:
IllegalArgumentException
- if this list cannot add the given node, for example if the node is already an element of anotherTreeNodeList
.
-
add
Adds the given node at the given index in this list, shifting all nodes currently at and after the given index.- Specified by:
add
in interfaceList<TreeTable.Node>
- Overrides:
add
in classAbstractList<TreeTable.Node>
- Parameters:
index
- the index where to insert the node.node
- the node to store at the given index (cannot benull
).- Throws:
IllegalArgumentException
- if this list cannot add the given node, for example if the node is already an element of anotherTreeNodeList
.
-
addChild
Adds the given node at the given index in this list, without any check for the parent. The parent of the given node shall already be set tothis
before this method is invoked. -
ensureNotPresent
Ensures the the given node is not already present in this list. This checks is performed only if a newly added node declares to have this list parent. Such case may occur either because the node is a custom user implementation with pre-set parent, or because the node is already presents in this list.- Parameters:
node
- the node to check.- Throws:
IllegalArgumentException
- if the given node is already present in this list.
-
removeRange
Removes all children in the given range of this list. This method removes the nodes in reverse order (last added nodes are removed first). If this method failed to remove a node, then that node and all nodes at lower index will be left in the list.- Overrides:
removeRange
in classAbstractList<TreeTable.Node>
- Throws:
IllegalArgumentException
- if this method failed to remove a node in the given range.
-
remove
Removes from this list the node at the given index. All nodes after the given index will be shifted by one.- Specified by:
remove
in interfaceList<TreeTable.Node>
- Overrides:
remove
in classAbstractList<TreeTable.Node>
- Parameters:
index
- the index of the node to remove.- Returns:
- the node which was previously at the given index (never
null
). - Throws:
IllegalArgumentException
-
remove
Removes the first occurrence of the given node from this list, if presents. The default implementation searches the node using theindexOf(Object)
, then removes it (if the node has been found) using theremove(int)
method.- Specified by:
remove
in interfaceCollection<TreeTable.Node>
- Specified by:
remove
in interfaceList<TreeTable.Node>
- Overrides:
remove
in classAbstractCollection<TreeTable.Node>
- Parameters:
node
- the node to remove.null
values are ignored.- Returns:
true
if the node has been removed, orfalse
if this list does not contain the given node.- Throws:
IllegalArgumentException
- if the node has been found but this list cannot remove it.
-
contains
Returnstrue
if this list contains the given node. This implementation only checks if the node parent is theparent
instance. This implementation does not iterate over the children.- Specified by:
contains
in interfaceCollection<TreeTable.Node>
- Specified by:
contains
in interfaceList<TreeTable.Node>
- Overrides:
contains
in classAbstractCollection<TreeTable.Node>
- Parameters:
node
- the node to check (can benull
).- Returns:
true
if this list contains the given node.
-
indexOf
Returns the index of the first occurrence of the specified node in this list. This method delegates tolastIndexOf(Object)
because the list is not expected to contain duplicated values.- Specified by:
indexOf
in interfaceList<TreeTable.Node>
- Overrides:
indexOf
in classAbstractList<TreeTable.Node>
- Parameters:
node
- the node to search (can benull
).- Returns:
- index of the given node, or -1 if not found.
-
lastIndexOf
Returns the index of the last occurrence of the specified node in this list.- Specified by:
lastIndexOf
in interfaceList<TreeTable.Node>
- Overrides:
lastIndexOf
in classAbstractList<TreeTable.Node>
- Parameters:
node
- the node to search (can benull
).- Returns:
- index of the given node, or -1 if not found.
-
toArray
Returns an array containing all the children in this list.- Specified by:
toArray
in interfaceCollection<TreeTable.Node>
- Specified by:
toArray
in interfaceList<TreeTable.Node>
- Overrides:
toArray
in classAbstractCollection<TreeTable.Node>
-