Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.gs.collections.impl.block.factory.Functions Maven / Gradle / Ivy
Go to download
GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map
implementations with a rich API and set of utility classes that work with any JDK compatible Collections,
Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.
/*
* Copyright 2015 Goldman Sachs.
*
* 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.gs.collections.impl.block.factory;
import java.util.Map;
import com.gs.collections.api.block.SerializableComparator;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.Function2;
import com.gs.collections.api.block.function.primitive.BooleanFunction;
import com.gs.collections.api.block.function.primitive.ByteFunction;
import com.gs.collections.api.block.function.primitive.CharFunction;
import com.gs.collections.api.block.function.primitive.DoubleFunction;
import com.gs.collections.api.block.function.primitive.FloatFunction;
import com.gs.collections.api.block.function.primitive.IntFunction;
import com.gs.collections.api.block.function.primitive.LongFunction;
import com.gs.collections.api.block.function.primitive.ShortFunction;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.block.procedure.Procedure2;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.block.comparator.primitive.BooleanFunctionComparator;
import com.gs.collections.impl.block.comparator.primitive.ByteFunctionComparator;
import com.gs.collections.impl.block.comparator.primitive.CharFunctionComparator;
import com.gs.collections.impl.block.comparator.primitive.DoubleFunctionComparator;
import com.gs.collections.impl.block.comparator.primitive.FloatFunctionComparator;
import com.gs.collections.impl.block.comparator.primitive.IntFunctionComparator;
import com.gs.collections.impl.block.comparator.primitive.LongFunctionComparator;
import com.gs.collections.impl.block.comparator.primitive.ShortFunctionComparator;
import com.gs.collections.impl.block.function.CaseFunction;
import com.gs.collections.impl.block.function.IfFunction;
import com.gs.collections.impl.block.function.checked.CheckedFunction;
import com.gs.collections.impl.block.function.checked.ThrowingFunction;
import com.gs.collections.impl.block.function.primitive.IntegerFunctionImpl;
import com.gs.collections.impl.tuple.Tuples;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.StringIterate;
public final class Functions
{
private static final Function DOUBLE_PASS_THRU_FUNCTION = new DoublePassThruFunction();
private static final Function INTEGER_PASS_THRU_FUNCTION = new IntegerPassThruFunction();
private static final Function LONG_PASS_THRU_FUNCTION = new LongPassThruFunction();
private static final Function TRUE_FUNCTION = new TrueFunction();
private static final Function FALSE_FUNCTION = new FalseFunction();
private static final Function, ?> PASS_THRU_FUNCTION = new PassThruFunction();
private static final Function STRING_TRIM_FUNCTION = new StringTrimFunction();
private static final Function> CLASS_FUNCTION = new ClassFunction();
private static final Function MATH_SIN_FUNCTION = new MathSinFunction();
private static final Function SQUARED_INTEGER = new SquaredIntegerFunction();
private static final Function TO_STRING_FUNCTION = new ToStringFunction();
private static final Function STRING_TO_INTEGER_FUNCTION = new StringToIntegerFunction();
private static final Function, ?> MAP_KEY_FUNCTION = new MapKeyFunction();
private static final Function, ?> MAP_VALUE_FUNCTION = new MapValueFunction();
private static final Function, Integer> SIZE_FUNCTION = new SizeFunction();
private static final FirstOfPairFunction> FIRST_OF_PAIR_FUNCTION = new FirstOfPairFunction();
private static final SecondOfPairFunction> SECOND_OF_PAIR_FUNCTION = new SecondOfPairFunction();
private static final CheckedFunction> CLASS_FOR_NAME = new ClassForNameFunction();
private static final SwappedPairFunction, ?> SWAPPED_PAIR_FUNCTION = new SwappedPairFunction();
private Functions()
{
throw new AssertionError("Suppress default constructor for noninstantiability");
}
private static class PassThruFunction implements Function
{
private static final long serialVersionUID = 1L;
public T valueOf(T anObject)
{
return anObject;
}
}
private static class StringTrimFunction implements Function
{
private static final long serialVersionUID = 1L;
public String valueOf(String s)
{
return s.trim();
}
}
private static final class FixedValueFunction implements Function
{
private static final long serialVersionUID = 1L;
private final V value;
private FixedValueFunction(V value)
{
this.value = value;
}
public V valueOf(T object)
{
return this.value;
}
}
private static final class ClassFunction implements Function>
{
private static final long serialVersionUID = 1L;
public Class> valueOf(Object anObject)
{
return anObject.getClass();
}
@Override
public String toString()
{
return "object.getClass()";
}
}
private static final class MathSinFunction implements Function
{
private static final long serialVersionUID = 1L;
public Double valueOf(Number number)
{
return Math.sin(number.doubleValue());
}
@Override
public String toString()
{
return "Math.sin()";
}
}
private static final class SquaredIntegerFunction implements Function
{
private static final long serialVersionUID = 1L;
public Integer valueOf(Integer value)
{
return value * value;
}
}
private static final class ToStringFunction implements Function
{
private static final long serialVersionUID = 1L;
public String valueOf(Object anObject)
{
return String.valueOf(anObject);
}
@Override
public String toString()
{
return "toString";
}
}
private static final class StringToIntegerFunction implements Function
{
private static final long serialVersionUID = 1L;
public Integer valueOf(String string)
{
return Integer.valueOf(string);
}
@Override
public String toString()
{
return "stringToInteger";
}
}
public static Function throwing(ThrowingFunction throwingFunction)
{
return new ThrowingFunctionAdapter(throwingFunction);
}
/**
* Allows a Java 8 lambda and method reference to be used in a method taking a Function as a parameter
* without any ambiguity.
*/
public static Function cast(Function function)
{
return function;
}
/**
* Alias for identity(). Inlineable.
*
* @see #identity()
*/
public static Function getPassThru()
{
return Functions.identity();
}
/**
* @since 6.0
*/
public static Function identity()
{
return (Function) PASS_THRU_FUNCTION;
}
/**
* @since 6.0
*/
public static Function getTrue()
{
return TRUE_FUNCTION;
}
/**
* @since 6.0
*/
public static Function getFalse()
{
return FALSE_FUNCTION;
}
public static Function getFixedValue(V value)
{
return new FixedValueFunction(value);
}
public static Function> getToClass()
{
return CLASS_FUNCTION;
}
public static Function getMathSinFunction()
{
return MATH_SIN_FUNCTION;
}
public static Function getNumberPassThru()
{
return (Function) PASS_THRU_FUNCTION;
}
public static Function getIntegerPassThru()
{
return INTEGER_PASS_THRU_FUNCTION;
}
public static Function getLongPassThru()
{
return LONG_PASS_THRU_FUNCTION;
}
public static Function getDoublePassThru()
{
return DOUBLE_PASS_THRU_FUNCTION;
}
public static Function getStringPassThru()
{
return (Function) PASS_THRU_FUNCTION;
}
public static Function getStringTrim()
{
return STRING_TRIM_FUNCTION;
}
public static Function getToString()
{
return TO_STRING_FUNCTION;
}
public static Function getNullSafeToString(String defaultValue)
{
return Functions.nullSafe(TO_STRING_FUNCTION, defaultValue);
}
public static SerializableComparator toBooleanComparator(BooleanFunction function)
{
return new BooleanFunctionComparator(function);
}
public static SerializableComparator toByteComparator(ByteFunction function)
{
return new ByteFunctionComparator(function);
}
public static SerializableComparator toCharComparator(CharFunction function)
{
return new CharFunctionComparator(function);
}
public static SerializableComparator toFloatComparator(FloatFunction function)
{
return new FloatFunctionComparator(function);
}
public static SerializableComparator toShortComparator(ShortFunction function)
{
return new ShortFunctionComparator(function);
}
public static SerializableComparator toIntComparator(IntFunction function)
{
return new IntFunctionComparator(function);
}
public static SerializableComparator toDoubleComparator(DoubleFunction function)
{
return new DoubleFunctionComparator(function);
}
public static SerializableComparator toLongComparator(LongFunction function)
{
return new LongFunctionComparator(function);
}
public static Function getStringToInteger()
{
return STRING_TO_INTEGER_FUNCTION;
}
public static Function withDefault(Function super T, ? extends V> function, V defaultValue)
{
return new DefaultFunction(function, defaultValue);
}
public static Function nullSafe(Function super T, ? extends V> function)
{
return new NullSafeFunction(function, null);
}
public static Function nullSafe(Function super T, ? extends V> function, V nullValue)
{
return new NullSafeFunction(function, nullValue);
}
public static Function, V1> firstOfPair()
{
return (Function, V1>) (Function, ?>) FIRST_OF_PAIR_FUNCTION;
}
public static Function, V2> secondOfPair()
{
return (Function, V2>) (Function, ?>) SECOND_OF_PAIR_FUNCTION;
}
/**
* Swap the input pair and return the swapped pair.
*
* @return A function that gets the swapped pair {@code Iterable}
*/
public static Function, Pair> swappedPair()
{
return (Function, Pair>) (Function, ?>) SWAPPED_PAIR_FUNCTION;
}
/**
* Bind the parameter passed to a Function2 into a new Function.
*
* @param function The Function2 to delegate the invocation to.
* @param parameter The parameter the use in the invocation of the delegate function.
* @return A new Function
*/
public static Function bind(Function2 super T, ? super P, ? extends R> function, P parameter)
{
return new BindFunction2(function, parameter);
}
/**
* Bind the input of a Procedure to the result of an function, returning a new Procedure.
*
* @param delegate The Procedure to delegate the invocation to.
* @param function The Function that will create the input for the delegate
* @return A new Procedure
*/
public static Procedure bind(
Procedure super T2> delegate,
Function super T1, T2> function)
{
return new BindProcedure(delegate, function);
}
/**
* Bind the input of a ObjectIntProcedure to the result of an function, returning a new ObjectIntProcedure.
*
* @param delegate The ObjectIntProcedure to delegate the invocation to.
* @param function The Function that will create the input for the delegate
* @return A new ObjectIntProcedure
*/
public static ObjectIntProcedure bind(
ObjectIntProcedure super T2> delegate,
Function super T1, T2> function)
{
return new BindObjectIntProcedure(delegate, function);
}
/**
* Bind the input of the first argument of a Procedure2 to the result of an function, returning a new Procedure2.
*
* @param delegate The Procedure2 to delegate the invocation to.
* @param function The Function that will create the input for the delegate
* @return A new Procedure2
*/
public static Procedure2 bind(
Procedure2 super T2, T3> delegate, Function super T1, T2> function)
{
return new BindProcedure2(delegate, function);
}
public static Function squaredInteger()
{
return SQUARED_INTEGER;
}
public static Function firstNotNullValue(Function... functions)
{
return new FirstNotNullFunction(functions);
}
public static Function firstNotEmptyStringValue(
Function... functions)
{
return new FirstNotEmptyStringFunction(functions);
}
public static > Function firstNotEmptyCollectionValue(
Function... functions)
{
return new FirstNotEmptyCollectionFunction(functions);
}
public static Function ifTrue(
Predicate super T> predicate,
Function super T, ? extends V> function)
{
return new IfFunction(predicate, function);
}
public static Function ifElse(
Predicate super T> predicate,
Function super T, ? extends V> trueFunction,
Function super T, ? extends V> falseFunction)
{
return new IfFunction(predicate, trueFunction, falseFunction);
}
public static , V> CaseFunction caseDefault(
Function super T, ? extends V> defaultFunction)
{
return new CaseFunction(defaultFunction);
}
public static , V> CaseFunction caseDefault(
Function super T, ? extends V> defaultFunction,
Predicate super T> predicate,
Function super T, ? extends V> function)
{
CaseFunction caseFunction = Functions.caseDefault(defaultFunction);
return caseFunction.addCase(predicate, function);
}
public static Function synchronizedEach(Function function)
{
return new SynchronizedFunction(function);
}
public static Function> classForName()
{
return CLASS_FOR_NAME;
}
private static final class FirstNotNullFunction implements Function
{
private static final long serialVersionUID = 1L;
private final Function[] functions;
private FirstNotNullFunction(Function... functions)
{
this.functions = functions;
}
public V valueOf(T object)
{
for (Function function : this.functions)
{
V result = function.valueOf(object);
if (result != null)
{
return result;
}
}
return null;
}
}
private static final class FirstNotEmptyStringFunction implements Function
{
private static final long serialVersionUID = 1L;
private final Function[] functions;
private FirstNotEmptyStringFunction(Function... functions)
{
this.functions = functions;
}
public String valueOf(T object)
{
for (Function function : this.functions)
{
String result = function.valueOf(object);
if (StringIterate.notEmpty(result))
{
return result;
}
}
return null;
}
}
private static final class FirstNotEmptyCollectionFunction> implements Function
{
private static final long serialVersionUID = 1L;
private final Function[] functions;
private FirstNotEmptyCollectionFunction(Function[] functions)
{
this.functions = functions;
}
public I valueOf(T1 object)
{
for (Function function : this.functions)
{
I result = function.valueOf(object);
if (Iterate.notEmpty(result))
{
return result;
}
}
return null;
}
}
private static final class SynchronizedFunction implements Function
{
private static final long serialVersionUID = 1L;
private final Function function;
private SynchronizedFunction(Function function)
{
this.function = function;
}
public V valueOf(T each)
{
synchronized (each)
{
return this.function.valueOf(each);
}
}
}
public static FunctionChain chain(Function function1, Function super T2, T3> function2)
{
return new FunctionChain(function1, function2);
}
public static BooleanFunctionChain chainBoolean(Function function1, BooleanFunction super T2> function2)
{
return new BooleanFunctionChain(function1, function2);
}
public static ByteFunctionChain chainByte(Function function1, ByteFunction super T2> function2)
{
return new ByteFunctionChain(function1, function2);
}
public static CharFunctionChain chainChar(Function function1, CharFunction super T2> function2)
{
return new CharFunctionChain(function1, function2);
}
public static DoubleFunctionChain chainDouble(Function function1, DoubleFunction super T2> function2)
{
return new DoubleFunctionChain(function1, function2);
}
public static FloatFunctionChain chainFloat(Function function1, FloatFunction super T2> function2)
{
return new FloatFunctionChain(function1, function2);
}
public static IntFunctionChain chainInt(Function function1, IntFunction super T2> function2)
{
return new IntFunctionChain(function1, function2);
}
public static LongFunctionChain chainLong(Function function1, LongFunction super T2> function2)
{
return new LongFunctionChain(function1, function2);
}
public static ShortFunctionChain chainShort(Function function1, ShortFunction super T2> function2)
{
return new ShortFunctionChain(function1, function2);
}
private static class DoublePassThruFunction implements Function, DoubleFunction
{
private static final long serialVersionUID = 1L;
public double doubleValueOf(Double each)
{
return each.doubleValue();
}
public Double valueOf(Double each)
{
return each;
}
@Override
public String toString()
{
return DoublePassThruFunction.class.getSimpleName();
}
}
private static class IntegerPassThruFunction implements Function, IntFunction
{
private static final long serialVersionUID = 1L;
public int intValueOf(Integer each)
{
return each.intValue();
}
public Integer valueOf(Integer each)
{
return each;
}
@Override
public String toString()
{
return IntegerPassThruFunction.class.getSimpleName();
}
}
private static class LongPassThruFunction implements Function, LongFunction
{
private static final long serialVersionUID = 1L;
public long longValueOf(Long each)
{
return each.longValue();
}
public Long valueOf(Long each)
{
return each;
}
@Override
public String toString()
{
return LongPassThruFunction.class.getSimpleName();
}
}
private static final class DefaultFunction implements Function
{
private static final long serialVersionUID = 1L;
private final Function super T, ? extends V> function;
private final V defaultValue;
private DefaultFunction(Function super T, ? extends V> newFunction, V newDefaultValue)
{
this.function = newFunction;
this.defaultValue = newDefaultValue;
}
public V valueOf(T anObject)
{
V returnValue = this.function.valueOf(anObject);
if (returnValue == null)
{
return this.defaultValue;
}
return returnValue;
}
}
private static final class NullSafeFunction implements Function
{
private static final long serialVersionUID = 1L;
private final Function super T, ? extends V> function;
private final V nullValue;
private NullSafeFunction(Function super T, ? extends V> function, V nullValue)
{
this.function = function;
this.nullValue = nullValue;
}
public V valueOf(T object)
{
return object == null ? this.nullValue : this.function.valueOf(object);
}
}
public static Function> pair(
final Function super T, V1> function1,
final Function super T, V2> function2)
{
return new Function>()
{
public Pair valueOf(T t)
{
return Tuples.pair(function1.valueOf(t), function2.valueOf(t));
}
};
}
/**
* @return A function that gets the key out of a {@link java.util.Map.Entry}
*/
@SuppressWarnings("UnnecessaryFullyQualifiedName")
public static Function, K> getKeyFunction()
{
return (Function, K>) MAP_KEY_FUNCTION;
}
/**
* @return A function that gets the value out of a {@link java.util.Map.Entry}
*/
@SuppressWarnings("UnnecessaryFullyQualifiedName")
public static Function, V> getValueFunction()
{
return (Function, V>) MAP_VALUE_FUNCTION;
}
private static class MapKeyFunction implements Function, K>
{
private static final long serialVersionUID = 1L;
public K valueOf(Map.Entry entry)
{
return entry.getKey();
}
}
private static class MapValueFunction implements Function, V>
{
private static final long serialVersionUID = 1L;
public V valueOf(Map.Entry, V> entry)
{
return entry.getValue();
}
}
/**
* @return A function that gets the size of an {@code Iterable}
*/
public static Function, Integer> getSizeOf()
{
return SIZE_FUNCTION;
}
public static class SizeFunction extends IntegerFunctionImpl>
{
private static final long serialVersionUID = 1L;
public int intValueOf(Iterable> iterable)
{
return Iterate.sizeOf(iterable);
}
}
public static final class FunctionChain implements Function
{
private static final long serialVersionUID = 1L;
private final Function function1;
private final Function super T2, T3> function2;
private FunctionChain(Function function1, Function super T2, T3> function2)
{
this.function1 = function1;
this.function2 = function2;
}
public T3 valueOf(T1 object)
{
return this.function2.valueOf(this.function1.valueOf(object));
}
public FunctionChain chain(Function super T3, T4> function)
{
return new FunctionChain(this, function);
}
public BooleanFunctionChain chainBoolean(BooleanFunction super T3> function)
{
return new BooleanFunctionChain(this, function);
}
public ByteFunctionChain chainByte(ByteFunction super T3> function)
{
return new ByteFunctionChain(this, function);
}
public CharFunctionChain chainChar(CharFunction super T3> function)
{
return new CharFunctionChain(this, function);
}
public DoubleFunctionChain chainDouble(DoubleFunction super T3> function)
{
return new DoubleFunctionChain(this, function);
}
public FloatFunctionChain