Package com.helger.tree.withid
Class BasicTreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>
- java.lang.Object
-
- com.helger.tree.withid.BasicTreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>
-
- Type Parameters:
KEYTYPE- tree item key typeDATATYPE- tree item value typeITEMTYPE- tree item implementation type
- All Implemented Interfaces:
com.helger.commons.hierarchy.IHasChildren<ITEMTYPE>,com.helger.commons.hierarchy.IHasChildrenRecursive<ITEMTYPE>,com.helger.commons.hierarchy.IHasChildrenSorted<ITEMTYPE>,com.helger.commons.hierarchy.IHasParent<ITEMTYPE>,com.helger.commons.id.IHasID<KEYTYPE>,com.helger.commons.traits.IGenericImplTrait<ITEMTYPE>,IBasicTreeItem<DATATYPE,ITEMTYPE>,ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>
- Direct Known Subclasses:
BasicFolderTreeItem,DefaultTreeItemWithID
@NotThreadSafe public class BasicTreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>> extends Object implements ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>
Basic tree item with ID implementation, independent of the implementation type.- Author:
- Philip Helger
-
-
Constructor Summary
Constructors Constructor Description BasicTreeItemWithID(ITreeItemWithIDFactory<KEYTYPE,DATATYPE,ITEMTYPE> aFactory)Constructor for root object with anulldata IDBasicTreeItemWithID(ITreeItemWithIDFactory<KEYTYPE,DATATYPE,ITEMTYPE> aFactory, KEYTYPE aDataID)Constructor for root objectBasicTreeItemWithID(ITEMTYPE aParent, KEYTYPE aDataID)Constructor for normal elements
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description com.helger.commons.state.ESuccesschangeParent(ITEMTYPE aNewParent)Change the parent node of this node to another node (subordination).booleancontainsChildItemWithDataID(KEYTYPE aDataID)Check if a direct child item with the given ID is presentITEMTYPEcreateChildItem(KEYTYPE aDataID, DATATYPE aData)Add a direct child item to this item.ITEMTYPEcreateChildItem(KEYTYPE aDataID, DATATYPE aData, boolean bAllowOverwrite)Add a direct child item to this item.booleanequals(Object o)ITEMTYPEfindFirstChild(Predicate<? super ITEMTYPE> aFilter)<DSTTYPE> DSTTYPEfindFirstChildMapped(Predicate<? super ITEMTYPE> aFilter, Function<? super ITEMTYPE,? extends DSTTYPE> aMapper)voidforAllChildren(Consumer<? super ITEMTYPE> aConsumer)voidforAllChildren(Predicate<? super ITEMTYPE> aFilter, Consumer<? super ITEMTYPE> aConsumer)<DSTTYPE> voidforAllChildrenMapped(Predicate<? super ITEMTYPE> aFilter, Function<? super ITEMTYPE,? extends DSTTYPE> aMapper, Consumer<? super DSTTYPE> aConsumer)com.helger.commons.collection.impl.ICommonsSet<KEYTYPE>getAllChildDataIDs()com.helger.commons.collection.impl.ICommonsList<DATATYPE>getAllChildDatas()Get the data values of all contained children.com.helger.commons.collection.impl.ICommonsList<ITEMTYPE>getAllChildren()ITEMTYPEgetChildAtIndex(int nIndex)intgetChildCount()ITEMTYPEgetChildItemOfDataID(KEYTYPE aDataID)Find the direct child item with the given IDcom.helger.commons.collection.impl.ICommonsIterable<ITEMTYPE>getChildren()DATATYPEgetData()ITreeItemWithIDFactory<KEYTYPE,DATATYPE,ITEMTYPE>getFactory()ITEMTYPEgetFirstChild()KEYTYPEgetID()ITEMTYPEgetLastChild()intgetLevel()ITEMTYPEgetParent()DATATYPEgetParentData()KEYTYPEgetParentID()booleanhasChildren()inthashCode()com.helger.commons.state.EChangeinternalAddChild(KEYTYPE aDataID, ITEMTYPE aChild, boolean bAllowOverwrite)Add an existing direct child to this tree item.booleanisRootItem()booleanisSameOrChildOf(ITEMTYPE aParent)Check if this item is the same or a child of the passed item.protected booleanisValidData(DATATYPE aData)This method is called to validate a data object.protected booleanisValidDataID(KEYTYPE aDataID)This method is called to validate a data ID object.com.helger.commons.state.EChangeremoveAllChildren()Remove all children from this node.com.helger.commons.state.EChangeremoveChild(KEYTYPE aDataID)Remove the passed node as a direct child node from this node.voidreorderChildrenByItems(Comparator<? super ITEMTYPE> aComparator)Reorder the child items based on the item itself.voidsetData(DATATYPE aData)Change the data associated with this node.StringtoString()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.helger.commons.hierarchy.IHasChildren
forAllChildrenBreakable, hasNoChildren
-
-
-
-
Constructor Detail
-
BasicTreeItemWithID
public BasicTreeItemWithID(@Nonnull ITreeItemWithIDFactory<KEYTYPE,DATATYPE,ITEMTYPE> aFactory)
Constructor for root object with anulldata ID- Parameters:
aFactory- The tree item factory to use. May not benull.
-
BasicTreeItemWithID
public BasicTreeItemWithID(@Nonnull ITreeItemWithIDFactory<KEYTYPE,DATATYPE,ITEMTYPE> aFactory, @Nullable KEYTYPE aDataID)
Constructor for root object- Parameters:
aFactory- The tree item factory to use. May not benull.aDataID- The data ID to use for the root item. May benull.
-
-
Method Detail
-
getFactory
@Nonnull public final ITreeItemWithIDFactory<KEYTYPE,DATATYPE,ITEMTYPE> getFactory()
- Specified by:
getFactoryin interfaceITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>- Returns:
- The factory used to create instances of this interface.
-
isValidDataID
@OverrideOnDemand protected boolean isValidDataID(KEYTYPE aDataID)
This method is called to validate a data ID object. This method may be overloaded in derived classes. The default implementation accepts all values.- Parameters:
aDataID- The value to validate.- Returns:
trueif the ID is valid,falseotherwise.
-
isValidData
@OverrideOnDemand protected boolean isValidData(DATATYPE aData)
This method is called to validate a data object. This method may be overloaded in derived classes. The default implementation accepts all values.- Parameters:
aData- The value to validate.- Returns:
trueif the ID is valid,falseotherwise.
-
isRootItem
public final boolean isRootItem()
- Specified by:
isRootItemin interfaceIBasicTreeItem<KEYTYPE,DATATYPE>- Returns:
trueif this is the internal root item without a parent,falseif this is a public item.
-
getParent
@Nullable public final ITEMTYPE getParent()
- Specified by:
getParentin interfacecom.helger.commons.hierarchy.IHasParent<KEYTYPE>
-
getParentID
@Nullable public final KEYTYPE getParentID()
- Specified by:
getParentIDin interfaceITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>- Returns:
- The data ID of the parent item. May be
nullif no parent is present or depending on the validator.
-
getParentData
@Nullable public final DATATYPE getParentData()
- Specified by:
getParentDatain interfaceIBasicTreeItem<KEYTYPE,DATATYPE>- Returns:
- the data associated with the parent node. May be
null. This is like a shortcut forgetParent().getData()with implicitnullhandling.
-
getLevel
@Nonnegative public final int getLevel()
- Specified by:
getLevelin interfaceIBasicTreeItem<KEYTYPE,DATATYPE>- Returns:
- The nesting level of this node. The root node has level 0. A child of the root item has level 1 etc.
-
getID
@Nullable public final KEYTYPE getID()
- Specified by:
getIDin interfacecom.helger.commons.id.IHasID<KEYTYPE>- Specified by:
getIDin interfaceITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>- Returns:
- The data ID of this item. May be
nulldepending on the validator.
-
getData
@Nullable public final DATATYPE getData()
- Specified by:
getDatain interfaceIBasicTreeItem<KEYTYPE,DATATYPE>- Returns:
- the data associated with this node. May be
null.
-
hasChildren
public final boolean hasChildren()
- Specified by:
hasChildrenin interfacecom.helger.commons.hierarchy.IHasChildren<KEYTYPE>
-
getChildCount
@Nonnegative public final int getChildCount()
- Specified by:
getChildCountin interfacecom.helger.commons.hierarchy.IHasChildren<KEYTYPE>
-
getAllChildren
@Nullable @ReturnsMutableCopy public final com.helger.commons.collection.impl.ICommonsList<ITEMTYPE> getAllChildren()
-
getChildren
@Nullable public final com.helger.commons.collection.impl.ICommonsIterable<ITEMTYPE> getChildren()
- Specified by:
getChildrenin interfacecom.helger.commons.hierarchy.IHasChildren<KEYTYPE>
-
forAllChildren
public final void forAllChildren(@Nonnull Consumer<? super ITEMTYPE> aConsumer)
- Specified by:
forAllChildrenin interfacecom.helger.commons.hierarchy.IHasChildren<KEYTYPE>
-
forAllChildren
public final void forAllChildren(@Nonnull Predicate<? super ITEMTYPE> aFilter, @Nonnull Consumer<? super ITEMTYPE> aConsumer)
- Specified by:
forAllChildrenin interfacecom.helger.commons.hierarchy.IHasChildren<KEYTYPE>
-
forAllChildrenMapped
public final <DSTTYPE> void forAllChildrenMapped(@Nonnull Predicate<? super ITEMTYPE> aFilter, @Nonnull Function<? super ITEMTYPE,? extends DSTTYPE> aMapper, @Nonnull Consumer<? super DSTTYPE> aConsumer)
- Specified by:
forAllChildrenMappedin interfacecom.helger.commons.hierarchy.IHasChildren<KEYTYPE>
-
getAllChildDataIDs
@Nullable @ReturnsMutableCopy public final com.helger.commons.collection.impl.ICommonsSet<KEYTYPE> getAllChildDataIDs()
- Specified by:
getAllChildDataIDsin interfaceITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>- Returns:
- A set with the data IDs of all children. If this tree item does not
have children,
nullif returned.
-
getAllChildDatas
@Nullable @ReturnsMutableCopy public final com.helger.commons.collection.impl.ICommonsList<DATATYPE> getAllChildDatas()
Description copied from interface:IBasicTreeItemGet the data values of all contained children.- Specified by:
getAllChildDatasin interfaceIBasicTreeItem<KEYTYPE,DATATYPE>- Returns:
nullif this item does not have children. UseIHasChildren.hasChildren()to check for the existence.
-
getChildAtIndex
@Nullable public final ITEMTYPE getChildAtIndex(@Nonnegative int nIndex)
- Specified by:
getChildAtIndexin interfacecom.helger.commons.hierarchy.IHasChildrenSorted<KEYTYPE>
-
getFirstChild
@Nullable public final ITEMTYPE getFirstChild()
- Specified by:
getFirstChildin interfacecom.helger.commons.hierarchy.IHasChildrenSorted<KEYTYPE>
-
findFirstChild
@Nullable public final ITEMTYPE findFirstChild(@Nonnull Predicate<? super ITEMTYPE> aFilter)
- Specified by:
findFirstChildin interfacecom.helger.commons.hierarchy.IHasChildrenSorted<KEYTYPE>
-
findFirstChildMapped
@Nullable public final <DSTTYPE> DSTTYPE findFirstChildMapped(@Nonnull Predicate<? super ITEMTYPE> aFilter, @Nonnull Function<? super ITEMTYPE,? extends DSTTYPE> aMapper)
- Specified by:
findFirstChildMappedin interfacecom.helger.commons.hierarchy.IHasChildrenSorted<KEYTYPE>
-
getLastChild
@Nullable public final ITEMTYPE getLastChild()
- Specified by:
getLastChildin interfacecom.helger.commons.hierarchy.IHasChildrenSorted<KEYTYPE>
-
setData
public final void setData(@Nullable DATATYPE aData)
Description copied from interface:IBasicTreeItemChange the data associated with this node.- Specified by:
setDatain interfaceIBasicTreeItem<KEYTYPE,DATATYPE>- Parameters:
aData- The data associated with this node. May benull.
-
createChildItem
@Nullable public final ITEMTYPE createChildItem(@Nullable KEYTYPE aDataID, @Nullable DATATYPE aData)
Description copied from interface:ITreeItemWithIDAdd a direct child item to this item. If another item with the same ID is already contained, the item is automatically overwritten.- Specified by:
createChildItemin interfaceITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>- Parameters:
aDataID- ID of the item to generate. May benullin rare circumstances.aData- The data associated with this item.- Returns:
- the created tree item. May be null in certain implementations
-
createChildItem
@Nullable public final ITEMTYPE createChildItem(@Nullable KEYTYPE aDataID, @Nullable DATATYPE aData, boolean bAllowOverwrite)
Description copied from interface:ITreeItemWithIDAdd a direct child item to this item.- Specified by:
createChildItemin interfaceITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>- Parameters:
aDataID- ID of the item to generate. May not benull.aData- The data associated with this item.bAllowOverwrite- Iftruea potential existing child item with the same ID is overwritten.- Returns:
- the created tree item or
nullif the data ID is already in use and bAllowOverwrite isfalse
-
containsChildItemWithDataID
public final boolean containsChildItemWithDataID(@Nullable KEYTYPE aDataID)
Description copied from interface:ITreeItemWithIDCheck if a direct child item with the given ID is present- Specified by:
containsChildItemWithDataIDin interfaceITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>- Parameters:
aDataID- The ID to search. May benull.- Returns:
trueif this item has a child with the given ID,falseotherwise
-
getChildItemOfDataID
@Nullable public final ITEMTYPE getChildItemOfDataID(@Nullable KEYTYPE aDataID)
Description copied from interface:ITreeItemWithIDFind the direct child item with the given ID- Specified by:
getChildItemOfDataIDin interfaceITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>- Parameters:
aDataID- The ID to search. May benull.- Returns:
nullif this item has no child with the given ID. The item otherwise.
-
isSameOrChildOf
public final boolean isSameOrChildOf(@Nonnull ITEMTYPE aParent)
Description copied from interface:IBasicTreeItemCheck if this item is the same or a child of the passed item. This is not limited to direct children but to children on all levels.- Specified by:
isSameOrChildOfin interfaceIBasicTreeItem<KEYTYPE,DATATYPE>- Parameters:
aParent- The parent item to check whether this is a child of it. May not benull.- Returns:
trueifthisis the same or a child of aParent.
-
changeParent
@Nonnull public final com.helger.commons.state.ESuccess changeParent(@Nonnull ITEMTYPE aNewParent)
Description copied from interface:IBasicTreeItemChange the parent node of this node to another node (subordination).- Specified by:
changeParentin interfaceIBasicTreeItem<KEYTYPE,DATATYPE>- Parameters:
aNewParent- The new parent to use. May not benull. To make it a root item, pass the owning tree's root item.- Returns:
ESuccess
-
internalAddChild
@Nonnull public final com.helger.commons.state.EChange internalAddChild(@Nonnull KEYTYPE aDataID, @Nonnull ITEMTYPE aChild, boolean bAllowOverwrite)
Description copied from interface:ITreeItemWithIDAdd an existing direct child to this tree item. Use only internally!- Specified by:
internalAddChildin interfaceITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>- Parameters:
aDataID- The data ID to use. May not benull.aChild- The child to be added. May not benull.bAllowOverwrite- iftrueexisting elements are overwritten.- Returns:
EChange
-
removeChild
@Nonnull public final com.helger.commons.state.EChange removeChild(@Nullable KEYTYPE aDataID)
Description copied from interface:ITreeItemWithIDRemove the passed node as a direct child node from this node.- Specified by:
removeChildin interfaceITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>- Parameters:
aDataID- The ID of the element to be removed. May not benull.- Returns:
EChange
-
removeAllChildren
@Nonnull public final com.helger.commons.state.EChange removeAllChildren()
Description copied from interface:ITreeItemWithIDRemove all children from this node.- Specified by:
removeAllChildrenin interfaceITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>- Returns:
EChange
-
reorderChildrenByItems
public final void reorderChildrenByItems(@Nonnull Comparator<? super ITEMTYPE> aComparator)
Description copied from interface:ITreeItemWithIDReorder the child items based on the item itself.- Specified by:
reorderChildrenByItemsin interfaceITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE extends ITreeItemWithID<KEYTYPE,DATATYPE,ITEMTYPE>>- Parameters:
aComparator- The comparator use. May not benull.
-
-