com.helger.commons.collection.StackHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ph-commons Show documentation
Show all versions of ph-commons Show documentation
Java 1.8+ Library with tons of utility classes required in all projects
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.commons.collection;
import java.util.Collection;
import java.util.function.Function;
import java.util.function.Predicate;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import com.helger.commons.annotation.ReturnsMutableCopy;
@Immutable
public final class StackHelper
{
private StackHelper ()
{}
@Nonnull
@ReturnsMutableCopy
public static NonBlockingStack newStack (@Nonnegative final int nInitialCapacity)
{
return new NonBlockingStack <> (nInitialCapacity);
}
@Nonnull
@ReturnsMutableCopy
public static NonBlockingStack newStack ()
{
return new NonBlockingStack <> ();
}
@Nonnull
@ReturnsMutableCopy
public static NonBlockingStack newStackMapped (@Nullable final Collection extends SRCTYPE> aCollection,
@Nonnull final Function super SRCTYPE, DSTTYPE> aMapper)
{
if (CollectionHelper.isEmpty (aCollection))
return newStack (0);
final NonBlockingStack ret = newStack (aCollection.size ());
for (final SRCTYPE aValue : aCollection)
ret.push (aMapper.apply (aValue));
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static NonBlockingStack newStackMapped (@Nullable final SRCTYPE [] aArray,
@Nonnull final Function super SRCTYPE, DSTTYPE> aMapper)
{
if (ArrayHelper.isEmpty (aArray))
return newStack (0);
final NonBlockingStack ret = newStack (aArray.length);
for (final SRCTYPE aValue : aArray)
ret.push (aMapper.apply (aValue));
return ret;
}
/**
* Create a new stack with a single element.
*
* @param
* The type of elements contained in the stack.
* @param aValue
* The value to push. Maybe null
.
* @return A non-null
stack.
*/
@Nonnull
@ReturnsMutableCopy
public static NonBlockingStack newStack (@Nullable final ELEMENTTYPE aValue)
{
final NonBlockingStack ret = newStack ();
ret.push (aValue);
return ret;
}
/**
* Create a new stack from the given array.
*
* @param
* The type of elements contained in the stack.
* @param aValues
* The values that are to be pushed on the stack. The last element will
* be the top element on the stack. May not be null
.
* @return A non-null
stack object.
*/
@Nonnull
@ReturnsMutableCopy
@SafeVarargs
public static NonBlockingStack newStack (@Nullable final ELEMENTTYPE... aValues)
{
return new NonBlockingStack <> (aValues);
}
@Nonnull
@ReturnsMutableCopy
public static NonBlockingStack newStack (@Nullable final Collection extends ELEMENTTYPE> aCollection,
@Nonnull final Predicate super ELEMENTTYPE> aFilter)
{
if (CollectionHelper.isEmpty (aCollection))
return newStack (0);
final NonBlockingStack ret = newStack (aCollection.size ());
CollectionHelper.findAll (aCollection, aFilter, ret::add);
return ret;
}
/**
* Create a new stack from the given collection.
*
* @param
* The type of elements contained in the stack.
* @param aValues
* The values that are to be pushed on the stack. The last element will
* be the top element on the stack. May not be null
.
* @return A non-null
stack object.
*/
@Nonnull
@ReturnsMutableCopy
public static NonBlockingStack newStack (@Nullable final Collection extends ELEMENTTYPE> aValues)
{
return new NonBlockingStack <> (aValues);
}
@Nullable
@ReturnsMutableCopy
public static NonBlockingStack getStackCopyWithoutTop (@Nullable final NonBlockingStack aStack)
{
if (CollectionHelper.isEmpty (aStack))
return null;
final NonBlockingStack ret = new NonBlockingStack <> (aStack);
ret.pop ();
return ret;
}
}