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

com.helger.tree.sort.TreeSorter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2014-2024 Philip Helger (www.helger.com)
 * philip[at]helger[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.helger.tree.sort;

import java.util.Comparator;

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

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.PresentForCodeCoverage;
import com.helger.commons.hierarchy.visit.ChildrenProviderHierarchyVisitor;
import com.helger.commons.hierarchy.visit.DefaultHierarchyVisitorCallback;
import com.helger.commons.hierarchy.visit.EHierarchyVisitorReturn;
import com.helger.tree.IBasicTree;
import com.helger.tree.IBasicTreeItem;
import com.helger.tree.ITreeItem;

/**
 * Sort {@link com.helger.tree.ITree} instances recursively.
 *
 * @author Philip Helger
 */
@Immutable
public final class TreeSorter
{
  @PresentForCodeCoverage
  private static final TreeSorter INSTANCE = new TreeSorter ();

  private TreeSorter ()
  {}

  private static > void _sort (@Nonnull final IBasicTree  aTree,
                                                                                         @Nonnull final Comparator  aComparator)
  {
    ValueEnforcer.notNull (aTree, "Tree");
    ValueEnforcer.notNull (aComparator, "Comparator");

    // and now start iterating (including the root item)
    ChildrenProviderHierarchyVisitor.visitFrom (aTree.getRootItem (), new DefaultHierarchyVisitorCallback  ()
    {
      @Override
      @Nonnull
      public EHierarchyVisitorReturn onItemBeforeChildren (@Nullable final ITEMTYPE aTreeItem)
      {
        if (aTreeItem != null)
          aTreeItem.reorderChildItems (aComparator);
        return EHierarchyVisitorReturn.CONTINUE;
      }
    }, true);
  }

  /**
   * Sort each level of the passed tree with the specified comparator.
   *
   * @param aTree
   *        The tree to be sorted.
   * @param aValueComparator
   *        The comparator to be used for sorting the tree items on each level.
   * @param 
   *        The tree item data type
   * @param 
   *        The tree item type
   */
  public static > void sort (@Nonnull final IBasicTree  aTree,
                                                                                       @Nonnull final Comparator  aValueComparator)
  {
    _sort (aTree, Comparator.comparing (IBasicTreeItem::getData, aValueComparator));
  }

  /**
   * Sort each level of the passed tree with the specified comparator. This
   * method assumes that the values in the tree item implement the
   * {@link Comparable} interface.
   *
   * @param aTree
   *        The tree to be sorted.
   * @param 
   *        The tree item data type
   * @param 
   *        The tree item type
   */
  public static , ITEMTYPE extends ITreeItem > void sort (@Nonnull final IBasicTree  aTree)
  {
    _sort (aTree, (o1, o2) -> o1.getData ().compareTo (o2.getData ()));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy