Class TreeNodeChildren
- All Implemented Interfaces:
Iterable<TreeTable.Node>
,Collection<TreeTable.Node>
TreeNode.getChildren()
.
This collection holds a reference to the metadata object at creation time;
it does not track changes in parent.getUserObject()
.
Note on value existence policy
It is better to use this class withValueExistencePolicy.NON_EMPTY
in order
to avoid code complication and surprising behavior of TreeNodeChildren.Iter.remove()
operation.
If the policy is set to another value, we need to keep the following aspects in mind:
- When
TreeNodeChildren.Iter.hasNext()
finds a null or empty collection, it may needs to simulate a singleton with a null value. - In
TreeNode.getUserObject()
, we need the same check than above for simulating a singleton collection with a null value if the node is for the element at index 0.
- Since:
- 0.3
- Version:
- 1.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final class
The iterator over the elements in the enclosingTreeNodeChildren
collection. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final PropertyAccessor
The accessor to use for accessing the property names, types and values of themetadata
object.private final TreeNode.Element[]
The children to be returned by this collection.(package private) final Object
The metadata object for which property values will be the elements of this collection.(package private) int
Modification count, incremented when the content of this collection is modified.private final TreeNode
The parent of the children to be returned by the iterator.(package private) final int
Index of the property to write in the parent node instead of as a child. -
Constructor Summary
ConstructorsConstructorDescriptionTreeNodeChildren
(TreeNode parent, Object metadata, PropertyAccessor accessor) Creates a collection of children for the specified metadata. -
Method Summary
Modifier and TypeMethodDescription(package private) final boolean
Implementation ofadd(TreeTable.Node)
, also invoked byTreeNode.NewChild
.boolean
add
(TreeTable.Node node) Adds the given node to this list.(package private) final TreeNode.Element
childAt
(int index, int subIndex) Returns the child at the given index, creating it if needed.(package private) final int
Returns the maximal number of children.void
clear()
Clears all properties in the metadata object.(package private) final void
clearAt
(int index) Clears the value at the given index.(package private) final Object
If a simple value should be associated to the parent node, returns that value.(package private) final Class
<?> If a simple value should be associated to the parent node, returns the type of that value.(package private) final boolean
isCollectionOrMap
(int index) Returnstrue
if the type at the given index is a collection or a map.boolean
isEmpty()
Returnstrue
if this collection contains no elements.(package private) final boolean
Returnstrue
if the give value shall be skipped by the iterators, according the value policy.iterator()
Returns an iterator over the nodes in the collection of children.(package private) final boolean
setParentTitle
(Object value) Sets the value associated to the parent node, if possible.int
size()
Returns the number of elements in this collection, ignoring theskipped
ones.toString()
Returns a string representation of this collection for debugging purpose.(package private) final Object
valueAt
(int index) Returns the value at the given index.Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
parent
The parent of the children to be returned by the iterator. Some useful information are available indirectly through this parent:ValueExistencePolicy
:parent.table.valuePolicy
- See Also:
-
metadata
The metadata object for which property values will be the elements of this collection. This is typically anAbstractMetadata
instance, but not necessarily. Any type for whichMetadataStandard.isMetadata(Class)
returnstrue
is okay.This field is a snapshot of the parent
TreeNode.getUserObject()
at creation time. This collection does not track changes in the reference returned by the above-citedgetUserObject()
. In other words, changes in themetadata
object will be reflected in this collection, but ifparent.getUserObject()
returns a reference to another object, this change will not be reflected in this collection. -
accessor
The accessor to use for accessing the property names, types and values of themetadata
object. This is given at construction time and shall be the same than the following code: -
children
The children to be returned by this collection. All elements in this collection are initiallynull
, then created bychildAt(int, int)
when first needed.Not all elements in this array will be returned by the iterator. The value needs to be verified for the
ValueExistencePolicy
. -
titleProperty
final int titlePropertyIndex of the property to write in the parent node instead of as a child. If a property has the same name than the parent property that contains it, we write its value in that parent property. For example, instead of: We simplify as:- See Also:
-
modCount
int modCountModification count, incremented when the content of this collection is modified. This check is done on a best effort basis only, since we cannot not track the changes which are done independently in the metadata object.
-
-
Constructor Details
-
TreeNodeChildren
TreeNodeChildren(TreeNode parent, Object metadata, PropertyAccessor accessor) Creates a collection of children for the specified metadata.- Parameters:
parent
- the parent for which this node is an element.metadata
- the metadata object for which property values will be the elements of this collection.accessor
- the accessor to use for accessing the property names, types and values of the metadata object.
-
-
Method Details
-
getParentType
If a simple value should be associated to the parent node, returns the type of that value. Otherwise returnsnull
. -
getParentTitle
If a simple value should be associated to the parent node, returns that value. Otherwise returnsnull
. -
setParentTitle
Sets the value associated to the parent node, if possible. This returned boolean tells whether the value has been written. -
clearAt
final void clearAt(int index) Clears the value at the given index. The givenindex
is relative to theaccessor
indexing, not to this collection.The cleared elements may or may not be considered as removed, depending on the value policy. To check if the element shall be considered as removed (for example in order to update index), invoke
isSkipped(value)
after this method.Implementation note
This method sets the property tonull
. This is not strictly correct for collections, since we should rather set the property to an empty collection. However, this approach would force us to check if the expected collection type is actually a list, a set or any other type. Passing null avoid the type check and is safe at least with SIS implementation. We may revisit later if this appears to be a problem with other implementations.- Parameters:
index
- the index in the accessor (not the index in this collection).
-
valueAt
Returns the value at the given index. The givenindex
is relative to theaccessor
indexing, not to this collection.- Parameters:
index
- the index in the accessor (not the index in this collection).- Returns:
- the value at the given index. May be
null
or a collection.
-
isCollectionOrMap
final boolean isCollectionOrMap(int index) Returnstrue
if the type at the given index is a collection or a map. The givenindex
is relative to theaccessor
indexing, not to this collection.Implementation note: We do not test(value instanceof Collection)
because the value could be any user's implementation. Nothing prevent users from implementing the collection interface even for singleton elements if they wish.- Parameters:
index
- the index in the accessor (not the index in this collection).- Returns:
true
if the value at the given index is a collection.
-
isSkipped
Returnstrue
if the give value shall be skipped by the iterators, according the value policy.- Parameters:
value
- the value to test.- Returns:
true
if the given value shall be skipped by the iterators.
-
childAt
Returns the child at the given index, creating it if needed. The givenindex
is relative to theaccessor
indexing, not to this collection.This method does not check if the child at the given index should be skipped. It is caller responsibility to do such verification before this method call.
- Parameters:
index
- the index in the accessor (not the index in this collection).subIndex
- if the property atindex
is a collection, the index in that collection (not the index in this collection). Otherwise -1.- Returns:
- the node to be returned by public API.
-
childCount
final int childCount() -
size
public int size()Returns the number of elements in this collection, ignoring theskipped
ones.- Specified by:
size
in interfaceCollection<TreeTable.Node>
- Specified by:
size
in classAbstractCollection<TreeTable.Node>
-
isEmpty
public boolean isEmpty()Returnstrue
if this collection contains no elements. Invoking this method is more efficient than testingsize() == 0
because this method does not iterate over all properties.- Specified by:
isEmpty
in interfaceCollection<TreeTable.Node>
- Overrides:
isEmpty
in classAbstractCollection<TreeTable.Node>
-
clear
public void clear()Clears all properties in the metadata object. Note that this collection will effectively by empty after this method call only if the value existence policy isNON_EMPTY
, which is the default.- Specified by:
clear
in interfaceCollection<TreeTable.Node>
- Overrides:
clear
in classAbstractCollection<TreeTable.Node>
-
iterator
Returns an iterator over the nodes in the collection of children.- Specified by:
iterator
in interfaceCollection<TreeTable.Node>
- Specified by:
iterator
in interfaceIterable<TreeTable.Node>
- Specified by:
iterator
in classAbstractCollection<TreeTable.Node>
-
add
Adds the given node to this list. This method fetches the object fromTableColumn.VALUE
and assigns it to the property identified byTableColumn.IDENTIFIER
. All other columns are ignored.If the identified property is a collection, then this method adds the value to that collection. Otherwise the new value will be set only if the previous value is null, nil or empty.
This method does not iterate explicitly through the children list, because adding a metadata object implicitly adds all its children.
- Specified by:
add
in interfaceCollection<TreeTable.Node>
- Overrides:
add
in classAbstractCollection<TreeTable.Node>
- Parameters:
node
- the node from which to get the values.- Returns:
true
if the metadata changed as a result of this method call.- Throws:
NullPointerException
- if the given node is null.IllegalArgumentException
- if this list does not have a property for the node identifier.IllegalStateException
- if a value already exists and no more value can be added for the node identifier.UnmodifiableMetadataException
- if the property for the node identifier is read-only.ClassCastException
- if the node value cannot be converted to the expected type.BackingStoreException
- if the metadata implementation threw a checked exception.
-
add
Implementation ofadd(TreeTable.Node)
, also invoked byTreeNode.NewChild
. This method will attempt to convert the givenvalue
to the expected type.- Parameters:
index
- the index in the accessor (not the index in this collection).value
- the property value to add.- Returns:
true
if the metadata changed as a result of this method call.- Throws:
IllegalStateException
-
toString
Returns a string representation of this collection for debugging purpose. This string representation uses one line per element instead of formatting everything on a single line.- Overrides:
toString
in classAbstractCollection<TreeTable.Node>
-