Package org.apache.sis.util.collection
Class TreeNodeList
- All Implemented Interfaces:
Serializable,Iterable<TreeTable.Node>,Collection<TreeTable.Node>,List<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
AbstractListreduce the number of methods to override, sinceArrayListoverrides 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, ornullif none.protected static final intEnumeration constant forsetParentOf(TreeTable.Node, int).protected static final intEnumeration constant forsetParentOf(TreeTable.Node, int).protected final TreeTable.NodeThe parent of all children managed by this list.private static final longFor cross-version compatibility.private intNumber of valid elements in thechildrenarray.protected static final intEnumeration constant forsetParentOf(TreeTable.Node, int).Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedTreeNodeList(TreeTable.Node parent) Creates an initially empty list. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(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 voidaddChild(int index, TreeTable.Node node) Adds the given node at the given index in this list, without any check for the parent.final booleanReturnstrueif this list contains the given node.private voidEnsures 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 intReturns the index of the first occurrence of the specified node in this list.private booleanisParentOf(TreeTable.Node node) Returnstrueif the node associated to this list is already the parent of the given node,falseif the given node has no parent, or throws an exception otherwise.final intlastIndexOf(Object node) Returns the index of the last occurrence of the specified node in this list.final TreeTable.Noderemove(int index) Removes from this list the node at the given index.booleanRemoves the first occurrence of the given node from this list, if presents.protected voidremoveRange(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 voidsetParentOf(TreeTable.Node node, int mode) Sets or clears the parent of the given node.final intsize()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, subListMethods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, removeAll, retainAll, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, streamMethods inherited from interface java.util.List
addAll, containsAll, isEmpty, removeAll, replaceAll, retainAll, 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, ornullif none. This array will be created when first needed. -
size
private int sizeNumber of valid elements in thechildrenarray.
-
-
Constructor Details
-
TreeNodeList
Creates an initially empty list.
-
-
Method Details
-
isParentOf
Returnstrueif the node associated to this list is already the parent of the given node,falseif the given node has no parent, or throws an exception otherwise.- Parameters:
node- the node for which to check the parent.- Returns:
trueif the given node already has its parent set, orfalseotherwise.- 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, sinceTreeNodeListwill take care of removing the tree node from its previous parent before to invoke this method.The
modeargument 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,THISorDRY_RUNconstants.- Throws:
IllegalArgumentException- if this method cannot set the parent of the given node.
-
getElementType
Returns the type of elements in this list.- Specified by:
getElementTypein interfaceCheckedContainer<TreeTable.Node>- Returns:
- fixed to
TreeTable.Node.
-
size
public final int size()Returns the number of nodes in this list.- Specified by:
sizein interfaceCollection<TreeTable.Node>- Specified by:
sizein interfaceList<TreeTable.Node>- Specified by:
sizein classAbstractCollection<TreeTable.Node>- Returns:
- the number of nodes.
-
get
Returns the node at the specified index in this list.- Specified by:
getin interfaceList<TreeTable.Node>- Specified by:
getin 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:
setin interfaceList<TreeTable.Node>- Overrides:
setin 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:
addin interfaceList<TreeTable.Node>- Overrides:
addin 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 tothisbefore 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:
removeRangein 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:
removein interfaceList<TreeTable.Node>- Overrides:
removein 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:
removein interfaceCollection<TreeTable.Node>- Specified by:
removein interfaceList<TreeTable.Node>- Overrides:
removein classAbstractCollection<TreeTable.Node>- Parameters:
node- the node to remove.nullvalues are ignored.- Returns:
trueif the node has been removed, orfalseif this list does not contain the given node.- Throws:
IllegalArgumentException- if the node has been found but this list cannot remove it.
-
contains
Returnstrueif this list contains the given node. This implementation only checks if the node parent is theparentinstance. This implementation does not iterate over the children.- Specified by:
containsin interfaceCollection<TreeTable.Node>- Specified by:
containsin interfaceList<TreeTable.Node>- Overrides:
containsin classAbstractCollection<TreeTable.Node>- Parameters:
node- the node to check (can benull).- Returns:
trueif 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:
indexOfin interfaceList<TreeTable.Node>- Overrides:
indexOfin 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:
lastIndexOfin interfaceList<TreeTable.Node>- Overrides:
lastIndexOfin 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:
toArrayin interfaceCollection<TreeTable.Node>- Specified by:
toArrayin interfaceList<TreeTable.Node>- Overrides:
toArrayin classAbstractCollection<TreeTable.Node>
-