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

com.phloc.commons.microdom.utils.MicroWalker 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.microdom.utils;

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.IHierarchyWalkerCallback;
import com.phloc.commons.microdom.IMicroNode;
import com.phloc.commons.parent.IChildrenProvider;
import com.phloc.commons.parent.impl.ChildrenProviderHasChildren;

/**
 * Helper class that walks an {@link com.phloc.commons.microdom.IMicroDocument}
 * or {@link com.phloc.commons.microdom.IMicroNode} with a callback.
 * 
 * @author Philip Helger
 */
@Immutable
public final class MicroWalker
{
  @PresentForCodeCoverage
  @SuppressWarnings ("unused")
  private static final MicroWalker s_aInstance = new MicroWalker ();

  private MicroWalker ()
  {}

  private static  void _walkNode (@Nonnull final T aNode,
                                                        @Nonnull final IChildrenProvider  aChildrenResolver,
                                                        @Nonnull final IHierarchyWalkerCallback  aCallback)
  {
    aCallback.onItemBeforeChildren (aNode);
    if (aChildrenResolver.hasChildren (aNode))
      for (final T aChildItem : aChildrenResolver.getChildren (aNode))
      {
        aCallback.onLevelDown ();
        // recursive call
        _walkNode (aChildItem, aChildrenResolver, aCallback);
        aCallback.onLevelUp ();
      }
    aCallback.onItemAfterChildren (aNode);
  }

  /**
   * Iterate the passed node and invoke the callback for all child nodes. The
   * callback is not invoked for the passed node itself!
   * 
   * @param aNode
   *        The node to iterate. May not be null.
   * @param aCallback
   *        The callback to call. May not be null.
   */
  public static void walkNode (@Nonnull final IMicroNode aNode,
                               @Nonnull final IHierarchyWalkerCallback  aCallback)
  {
    walkNode (aNode, new ChildrenProviderHasChildren  (), aCallback);
  }

  /**
   * Iterate the passed node and invoke the callback for all child nodes. The
   * callback is not invoked for the passed node itself!
   * 
   * @param aNode
   *        The node to iterate. May not be null.
   * @param aChildrenResolver
   *        The child resolver to use. May not be null.
   * @param aCallback
   *        The callback to call. May not be null.
   */
  public static  void walkNode (@Nonnull final T aNode,
                                                      @Nonnull final IChildrenProvider  aChildrenResolver,
                                                      @Nonnull final IHierarchyWalkerCallback  aCallback)
  {
    ValueEnforcer.notNull (aNode, "Node");
    ValueEnforcer.notNull (aCallback, "Callback");

    aCallback.begin ();
    try
    {
      if (aChildrenResolver.hasChildren (aNode))
        for (final T aChildItem : aChildrenResolver.getChildren (aNode))
          _walkNode (aChildItem, aChildrenResolver, aCallback);
    }
    finally
    {
      aCallback.end ();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy