Java Code Examples for com.helger.commons.hierarchy.visit.EHierarchyVisitorReturn

The following examples show how to use com.helger.commons.hierarchy.visit.EHierarchyVisitorReturn. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ph-commons   Source File: TreeWithIDSorter.java    License: Apache License 2.0 6 votes vote down vote up
private static <KEYTYPE, DATATYPE, ITEMTYPE extends ITreeItemWithID <KEYTYPE, DATATYPE, ITEMTYPE>> void _sort (@Nonnull final IBasicTree <DATATYPE, ITEMTYPE> aTree,
                                                                                                               @Nonnull final Comparator <? super ITEMTYPE> aComparator)
{
  ValueEnforcer.notNull (aTree, "Tree");
  ValueEnforcer.notNull (aComparator, "Comparator");

  // sort root manually
  aTree.getRootItem ().reorderChildrenByItems (aComparator);

  // and now start iterating
  TreeVisitor.visitTree (aTree, new DefaultHierarchyVisitorCallback <ITEMTYPE> ()
  {
    @Override
    public EHierarchyVisitorReturn onItemBeforeChildren (@Nullable final ITEMTYPE aTreeItem)
    {
      if (aTreeItem != null)
        aTreeItem.reorderChildrenByItems (aComparator);
      return EHierarchyVisitorReturn.CONTINUE;
    }
  });
}
 
Example 2
Source Project: ph-commons   Source File: TreeSorter.java    License: Apache License 2.0 6 votes vote down vote up
private static <DATATYPE, ITEMTYPE extends ITreeItem <DATATYPE, ITEMTYPE>> void _sort (@Nonnull final IBasicTree <? extends DATATYPE, ITEMTYPE> aTree,
                                                                                       @Nonnull final Comparator <? super ITEMTYPE> aComparator)
{
  ValueEnforcer.notNull (aTree, "Tree");
  ValueEnforcer.notNull (aComparator, "Comparator");

  // and now start iterating (including the root item)
  ChildrenProviderHierarchyVisitor.visitFrom (aTree.getRootItem (), new DefaultHierarchyVisitorCallback <ITEMTYPE> ()
  {
    @Override
    @Nonnull
    public EHierarchyVisitorReturn onItemBeforeChildren (@Nullable final ITEMTYPE aTreeItem)
    {
      if (aTreeItem != null)
        aTreeItem.reorderChildItems (aComparator);
      return EHierarchyVisitorReturn.CONTINUE;
    }
  }, true);
}
 
Example 3
Source Project: ph-commons   Source File: TreeWithIDSearcher.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Fill all items with the same ID by linearly scanning the tree.
 *
 * @param <KEYTYPE>
 *        tree ID type
 * @param <DATATYPE>
 *        tree data type
 * @param <ITEMTYPE>
 *        tree item type
 * @param aTreeItem
 *        The tree item to search. May not be <code>null</code>.
 * @param aSearchID
 *        The ID to search. May not be <code>null</code>.
 * @return A non-<code>null</code> list with all matching items.
 */
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, DATATYPE, ITEMTYPE extends ITreeItemWithID <KEYTYPE, DATATYPE, ITEMTYPE>> ICommonsList <ITEMTYPE> findAllItemsWithIDRecursive (@Nonnull final ITEMTYPE aTreeItem,
                                                                                                                                                       @Nullable final KEYTYPE aSearchID)
{
  final ICommonsList <ITEMTYPE> aRetList = new CommonsArrayList <> ();
  TreeVisitor.visitTreeItem (aTreeItem, new DefaultHierarchyVisitorCallback <ITEMTYPE> ()
  {
    @Override
    @Nonnull
    public EHierarchyVisitorReturn onItemBeforeChildren (@Nullable final ITEMTYPE aItem)
    {
      if (aItem != null && aItem.getID ().equals (aSearchID))
        aRetList.add (aItem);
      return EHierarchyVisitorReturn.CONTINUE;
    }
  });
  return aRetList;
}
 
Example 4
Source Project: ph-commons   Source File: TreeXMLConverter.java    License: Apache License 2.0 5 votes vote down vote up
public static <KEYTYPE, DATATYPE, ITEMTYPE extends ITreeItemWithID <KEYTYPE, DATATYPE, ITEMTYPE>> void fillXMLAsTreeWithID (@Nonnull final IMicroElement aElement,
                                                                                                                            @Nonnull final Function <? super String, ? extends KEYTYPE> aIDConverter,
                                                                                                                            @Nonnull final IConverterMicroNodeToTreeItem <? extends DATATYPE> aDataConverter,
                                                                                                                            @Nonnull final BasicTreeWithID <KEYTYPE, DATATYPE, ITEMTYPE> aTree)
{
  final String sNamespaceURI = aDataConverter.getNamespaceURI ();
  final NonBlockingStack <ITEMTYPE> aParents = new NonBlockingStack <> ();
  aParents.push (aTree.getRootItem ());
  MicroVisitor.visit (aElement,
                      new ChildrenProviderElementWithName (sNamespaceURI, ELEMENT_ITEM),
                      new DefaultHierarchyVisitorCallback <IMicroElement> ()
                      {
                        @Override
                        @Nonnull
                        public EHierarchyVisitorReturn onItemBeforeChildren (@Nullable final IMicroElement eItem)
                        {
                          if (eItem != null)
                          {
                            final KEYTYPE aTreeItemID = aIDConverter.apply (eItem.getAttributeValue (ATTR_ID));

                            final IMicroElement eData = eItem.getFirstChildElement (sNamespaceURI, ELEMENT_DATA);
                            final DATATYPE aTreeItemValue = aDataConverter.getAsDataValue (eData);

                            final ITEMTYPE aTreeItem = aParents.peek ().createChildItem (aTreeItemID, aTreeItemValue);
                            aParents.push (aTreeItem);
                          }
                          return EHierarchyVisitorReturn.CONTINUE;
                        }

                        @Override
                        @Nonnull
                        public EHierarchyVisitorReturn onItemAfterChildren (@Nullable final IMicroElement aItem)
                        {
                          if (aItem != null)
                            aParents.pop ();
                          return EHierarchyVisitorReturn.CONTINUE;
                        }
                      });
}
 
Example 5
Source Project: ph-commons   Source File: TreeVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public EHierarchyVisitorReturn onItemBeforeChildren (@Nonnull final ITEMTYPE aItem)
{
  final DATATYPE aConvertedValue = m_aConverter.apply (aItem);
  return m_aDataCallback.onItemBeforeChildren (aConvertedValue);
}
 
Example 6
Source Project: ph-commons   Source File: TreeVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public EHierarchyVisitorReturn onItemAfterChildren (@Nonnull final ITEMTYPE aItem)
{
  final DATATYPE aConvertedValue = m_aConverter.apply (aItem);
  return m_aDataCallback.onItemAfterChildren (aConvertedValue);
}
 
Example 7
Source Project: ph-commons   Source File: MockTreeVisitorCallback.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public EHierarchyVisitorReturn onItemBeforeChildren (final DefaultTreeItemWithID <String, Object> aItem)
{
  if (getLevel () < 0)
    throw new IllegalStateException ();
  m_aMI.inc ();
  return EHierarchyVisitorReturn.CONTINUE;
}