All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.phloc.commons.tree.utils.walk.TreeWalker Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
/**
 * Copyright (C) 2006-2015 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phloc.commons.tree.utils.walk;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;

import com.phloc.commons.ValueEnforcer;
import com.phloc.commons.annotations.PresentForCodeCoverage;
import com.phloc.commons.hierarchy.DefaultHierarchyWalkerCallback;
import com.phloc.commons.hierarchy.IHierarchyWalkerCallback;
import com.phloc.commons.parent.IChildrenProvider;
import com.phloc.commons.parent.impl.ChildrenProviderHasChildren;
import com.phloc.commons.tree.IBasicTree;
import com.phloc.commons.tree.IBasicTreeItem;

/**
 * Iterate all nodes of a tree, or a tree element using a custom callback
 * mechanism.
 * 
 * @author Philip Helger
 */
@Immutable
public final class TreeWalker
{
  @PresentForCodeCoverage
  @SuppressWarnings ("unused")
  private static final TreeWalker s_aInstance = new TreeWalker ();

  private TreeWalker ()
  {}

  private static > void _walkTree (@Nonnull final ITEMTYPE aTreeItem,
                                                                                                  @Nonnull final IChildrenProvider  aChildrenProvider,
                                                                                                  @Nonnull final IHierarchyWalkerCallback  aCallback)
  {
    aCallback.onItemBeforeChildren (aTreeItem);
    if (aChildrenProvider.hasChildren (aTreeItem))
      for (final ITEMTYPE aChildItem : aChildrenProvider.getChildren (aTreeItem))
      {
        aCallback.onLevelDown ();
        // recursive call
        _walkTree (aChildItem, aChildrenProvider, aCallback);
        aCallback.onLevelUp ();
      }
    aCallback.onItemAfterChildren (aTreeItem);
  }

  public static > void walkTree (@Nonnull final IBasicTree  aTree,
                                                                                                @Nonnull final IHierarchyWalkerCallback  aCallback)
  {
    walkTree (aTree, new ChildrenProviderHasChildren  (), aCallback);
  }

  public static > void walkTree (@Nonnull final IBasicTree  aTree,
                                                                                                @Nonnull final IChildrenProvider  aChildrenProvider,
                                                                                                @Nonnull final IHierarchyWalkerCallback  aCallback)
  {
    ValueEnforcer.notNull (aTree, "Tree");

    walkSubTree (aTree.getRootItem (), aChildrenProvider, aCallback);
  }

  public static > void walkTreeData (@Nonnull final IBasicTree  aTree,
                                                                                                    @Nonnull final IHierarchyWalkerCallback  aDataCallback)
  {
    walkTreeData (aTree, new ChildrenProviderHasChildren  (), aDataCallback);
  }

  public static > void walkTreeData (@Nonnull final IBasicTree  aTree,
                                                                                                    @Nonnull final IChildrenProvider  aChildrenProvider,
                                                                                                    @Nonnull final IHierarchyWalkerCallback  aDataCallback)
  {
    ValueEnforcer.notNull (aTree, "Tree");

    walkSubTreeData (aTree.getRootItem (), aChildrenProvider, aDataCallback);
  }

  public static > void walkSubTree (@Nonnull final ITEMTYPE aTreeItem,
                                                                                                   @Nonnull final IHierarchyWalkerCallback  aCallback)
  {
    walkSubTree (aTreeItem, new ChildrenProviderHasChildren  (), aCallback);
  }

  public static > void walkSubTree (@Nonnull final ITEMTYPE aTreeItem,
                                                                                                   @Nonnull final IChildrenProvider  aChildrenProvider,
                                                                                                   @Nonnull final IHierarchyWalkerCallback  aCallback)
  {
    ValueEnforcer.notNull (aTreeItem, "TreeItem");
    ValueEnforcer.notNull (aChildrenProvider, "ChildrenProvider");
    ValueEnforcer.notNull (aCallback, "Callback");

    aCallback.begin ();
    try
    {
      if (aChildrenProvider.hasChildren (aTreeItem))
        for (final ITEMTYPE aChildItem : aChildrenProvider.getChildren (aTreeItem))
          _walkTree (aChildItem, aChildrenProvider, aCallback);
    }
    finally
    {
      aCallback.end ();
    }
  }

  public static > void walkSubTreeData (@Nonnull final ITEMTYPE aTreeItem,
                                                                                                       @Nonnull final IHierarchyWalkerCallback  aDataCallback)
  {
    walkSubTreeData (aTreeItem, new ChildrenProviderHasChildren  (), aDataCallback);
  }

  public static > void walkSubTreeData (@Nonnull final ITEMTYPE aTreeItem,
                                                                                                       @Nonnull final IChildrenProvider  aChildrenProvider,
                                                                                                       @Nonnull final IHierarchyWalkerCallback  aDataCallback)
  {
    ValueEnforcer.notNull (aDataCallback, "DataCallback");

    // Wrap callback
    walkSubTree (aTreeItem, aChildrenProvider, new DefaultHierarchyWalkerCallback  ()
    {
      @Override
      public void begin ()
      {
        aDataCallback.begin ();
      }

      @Override
      public void onLevelDown ()
      {
        super.onLevelDown ();
        aDataCallback.onLevelDown ();
      }

      @Override
      public void onLevelUp ()
      {
        super.onLevelUp ();
        aDataCallback.onLevelUp ();
      }

      @Override
      public void end ()
      {
        aDataCallback.end ();
      }

      @Override
      public void onItemBeforeChildren (@Nonnull final ITEMTYPE aItem)
      {
        aDataCallback.onItemBeforeChildren (aItem.getData ());
      }

      @Override
      public void onItemAfterChildren (@Nonnull final ITEMTYPE aItem)
      {
        aDataCallback.onItemAfterChildren (aItem.getData ());
      }
    });
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy