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

org.eclipse.collections.impl.block.factory.Functions Maven / Gradle / Ivy

Go to download

Builds the commons-text. Requires eclipse-collections-api be built first and be excluded from any other poms requiring it.

There is a newer version: 11.1.0-r13
Show newest version
/*
 * Copyright (c) 2022 Goldman Sachs and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

package org.eclipse.collections.impl.block.factory;

import java.util.Map;

import org.eclipse.collections.api.block.SerializableComparator;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.comparator.primitive.BooleanFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.ByteFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.CharFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.DoubleFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.FloatFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.IntFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.LongFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.ShortFunctionComparator;
import org.eclipse.collections.impl.block.function.CaseFunction;
import org.eclipse.collections.impl.block.function.IfFunction;
import org.eclipse.collections.impl.block.function.checked.CheckedFunction;
import org.eclipse.collections.impl.block.function.checked.ThrowingFunction;
import org.eclipse.collections.impl.block.function.primitive.IntegerFunctionImpl;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.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;

        @Override
        public T valueOf(T anObject)
        {
            return anObject;
        }
    }

    private static class StringTrimFunction implements Function
    {
        private static final long serialVersionUID = 1L;

        @Override
        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;
        }

        @Override
        public V valueOf(T object)
        {
            return this.value;
        }
    }

    private static final class ClassFunction implements Function>
    {
        private static final long serialVersionUID = 1L;

        @Override
        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;

        @Override
        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;

        @Override
        public Integer valueOf(Integer value)
        {
            return value * value;
        }
    }

    private static final class ToStringFunction implements Function
    {
        private static final long serialVersionUID = 1L;

        @Override
        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;

        @Override
        public Integer valueOf(String string)
        {
            return Integer.valueOf(string);
        }

        @Override
        public String toString()
        {
            return "stringToInteger";
        }
    }

    /**
     * Allows a lambda or anonymous inner class that needs to throw a checked exception to be safely wrapped as a
     * Function that will throw a RuntimeException, wrapping the checked exception that is the cause.
     */
    public static  Function throwing(ThrowingFunction throwingFunction)
    {
        return new ThrowingFunctionAdapter<>(throwingFunction);
    }

    /**
     * Allows a lambda or anonymous inner class that needs to throw a checked exception to be safely wrapped as a
     * Function that will throw a user specified RuntimeException based on the provided function. The function
     * is passed the current element and the checked exception that was thrown as context arguments.
     */
    public static  Function throwing(
            ThrowingFunction throwingFunction,
            Function2 rethrow)
    {
        return each ->
        {
            try
            {
                return throwingFunction.safeValueOf(each);
            }
            catch (RuntimeException e)
            {
                throw e;
            }
            catch (Throwable t)
            {
                throw rethrow.value(each, t);
            }
        };
    }

    /**
     * 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 function, V defaultValue)
    {
        return new DefaultFunction<>(function, defaultValue);
    }

    public static  Function nullSafe(Function function)
    {
        return new NullSafeFunction<>(function, null);
    }

    public static  Function nullSafe(Function 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 function, P parameter)
    {
        return new BindFunction2<>(function, parameter);
    }

    /**
     * Bind the input of a Procedure to the result of a 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 delegate,
            Function function)
    {
        return new BindProcedure<>(delegate, function);
    }

    /**
     * Bind the input of a ObjectIntProcedure to the result of a 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 delegate,
            Function function)
    {
        return new BindObjectIntProcedure<>(delegate, function);
    }

    /**
     * Bind the input of the first argument of a Procedure2 to the result of a 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 delegate, Function 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 predicate,
            Function function)
    {
        return new IfFunction<>(predicate, function);
    }

    public static  Function ifElse(
            Predicate predicate,
            Function trueFunction,
            Function falseFunction)
    {
        return new IfFunction<>(predicate, trueFunction, falseFunction);
    }

    public static , V> CaseFunction caseDefault(
            Function defaultFunction)
    {
        return new CaseFunction(defaultFunction);
    }

    public static , V> CaseFunction caseDefault(
            Function defaultFunction,
            Predicate predicate,
            Function 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;
        }

        @Override
        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;
        }

        @Override
        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;
        }

        @Override
        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;
        }

        @Override
        public V valueOf(T each)
        {
            synchronized (each)
            {
                return this.function.valueOf(each);
            }
        }
    }

    public static  FunctionChain chain(Function function1, Function function2)
    {
        return new FunctionChain<>(function1, function2);
    }

    public static  BooleanFunctionChain chainBoolean(Function function1, BooleanFunction function2)
    {
        return new BooleanFunctionChain<>(function1, function2);
    }

    public static  ByteFunctionChain chainByte(Function function1, ByteFunction function2)
    {
        return new ByteFunctionChain<>(function1, function2);
    }

    public static  CharFunctionChain chainChar(Function function1, CharFunction function2)
    {
        return new CharFunctionChain<>(function1, function2);
    }

    public static  DoubleFunctionChain chainDouble(Function function1, DoubleFunction function2)
    {
        return new DoubleFunctionChain<>(function1, function2);
    }

    public static  FloatFunctionChain chainFloat(Function function1, FloatFunction function2)
    {
        return new FloatFunctionChain<>(function1, function2);
    }

    public static  IntFunctionChain chainInt(Function function1, IntFunction function2)
    {
        return new IntFunctionChain<>(function1, function2);
    }

    public static  LongFunctionChain chainLong(Function function1, LongFunction function2)
    {
        return new LongFunctionChain<>(function1, function2);
    }

    public static  ShortFunctionChain chainShort(Function function1, ShortFunction function2)
    {
        return new ShortFunctionChain<>(function1, function2);
    }

    private static class DoublePassThruFunction implements Function, DoubleFunction
    {
        private static final long serialVersionUID = 1L;

        @Override
        public double doubleValueOf(Double each)
        {
            return each.doubleValue();
        }

        @Override
        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;

        @Override
        public int intValueOf(Integer each)
        {
            return each.intValue();
        }

        @Override
        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;

        @Override
        public long longValueOf(Long each)
        {
            return each.longValue();
        }

        @Override
        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 function;
        private final V defaultValue;

        private DefaultFunction(Function newFunction, V newDefaultValue)
        {
            this.function = newFunction;
            this.defaultValue = newDefaultValue;
        }

        @Override
        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 function;
        private final V nullValue;

        private NullSafeFunction(Function function, V nullValue)
        {
            this.function = function;
            this.nullValue = nullValue;
        }

        @Override
        public V valueOf(T object)
        {
            return object == null ? this.nullValue : this.function.valueOf(object);
        }
    }

    public static  Function> pair(
            Function function1,
            Function function2)
    {
        return t -> 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;

        @Override
        public K valueOf(Map.Entry entry)
        {
            return entry.getKey();
        }
    }

    private static class MapValueFunction implements Function, V>
    {
        private static final long serialVersionUID = 1L;

        @Override
        public V valueOf(Map.Entry 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;

        @Override
        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 function2;

        private FunctionChain(Function function1, Function function2)
        {
            this.function1 = function1;
            this.function2 = function2;
        }

        @Override
        public T3 valueOf(T1 object)
        {
            return this.function2.valueOf(this.function1.valueOf(object));
        }

        public  FunctionChain chain(Function function)
        {
            return new FunctionChain<>(this, function);
        }

        public BooleanFunctionChain chainBoolean(BooleanFunction function)
        {
            return new BooleanFunctionChain<>(this, function);
        }

        public ByteFunctionChain chainByte(ByteFunction function)
        {
            return new ByteFunctionChain<>(this, function);
        }

        public CharFunctionChain chainChar(CharFunction function)
        {
            return new CharFunctionChain<>(this, function);
        }

        public DoubleFunctionChain chainDouble(DoubleFunction function)
        {
            return new DoubleFunctionChain<>(this, function);
        }

        public FloatFunctionChain chainFloat(FloatFunction function)
        {
            return new FloatFunctionChain<>(this, function);
        }

        public IntFunctionChain chainInt(IntFunction function)
        {
            return new IntFunctionChain<>(this, function);
        }

        public LongFunctionChain chainLong(LongFunction function)
        {
            return new LongFunctionChain<>(this, function);
        }

        public ShortFunctionChain chainShort(ShortFunction function)
        {
            return new ShortFunctionChain<>(this, function);
        }
    }

    public static final class BooleanFunctionChain implements BooleanFunction
    {
        private static final long serialVersionUID = 1L;
        private final Function function1;
        private final BooleanFunction function2;

        private BooleanFunctionChain(Function function1, BooleanFunction function2)
        {
            this.function1 = function1;
            this.function2 = function2;
        }

        @Override
        public boolean booleanValueOf(T1 object)
        {
            return this.function2.booleanValueOf(this.function1.valueOf(object));
        }
    }

    public static final class ByteFunctionChain implements ByteFunction
    {
        private static final long serialVersionUID = 1L;
        private final Function function1;
        private final ByteFunction function2;

        private ByteFunctionChain(Function function1, ByteFunction function2)
        {
            this.function1 = function1;
            this.function2 = function2;
        }

        @Override
        public byte byteValueOf(T1 object)
        {
            return this.function2.byteValueOf(this.function1.valueOf(object));
        }
    }

    public static final class CharFunctionChain implements CharFunction
    {
        private static final long serialVersionUID = 1L;
        private final Function function1;
        private final CharFunction function2;

        private CharFunctionChain(Function function1, CharFunction function2)
        {
            this.function1 = function1;
            this.function2 = function2;
        }

        @Override
        public char charValueOf(T1 object)
        {
            return this.function2.charValueOf(this.function1.valueOf(object));
        }
    }

    public static final class DoubleFunctionChain implements DoubleFunction
    {
        private static final long serialVersionUID = 1L;
        private final Function function1;
        private final DoubleFunction function2;

        private DoubleFunctionChain(Function function1, DoubleFunction function2)
        {
            this.function1 = function1;
            this.function2 = function2;
        }

        @Override
        public double doubleValueOf(T1 object)
        {
            return this.function2.doubleValueOf(this.function1.valueOf(object));
        }
    }

    public static final class FloatFunctionChain implements FloatFunction
    {
        private static final long serialVersionUID = 1L;
        private final Function function1;
        private final FloatFunction function2;

        private FloatFunctionChain(Function function1, FloatFunction function2)
        {
            this.function1 = function1;
            this.function2 = function2;
        }

        @Override
        public float floatValueOf(T1 object)
        {
            return this.function2.floatValueOf(this.function1.valueOf(object));
        }
    }

    public static final class IntFunctionChain implements IntFunction
    {
        private static final long serialVersionUID = 1L;
        private final Function function1;
        private final IntFunction function2;

        private IntFunctionChain(Function function1, IntFunction function2)
        {
            this.function1 = function1;
            this.function2 = function2;
        }

        @Override
        public int intValueOf(T1 object)
        {
            return this.function2.intValueOf(this.function1.valueOf(object));
        }
    }

    public static final class LongFunctionChain implements LongFunction
    {
        private static final long serialVersionUID = 1L;
        private final Function function1;
        private final LongFunction function2;

        private LongFunctionChain(Function function1, LongFunction function2)
        {
            this.function1 = function1;
            this.function2 = function2;
        }

        @Override
        public long longValueOf(T1 object)
        {
            return this.function2.longValueOf(this.function1.valueOf(object));
        }
    }

    public static final class ShortFunctionChain implements ShortFunction
    {
        private static final long serialVersionUID = 1L;
        private final Function function1;
        private final ShortFunction function2;

        private ShortFunctionChain(Function function1, ShortFunction function2)
        {
            this.function1 = function1;
            this.function2 = function2;
        }

        @Override
        public short shortValueOf(T1 object)
        {
            return this.function2.shortValueOf(this.function1.valueOf(object));
        }
    }

    private static class FirstOfPairFunction implements Function, T>
    {
        private static final long serialVersionUID = 1L;

        @Override
        public T valueOf(Pair pair)
        {
            return pair.getOne();
        }
    }

    private static class SecondOfPairFunction implements Function, T>
    {
        private static final long serialVersionUID = 1L;

        @Override
        public T valueOf(Pair pair)
        {
            return pair.getTwo();
        }
    }

    private static class ClassForNameFunction extends CheckedFunction>
    {
        private static final long serialVersionUID = 1L;

        @Override
        public Class safeValueOf(String className) throws ClassNotFoundException
        {
            return Class.forName(className);
        }
    }

    private static final class BindObjectIntProcedure implements ObjectIntProcedure
    {
        private static final long serialVersionUID = 1L;
        private final ObjectIntProcedure delegate;
        private final Function function;

        private BindObjectIntProcedure(ObjectIntProcedure delegate, Function function)
        {
            this.delegate = delegate;
            this.function = function;
        }

        @Override
        public void value(T1 each, int index)
        {
            this.delegate.value(this.function.valueOf(each), index);
        }
    }

    private static final class BindProcedure implements Procedure
    {
        private static final long serialVersionUID = 1L;
        private final Procedure delegate;
        private final Function function;

        private BindProcedure(Procedure delegate, Function function)
        {
            this.delegate = delegate;
            this.function = function;
        }

        @Override
        public void value(T1 each)
        {
            this.delegate.value(this.function.valueOf(each));
        }
    }

    private static final class BindProcedure2 implements Procedure2
    {
        private static final long serialVersionUID = 1L;
        private final Procedure2 delegate;
        private final Function function;

        private BindProcedure2(Procedure2 delegate, Function function)
        {
            this.delegate = delegate;
            this.function = function;
        }

        @Override
        public void value(T1 each, T3 constant)
        {
            this.delegate.value(this.function.valueOf(each), constant);
        }
    }

    private static final class BindFunction2 implements Function
    {
        private static final long serialVersionUID = 1L;
        private final Function2 delegate;
        private final T2 parameter;

        private BindFunction2(Function2 delegate, T2 parameter)
        {
            this.delegate = delegate;
            this.parameter = parameter;
        }

        @Override
        public T3 valueOf(T1 object)
        {
            return this.delegate.value(object, this.parameter);
        }
    }

    private static class SwappedPairFunction implements Function, Pair>
    {
        private static final long serialVersionUID = 1L;

        @Override
        public Pair valueOf(Pair pair)
        {
            return pair.swap();
        }
    }

    private static final class ThrowingFunctionAdapter extends CheckedFunction
    {
        private static final long serialVersionUID = 1L;
        private final ThrowingFunction throwingFunction;

        private ThrowingFunctionAdapter(ThrowingFunction throwingFunction)
        {
            this.throwingFunction = throwingFunction;
        }

        @Override
        public V safeValueOf(T object) throws Exception
        {
            return this.throwingFunction.safeValueOf(object);
        }
    }

    private static class TrueFunction implements Function
    {
        private static final long serialVersionUID = 1L;

        @Override
        public Boolean valueOf(Object object)
        {
            return Boolean.TRUE;
        }
    }

    private static class FalseFunction implements Function
    {
        private static final long serialVersionUID = 1L;

        @Override
        public Boolean valueOf(Object object)
        {
            return Boolean.FALSE;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy