com.phloc.commons.tree.utils.search.TreeWithIDSearcher Maven / Gradle / Ivy
/**
* 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.search;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import com.phloc.commons.annotations.PresentForCodeCoverage;
import com.phloc.commons.annotations.ReturnsMutableCopy;
import com.phloc.commons.hierarchy.DefaultHierarchyWalkerCallback;
import com.phloc.commons.tree.IBasicTree;
import com.phloc.commons.tree.utils.walk.TreeWalker;
import com.phloc.commons.tree.withid.ITreeItemWithID;
/**
* A utility class that helps searching items within trees.
*
* @author Philip Helger
*/
@Immutable
public final class TreeWithIDSearcher
{
@PresentForCodeCoverage
@SuppressWarnings ("unused")
private static final TreeWithIDSearcher s_aInstance = new TreeWithIDSearcher ();
private TreeWithIDSearcher ()
{}
/**
* Fill all items with the same ID by linearly scanning of the tree.
*
* @param
* tree ID type
* @param
* tree data type
* @param
* tree item type
* @param aTree
* The tree to search. May not be null
.
* @param aSearchID
* The ID to search. May not be null
.
* @return A non-null
list with all matching items.
*/
@Nonnull
@ReturnsMutableCopy
public static > List findAllItemsWithIDRecursive (@Nonnull final IBasicTree aTree,
@Nullable final KEYTYPE aSearchID)
{
return findAllItemsWithIDRecursive (aTree.getRootItem (), aSearchID);
}
/**
* Fill all items with the same ID by linearly scanning the tree.
*
* @param
* tree ID type
* @param
* tree data type
* @param
* tree item type
* @param aTreeItem
* The tree item to search. May not be null
.
* @param aSearchID
* The ID to search. May not be null
.
* @return A non-null
list with all matching items.
*/
@Nonnull
@ReturnsMutableCopy
public static > List findAllItemsWithIDRecursive (@Nonnull final ITEMTYPE aTreeItem,
@Nullable final KEYTYPE aSearchID)
{
final List aRetList = new ArrayList ();
TreeWalker.walkSubTree (aTreeItem, new DefaultHierarchyWalkerCallback ()
{
@Override
public void onItemBeforeChildren (@Nullable final ITEMTYPE aItem)
{
if (aItem != null && aItem.getID ().equals (aSearchID))
aRetList.add (aItem);
}
});
return aRetList;
}
}