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

hu.akarnokd.reactive4java.util.Functions Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2011-2013 David Karnok
 *
 * 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 hu.akarnokd.reactive4java.util;

import hu.akarnokd.reactive4java.base.Action0;
import hu.akarnokd.reactive4java.base.Action1;
import hu.akarnokd.reactive4java.base.Action2;
import hu.akarnokd.reactive4java.base.Func0;
import hu.akarnokd.reactive4java.base.Func1;
import hu.akarnokd.reactive4java.base.Func2;
import hu.akarnokd.reactive4java.base.Pair;
import hu.akarnokd.reactive4java.base.Pred0;
import hu.akarnokd.reactive4java.base.Pred1;
import hu.akarnokd.reactive4java.base.Pred2;

import java.lang.ref.Reference;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import javax.annotation.Nonnull;

/**
 * Helper class with function types.
 * @author akarnokd
 */
public final class Functions {
    /** Constant function returning always false. */
    @Nonnull 
    private static final Pred1 FALSE1 = new Pred1() {
        @Override
        public Boolean invoke(Object param1) {
            return false;
        }
    };
    /** Constant function returning always true. */
    @Nonnull 
    private static final Pred1 TRUE1 = new Pred1() {
        @Override
        public Boolean invoke(Object param1) {
            return true;
        }
    };
    /** Constant function returning always false. */
    @Nonnull 
    private static final Pred2 FALSE2 = new Pred2() {
        @Override
        public Boolean invoke(Object param1, Object param2) {
            return false;
        }
    };
    /** Constant function returning always true. */
    @Nonnull 
    private static final Pred2 TRUE2 = new Pred2() {
        @Override
        public Boolean invoke(Object param1, Object param2) {
            return true;
        }
    };
    /** Constant parameterless function which returns always false. */
    @Nonnull 
    public static final Pred0 FALSE = new Pred0() {
        @Override
        public Boolean invoke() {
            return false;
        }
    };
    /** Constant parameterless function which returns always true. */
    @Nonnull 
    public static final Pred0 TRUE = new Pred0() {
        @Override
        public Boolean invoke() {
            return true;
        }
    };
    /** The identity function which returns its parameter. */
    @Nonnull 
    private static final Func1 IDENTITY = new Func1() {
        @Override
        public Object invoke(Object param1) {
            return param1;
        }
    };
    /** An empty runnable. */
    @Nonnull 
    public static final Runnable EMPTY_RUNNABLE = new Runnable() {
        @Override
        public void run() {

        }
    };
    /** Function to sum integers in aggregators. */
    @Nonnull 
    static final Func2 SUM_INTEGER = new Func2() {
        @Override
        public Integer invoke(Integer param1, Integer param2) {
            return param1 != null ? param1 + param2 : param2;
        }
    };
    /** Function to sum integers in aggregators. */
    @Nonnull 
    static final Func2 SUM_FLOAT = new Func2() {
        @Override
        public Float invoke(Float param1, Float param2) {
            return param1 != null ? param1 + param2 : param2;
        }
    };
    /** Function to sum integers in aggregators. */
    @Nonnull 
    static final Func2 SUM_DOUBLE = new Func2() {
        @Override
        public Double invoke(Double param1, Double param2) {
            return param1 != null ? param1 + param2 : param2;
        }
    };
    /** Function to sum integers in aggregators. */
    @Nonnull 
    static final Func2 SUM_LONG = new Func2() {
        @Override
        public Long invoke(Long param1, Long param2) {
            return param1 != null ? param1 + param2 : param2;
        }
    };
    /** Function to sum integers in aggregators. */
    @Nonnull 
    static final Func2 SUM_BIGINTEGER = new Func2() {
        @Override
        public BigInteger invoke(BigInteger param1, BigInteger param2) {
            return param1 != null ? param1.add(param2) : param2;
        }
    };
    /** Function to sum integers in aggregators. */
    @Nonnull 
    static final Func2 SUM_BIGDECIMAL = new Func2() {
        @Override
        public BigDecimal invoke(BigDecimal param1, BigDecimal param2) {
            return param1 != null ? param1.add(param2) : param2;
        }
    };
    /** A helper function which returns its first parameter. */
    @Nonnull 
    private static final Func2 IDENTITY_FIRST = new Func2() {
        @Override
        public Object invoke(Object param1, Object param2) {
            return param1;
        }
    };
    /** A helper function which returns its second parameter. */
    @Nonnull 
    private static final Func2 IDENTITY_SECOND = new Func2() {
        @Override
        public Object invoke(Object param1, Object param2) {
            return param2;
        }
    };
    /**
     * Returns a function which always returns false regardless of its parameters.
     * @param  the type of the parameter (irrelevant)
     * @return the function which returns always false
     */
    @SuppressWarnings("unchecked")
    @Nonnull
    public static  Pred1 alwaysFalse1() {
        return (Pred1)FALSE1;
    }
    /**
     * Returns a function which always returns true regardless of its parameters.
     * @param  the type of the parameter (irrelevant)
     * @return the function which returns always true
     */
    @Nonnull
    @SuppressWarnings("unchecked")
    public static  Pred1 alwaysTrue1() {
        return (Pred1)TRUE1;
    }
    /**
     * Returns a function which always returns false regardless of its parameters.
     * @param  the type of the parameter (irrelevant)
     * @param  the type of the parameter (irrelevant)
     * @return the function which returns always false
     */
    @SuppressWarnings("unchecked")
    @Nonnull
    public static  Pred2 alwaysFalse2() {
        return (Pred2)FALSE2;
    }
    /**
     * Returns a function which always returns true regardless of its parameters.
     * @param  the type of the parameter (irrelevant)
     * @param  the type of the parameter (irrelevant)
     * @return the function which returns always true
     */
    @Nonnull
    @SuppressWarnings("unchecked")
    public static  Pred2 alwaysTrue2() {
        return (Pred2)TRUE2;
    }
    /**
     * Wraps the given Func0 object into a callable instance.
     * @param  the return type
     * @param func the function to wrap
     * @return the callable wrapper
     */
    @Nonnull
    public static  Callable asCallable(
            @Nonnull final Func0 func) {
        return new Callable() {
            @Override
            public T call() throws Exception {
                return func.invoke();
            }
        };
    }
    /**
     * Wrap the given two argument function returning an integer as a comparator.
     * @param  the type of the elements to compare
     * @param func the function to wrap
     * @return the comparator
     */
    @Nonnull
    public static  Comparator asComparator(
            @Nonnull final Func2 func) {
        return new Comparator() {
            @Override
            public int compare(T o1, T o2) {
                return func.invoke(o1, o2);
            }
        };
    }
    /**
     * Wraps the given action into a function which calls the
     * action and then returns the result value.
     * @param  the result type
     * @param action the action to invoke
     * @param result the result to present after the action invocation
     * @return the function
     */
    @Nonnull 
    public static  Func0 asFunc0(
            @Nonnull final Action0 action, final T result) {
        return new Func0() {
            @Override
            public T invoke() {
                action.invoke();
                return result;
            }
        };
    }
    /**
     * Wraps the given action into a function which calls the
     * action and then returns the result value.
     * @param  the parameter type
     * @param  the result type
     * @param action the action to invoke
     * @param result the result to present after the action invocation
     * @return the function
     */
    @Nonnull 
    public static  Func1 asFunc1(
            @Nonnull final Action1 action, final U result) {
        return new Func1() {
            @Override
            public U invoke(T param1) {
                action.invoke(param1);
                return result;
            }
        };
    }
    /**
     * Wraps the given action into a function which calls the
     * action and then returns the result value.
     * @param  the first parameter type
     * @param  the second parameter type
     * @param  the result type
     * @param action the action to invoke
     * @param result the result to present after the action invocation
     * @return the function
     */
    @Nonnull 
    public static  Func2 asFunc2(
            @Nonnull final Action2 action, final V result) {
        return new Func2() {
            @Override
            public V invoke(T param1, U param2) {
                action.invoke(param1, param2);
                return result;
            }
        };
    }
    /**
     * Wraps the given Callable function into a Func0 object.
     * @param  the return type of the function
     * @param call the original call function
     * @return the Func0 function wrapping the call
     */
    @Nonnull
    public static  Func0 asFunc0(
            @Nonnull final Callable call) {
        return new Func0() {
            @Override
            public T invoke() {
                try {
                    return call.call();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }
    /**
     * Wrap the given comparator function into a function object.
     * @param  the type of elements to compare
     * @param comparator the comparator
     * @return the wrapped comparator
     */
    @Nonnull
    public static  Func2 asFunc2(
            @Nonnull final Comparator comparator) {
        return new Func2() {
            @Override
            public Integer invoke(T param1, T param2) {
                return comparator.compare(param1, param2);
            }
        };
    }
    /**
     * Wraps the given atomic boolean and returns its value.
     * @param source the source atomic reference
     * @return the function
     */
    @Nonnull
    public static Func0 asFunc0(
            @Nonnull final AtomicBoolean source) {
        return new Func0() {
            @Override
            public Boolean invoke() {
                return source.get();
            }
        };
    }
    /**
     * Wraps the given atomic integer object and returns its value.
     * @param source the source atomic reference
     * @return the function
     */
    @Nonnull
    public static Func0 asFunc0(
            @Nonnull final AtomicInteger source) {
        return new Func0() {
            @Override
            public Integer invoke() {
                return source.get();
            }
        };
    }
    /**
     * Wraps the given atomic integer object and returns its value.
     * @param source the source atomic reference
     * @return the function
     */
    @Nonnull
    public static Func0 asFunc0(
            @Nonnull final AtomicLong source) {
        return new Func0() {
            @Override
            public Long invoke() {
                return source.get();
            }
        };
    }
    /**
     * Wraps the given atomic reference object and returns its value.
     * @param  the type of the contained object
     * @param source the source atomic reference
     * @return the function
     */
    @Nonnull
    public static  Func0 asFunc0(
            @Nonnull final AtomicReference source) {
        return new Func0() {
            @Override
            public T invoke() {
                return source.get();
            }
        };
    }
    /**
     * Returns a convenience comparator which basically compares 
     * objects which implement the Comparable
     * interface. The comparator is null safe in the manner, 
     * that nulls are always less than any non-nulls.
     * To have a comparator which places nulls last, use the comparator0() method.
     * @param  the element types to compare
     * @return the comparator
     * @see Functions#comparator0()
     */
    @Nonnull
    public static > Comparator comparator() {
        return new Comparator() {
            @Override
            public int compare(T o1, T o2) {
                if (o1 == null) {
                    if (o2 == null) {
                        return 0;
                    }
                    return -1;
                }
                if (o2 == null) {
                    return 1;
                }
                return o1.compareTo(o2);
            }
        };
    }
    /**
     * Returns a convenience comparator which basically compares objects which implement the Comparable
     * interface. The comparator is null safe in the manner, that nulls are always greater than any non-nulls.
     * To have a comparator which places nulls first, use the comparator() method.
     * @param  the element types to compare
     * @return the comparator
     */
    @Nonnull
    public static > Comparator comparator0() {
        return new Comparator() {
            @Override
            public int compare(T o1, T o2) {
                if (o1 == null) {
                    if (o2 == null) {
                        return 0;
                    }
                    return 1;
                }
                if (o2 == null) {
                    return -1;
                }
                return o1.compareTo(o2);
            }
        };
    }
    /**
     * Creates a new comparator which reverses the order of the comparison.
     * @param  the element type, which must be self comparable
     * @return the new comparator
     */
    @Nonnull
    public static > Comparator comparatorReverse() {
        return new Comparator() {
            @Override
            public int compare(T o1, T o2) {
                return o2.compareTo(o1);
            }
        };
    }
    /**
     * Creates a new comparator which reverses the order produced by the given
     * normal comparator.
     * @param  the element type
     * @param normal the normal comparator
     * @return the new comparator
     */
    @Nonnull
    public static  Comparator comparatorReverse(
            @Nonnull final Comparator normal) {
        return new Comparator() {
            @Override
            public int compare(T o1, T o2) {
                return normal.compare(o2, o1);
            }
        };
    }
    /**
     * Creates a function which returns always the same value.
     * @param  the parameter type, irrelevant
     * @param  the value type to return
     * @param value the value to return
     * @return the function
     */
    @Nonnull
    public static  Func1 constant(final Result value) {
        return new Func1() {
            @Override
            public Result invoke(Param1 param1) {
                return value;
            }
        };
    }
    /**
     * Creates a function which returns always the same value.
     * @param  the value type to return
     * @param value the value to return
     * @return the function
     */
    @Nonnull
    public static  Func0 constant0(final T value) {
        return new Func0() {
            @Override
            public T invoke() {
                return value;
            }
        };
    }
    /**
     * @return a function which returns param - 1 for BigIntegers.
     */
    @Nonnull
    public static Func1 decrementBigInteger() {
        return new Func1() {
            @Override
            public BigInteger invoke(BigInteger param1) {
                return param1.subtract(BigInteger.ONE);
            }
        };
    }
    /**
     * @return a function which returns param - 1 for Integers.
     */
    @Nonnull
    public static Func1 decrementInt() {
        return new Func1() {
            @Override
            public Integer invoke(Integer param1) {
                return param1 - 1;
            }
        };
    }
    /**
     * @return a function which returns param + 1 for Longs.
     */
    @Nonnull
    public static Func1 decrementLong() {
        return new Func1() {
            @Override
            public Long invoke(Long param1) {
                return param1 - 1;
            }
        };
    }
    /**
     * Returns a function which returns true if its submitted parameter
     * value equals to the given constant.
     * @param  the value type
     * @param value the value
     * @return the function
     */
    @Nonnull
    public static  Func1 equal(final T value) {
        return new Func1() {
            @Override
            public Boolean invoke(T param) {
                return value == param || (value != null && value.equals(param));
            }
        };
    }
    /**
     * Create a function which returns true for submitted values greater or equal than the given value.
     * @param  a type which is comparable with itself
     * @param value constant to compare against
     * @return the function
     */
    @Nonnull
    public static > Func1 greaterOrEqual(
            @Nonnull final T value) {
        return new Func1() {
            @Override
            public Boolean invoke(T param1) {
                return param1.compareTo(value) >= 0;
            }
        };
    }
    /**
     * Returns a function which returns true if the function parameter
     * is greater or equal to the constant in respect to the supplied comparator.
     * @param  the value type
     * @param value constant to compare against
     * @param comparator the comparator for Ts.
     * @return the function
     */
    @Nonnull
    public static  Func1 greaterOrEqual(
            @Nonnull final T value,
            @Nonnull final Comparator comparator) {
        return new Func1() {
            @Override
            public Boolean invoke(T param1) {
                return comparator.compare(param1, value) >= 0;
            }
        };
    }
    /**
     * Create a function which returns true for submitted values greater
     * than the given value.
     * @param  a type which is comparable with itself
     * @param value constant to compare against
     * @return the function
     */
    @Nonnull
    public static > Func1 greaterThan(
            @Nonnull final T value) {
        return new Func1() {
            @Override
            public Boolean invoke(T param1) {
                return param1.compareTo(value) > 0;
            }
        };
    }
    /**
     * Returns a function which returns true if the function parameter
     * is greater than the constant in respect to the supplied comparator.
     * @param  the value type
     * @param value constant to compare against
     * @param comparator the comparator for Ts.
     * @return the function
     */
    @Nonnull
    public static  Func1 greaterThan(
            @Nonnull final T value,
            @Nonnull final Comparator comparator) {
        return new Func1() {
            @Override
            public Boolean invoke(T param1) {
                return comparator.compare(param1, value) > 0;
            }
        };
    }
    /**
     * Returns a function which returns its parameter value.
     * @param  the type of the object
     * @return the function which returns its parameter as is
     */
    @SuppressWarnings("unchecked")
    @Nonnull
    public static  Func1 identity() {
        return (Func1)IDENTITY;
    }
    /**
     * Returns a helper function of two parameters which always returns its first parameter.
     * @param  the result and the first parameter type
     * @param  the second parameter type, irrelevant
     * @return the function
     */
    @SuppressWarnings("unchecked")
    @Nonnull
    public static  Func2 identityFirst() {
        return (Func2)IDENTITY_FIRST;
    }
    /**
     * Returns a helper function of two parameters which always returns its second parameter.
     * @param  the result and the second parameter type
     * @param  the first parameter type, irrelevant
     * @return the function
     */
    @SuppressWarnings("unchecked")
    @Nonnull
    public static  Func2 identitySecond() {
        return (Func2)IDENTITY_SECOND;
    }
    /**
     * @return a function which returns param + 1 for BigIntegers.
     */
    @Nonnull
    public static Func1 incrementBigInteger() {
        return new Func1() {
            @Override
            public BigInteger invoke(BigInteger param1) {
                return param1.add(BigInteger.ONE);
            }
        };
    }
    /**
     * Returns a function which increments its parameter by the given amount.
     * Can be used to decrement if value is less than zero
     * @param value the value to increment by
     * @return the function
     */
    @Nonnull
    public static Func1 incrementBy(final int value) {
        return new Func1() {
            @Override
            public Integer invoke(Integer param1) {
                return param1 + value;
            }
        };
    }
    /**
     * Returns a function which increments its parameter by the given amount.
     * Can be used to decrement if value is less than zero
     * @param value the value to increment by
     * @return the function
     */
    @Nonnull
    public static Func1 incrementBy(final long value) {
        return new Func1() {
            @Override
            public Long invoke(Long param1) {
                return param1 + value;
            }
        };
    }
    /**
     * @return a function which returns param + 1 for Integers.
     */
    @Nonnull
    public static Func1 incrementInt() {
        return new Func1() {
            @Override
            public Integer invoke(Integer param1) {
                return param1 + 1;
            }
        };
    }
    /**
     * @return a function which returns param + 1 for Longs.
     */
    @Nonnull
    public static Func1 incrementLong() {
        return new Func1() {
            @Override
            public Long invoke(Long param1) {
                return param1 + 1;
            }
        };
    }
    /**
     * Create a function which returns true for submitted values less or equal
     * than the given value.
     * @param  a type which is comparable to itself
     * @param value constant to compare against
     * @return the function
     */
    @Nonnull
    public static > Func1 lessOrEqual(
            @Nonnull final T value) {
        return new Func1() {
            @Override
            public Boolean invoke(T param1) {
                return param1.compareTo(value) <= 0;
            }
        };
    }
    /**
     * Create a function which returns true for submitted values less or equal
     * than the given value in respect to the supplied comparator.
     * @param  a type which is comparable to itself
     * @param value constant to compare against
     * @param comparator the comparator
     * @return the function
     */
    @Nonnull
    public static  Func1 lessOrEqual(
            @Nonnull final T value,
            @Nonnull final Comparator comparator) {
        return new Func1() {
            @Override
            public Boolean invoke(T param1) {
                return comparator.compare(param1, value) <= 0;
            }
        };
    }
    /**
     * Create a function which returns true for submitted values less
     * than the given value.
     * @param  a type which is comparable with itself
     * @param value constant to compare against
     * @return the function
     */
    @Nonnull
    public static > Func1 lessThan(
            @Nonnull final T value) {
        return new Func1() {
            @Override
            public Boolean invoke(T param1) {
                return param1.compareTo(value) < 0;
            }
        };
    }
    /**
     * Create a function which returns true for submitted values less
     * than the given value in respect to the supplied comparator.
     * @param  a type which is comparable to itself
     * @param value constant to compare against
     * @param comparator the comparator
     * @return the function
     */
    @Nonnull
    public static  Func1 lessThan(
            @Nonnull final T value,
            @Nonnull final Comparator comparator) {
        return new Func1() {
            @Override
            public Boolean invoke(T param1) {
                return comparator.compare(param1, value) < 0;
            }
        };
    }
    /**
     * Returns a function which returns the greater of its parameters.
     * If only one of the parameters is null, the other parameter is returned.
     * If both parameters are null, null is returned.
     * @param  the parameter types, which must be self-comparable
     * @return the function
     */
    @Nonnull
    public static > Func2 max() {
        return new Func2() {
            @Override
            public T invoke(T param1, T param2) {
                if (param1 == null || param2 == null) {
                    if (param2 == null) {
                        return param1;
                    }
                    return param2;
                }
                return param1.compareTo(param2) < 0 ? param2 : param1;
            }
        };
    }
    /**
     * Returns a function which returns the greater of its parameters in respect to the supplied Comparator.
     * If only one of the parameters is null, the other parameter is returned.
     * If both parameters are null, null is returned.
     * @param  the parameter types, which must be self-comparable
     * @param comparator the value comparator
     * @return the function
     */
    @Nonnull
    public static  Func2 max(
            @Nonnull final Comparator comparator) {
        return new Func2() {
            @Override
            public T invoke(T param1, T param2) {
                if (param1 == null || param2 == null) {
                    if (param2 == null) {
                        return param1;
                    }
                    return param2;
                }
                return comparator.compare(param1, param2) < 0 ? param2 : param1;
            }
        };
    }
    /**
     * Returns a function which returns the smaller of its parameters.
     * If only one of the parameters is null, the other parameter is returned.
     * If both parameters are null, null is returned.
     * @param  the parameter types, which must be self-comparable
     * @return the function
     */
    @Nonnull
    public static > Func2 min() {
        return new Func2() {
            @Override
            public T invoke(T param1, T param2) {
                if (param1 == null || param2 == null) {
                    if (param2 == null) {
                        return param1;
                    }
                    return param2;
                }
                return param1.compareTo(param2) > 0 ? param2 : param1;
            }
        };
    }
    /**
     * Returns a function which returns the smaller of its parameters in respect to the supplied Comparator.
     * If only one of the parameters is null, the other parameter is returned.
     * If both parameters are null, null is returned.
     * @param  the parameter types, which must be self-comparable
     * @param comparator the value comparator
     * @return the function
     */
    @Nonnull
    public static  Func2 min(
            @Nonnull final Comparator comparator) {
        return new Func2() {
            @Override
            public T invoke(T param1, T param2) {
                if (param1 == null || param2 == null) {
                    if (param2 == null) {
                        return param1;
                    }
                    return param2;
                }
                return comparator.compare(param1, param2) > 0 ? param2 : param1;
            }
        };
    }
    /**
     * Creates a function which negates the supplied function's value.
     * @param func the original function
     * @return the wrapped negator function
     */
    @Nonnull
    public static Func0 not(
            @Nonnull final Func0 func) {
        return new Func0() {
            @Override
            public Boolean invoke() {
                return func.invoke() == Boolean.TRUE ? Boolean.FALSE : Boolean.TRUE;
            }
        };
    }
    /**
     * Returns a function which returns true if its sumbitted parameter
     * value does not equal to the given constant.
     * @param  the value type
     * @param value the value
     * @return the function
     */
    @Nonnull
    public static  Func1 notEqual(
            @Nonnull final T value) {
        return new Func1() {
            @Override
            public Boolean invoke(T param) {
                return value != param && (value == null || !value.equals(param));
            }
        };
    }
    /**
     * Retuns a function that adds two BigDecimal numbers and
     * returns a new one.
     * 

If the first parameter is null, it returns the second parameter.

* @return Function to sum integers in aggregators. */ @Nonnull public static Func2 sumBigDecimal() { return SUM_BIGDECIMAL; } /** * Retuns a function that adds two BigInteger numbers and * returns a new one. *

If the first parameter is null, it returns the second parameter.

* @return Function to sum integers in aggregators. */ @Nonnull public static Func2 sumBigInteger() { return SUM_BIGINTEGER; } /** * Retuns a function that adds two Double number and * returns a new one. *

If the first parameter is null, it returns the second parameter.

* @return Function to sum integers in aggregators. */ @Nonnull public static Func2 sumDouble() { return SUM_DOUBLE; } /** * Retuns a function that adds two Float number and * returns a new one. *

If the first parameter is null, it returns the second parameter.

* @return Function to sum integers in aggregators. */ @Nonnull public static Func2 sumFloat() { return SUM_FLOAT; } /** * Retuns a function that adds two Integer number and * returns a new one. *

If the first parameter is null, it returns the second parameter.

* @return Function to sum integers in aggregators. */ @Nonnull public static Func2 sumInteger() { return SUM_INTEGER; } /** * Retuns a function that adds two Long number and * returns a new one. *

If the first parameter is null, it returns the second parameter.

* @return Function to sum integers in aggregators. */ @Nonnull public static Func2 sumLong() { return SUM_LONG; } /** A null safe equals function. */ @Nonnull private static final Func2 NULL_SAFE_EQUALS = new Func2() { @Override public Boolean invoke(Object param1, Object param2) { return param1 == param2 || (param1 != null && param1.equals(param2)); } }; /** * Returns a function which compares its two parameters by a null-safe * equals. * @param the parameter type * @return the function */ @SuppressWarnings("unchecked") @Nonnull public static Func2 equals() { return (Func2)NULL_SAFE_EQUALS; } /** * Creates a single parameter function which returns values from the given map. * @param the key type * @param the value type * @param map the backing map. * @return the created function * @since 0.96 */ @Nonnull public static Func1 asFunc1( @Nonnull final Map map) { return new Func1() { @Override public V invoke(K param1) { return map.get(param1); } }; } /** * Wraps a two-layer (map of map of something) into a two parameter function. *

If the first level map returns null, the function returns null.

* @param the first level key type * @param the second level key type * @param the value type type * @param map the source map of map of something * @return the function * @since 0.96 */ @Nonnull public static Func2 asFunc2( @Nonnull final Map> map) { return new Func2() { @Override public V invoke(K1 param1, K2 param2) { Map map2 = map.get(param1); if (map2 != null) { return map2.get(param2); } return null; } }; } /** * Creates a single parameter predicate function which returns true if the supplied * parameter is in the given collection. * @param the element type * @param set the backing set * @return the created function * @since 0.96 */ @Nonnull public static Func1 asFunc1( @Nonnull final Set set) { return new Pred1() { @Override public Boolean invoke(K param1) { return set.contains(param1); } }; } /** * A list creator factory. * @param the value type * @return a function which creates a new empty instance of the given concrete list implementation. * @since 0.96.1 */ @Nonnull public static Func0> arrayListProvider() { return new Func0>() { @Override public ArrayList invoke() { return new ArrayList(); } }; } /** * A list creator factory for Func1 that ignores the parameter. * @param the value type * @param the function parameter type, ignored * @return a function which creates a new empty instance of * the given concrete list implementation. * @since 0.97 */ @Nonnull public static Func1> arrayListProvider1() { return new Func1>() { @Override public ArrayList invoke(U ignored) { return new ArrayList(); } }; } /** * A list creator factory. * @param the value type * @return a function which creates a new empty instance of the given concrete list implementation. * @since 0.96.1 */ @Nonnull public static Func0> linkedListProvider() { return new Func0>() { @Override public LinkedList invoke() { return new LinkedList(); } }; } /** * A map creator factory. * @param the key type * @param the value type * @return a function which creates a new empty instance of the given concrete map implementation. * @since 0.96.1 */ @Nonnull public static Func0> hashMapProvider() { return new Func0>() { @Override public HashMap invoke() { return new HashMap(); } }; } /** * A map creator factory. * @param the key type * @param the value type * @return a function which creates a new empty instance of the given concrete map implementation. * @since 0.96.1 */ @Nonnull public static Func0> treeMapProvider() { return new Func0>() { @Override public TreeMap invoke() { return new TreeMap(); } }; } /** * A map creator factory. * @param the key type * @param the value type * @param keyComparator the key comparator function * @return a function which creates a new empty instance of the given concrete map implementation. * @since 0.96.1 */ @Nonnull public static Func0> treeMapProvider(@Nonnull final Comparator keyComparator) { return new Func0>() { @Override public TreeMap invoke() { return new TreeMap(keyComparator); } }; } /** * A map creator factory. * @param the key type * @param the value type * @return a function which creates a new empty instance of the given concrete map implementation. * @since 0.96.1 */ @Nonnull public static Func0> linkedHashMapProvider() { return new Func0>() { @Override public LinkedHashMap invoke() { return new LinkedHashMap(); } }; } /** * A map creator factory. * @param the key type * @param the value type * @return a function which creates a new empty instance of the given concrete map implementation. * @since 0.96.1 */ @Nonnull public static Func0> concurrentHashMapProvider() { return new Func0>() { @Override public ConcurrentHashMap invoke() { return new ConcurrentHashMap(); } }; } /** * A set creation provider. * @param the element type * @return the function which creates an empty instance of the set * @since 0.96.1 */ @Nonnull public static Func0> hashSetProvider() { return new Func0>() { @Override public HashSet invoke() { return new HashSet(); } }; } /** * A set creation provider. * @param the element type * @return the function which creates an empty instance of the set * @since 0.96.1 */ @Nonnull public static Func0> treeSetProvider() { return new Func0>() { @Override public TreeSet invoke() { return new TreeSet(); } }; } /** * A set creation provider. * @param the element type * @param elementComparator the custom element comparator * @return the function which creates an empty instance of the set * @since 0.96.1 */ @Nonnull public static Func0> treeSetProvider(@Nonnull final Comparator elementComparator) { return new Func0>() { @Override public TreeSet invoke() { return new TreeSet(elementComparator); } }; } /** * Wrap the given two dimensional array into a function which * returns the {@code param1, param2} element. * @param values the values * @return the function * @since 0.96.1 */ @Nonnull public static Func2 asFunc2(@Nonnull final double[][] values) { return new Func2() { @Override public Double invoke(Integer param1, Integer param2) { return values[param1][param2]; } }; } /** * Wrap the given two dimensional array into a function which * returns the {@code param1, param2} element. * @param values the values * @return the function * @since 0.96.1 */ @Nonnull public static Func2 asFunc2(@Nonnull final int[][] values) { return new Func2() { @Override public Integer invoke(Integer param1, Integer param2) { return values[param1][param2]; } }; } /** * Wrap the given two dimensional array into a function which * returns the {@code param1, param2} element. * @param values the values * @return the function * @since 0.96.1 */ @Nonnull public static Func2 asFunc2(@Nonnull final long[][] values) { return new Func2() { @Override public Long invoke(Integer param1, Integer param2) { return values[param1][param2]; } }; } /** * Wrap the given two dimensional array into a function which * returns the {@code param1, param2} element. * @param the element type * @param values the values * @return the function * @since 0.96.1 */ @Nonnull public static Func2 asFunc2(@Nonnull final T[][] values) { return new Func2() { @Override public T invoke(Integer param1, Integer param2) { return values[param1][param2]; } }; } /** * Wrap the given one dimensional array into a function which * returns the {@code param1} element. * @param values the values * @return the function * @since 0.96.1 */ @Nonnull public static Func1 asFunc1(@Nonnull final double... values) { return new Func1() { @Override public Double invoke(Integer param1) { return values[param1]; } }; } /** * Wrap the given one dimensional array into a function which * returns the {@code param1} element. * @param values the values * @return the function * @since 0.96.1 */ @Nonnull public static Func1 asFunc1(@Nonnull final long... values) { return new Func1() { @Override public Long invoke(Integer param1) { return values[param1]; } }; } /** * Wrap the given one dimensional array into a function which * returns the {@code param1} element. * @param values the values * @return the function * @since 0.96.1 */ @Nonnull public static Func1 asFunc1(@Nonnull final int... values) { return new Func1() { @Override public Integer invoke(Integer param1) { return values[param1]; } }; } /** * Wrap the given list into a function which * returns the {@code param1} element. * @param the element type * @param values the values * @return the function * @since 0.96.1 */ @Nonnull public static Func1 asFunc1(@Nonnull final List values) { return new Func1() { @Override public T invoke(Integer param1) { return values.get(param1); } }; } /** * Wrap the given list of number into a function which * returns the {@code param1} element as double. * @param values the values * @return the function * @since 0.96.1 */ @Nonnull public static Func1 asDoubleFunc1(@Nonnull final List values) { return new Func1() { @Override public Double invoke(Integer param1) { return values.get(param1).doubleValue(); } }; } /** * Wrap the given list of number into a function which * returns the {@code param1} element as int. * @param values the values * @return the function * @since 0.96.1 */ @Nonnull public static Func1 asIntFunc1(@Nonnull final List values) { return new Func1() { @Override public Integer invoke(Integer param1) { return values.get(param1).intValue(); } }; } /** * Wrap the given list of number into a function which * returns the {@code param1} element as long. * @param values the values * @return the function * @since 0.96.1 */ @Nonnull public static Func1 asLongFunc1(@Nonnull final List values) { return new Func1() { @Override public Long invoke(Integer param1) { return values.get(param1).longValue(); } }; } /** * Returns a function which takes the logical AND of the given two functions when invoked. * @param the parameter type * @param f1 the first function * @param f2 the second function * @return the combined function * @since 0.96.1 */ @Nonnull public static Func1 and( @Nonnull final Func1 f1, @Nonnull final Func1 f2) { return new Func1() { @Override public Boolean invoke(T param1) { return f1.invoke(param1) && f2.invoke(param1); } }; } /** * @return curried logical OR */ @Nonnull public static Func2 or() { return new Func2() { @Override public Boolean invoke(Boolean a, Boolean b) { return a || b; } }; } /** * Returns a function which takes the logical OR of the given two functions when invoked. * @param the element type * @param f1 the first function * @param f2 the second function * @return the combined function * @since 0.96.1 */ @Nonnull public static Func1 or( @Nonnull final Func1 f1, @Nonnull final Func1 f2) { return new Func1() { @Override public Boolean invoke(T param1) { return f1.invoke(param1) || f2.invoke(param1); } }; } /** * Returns a function which takes the logical XOR of the given two functions when invoked. * @param the element type * @param f1 the first function * @param f2 the second function * @return the combined function * @since 0.96.1 */ @Nonnull public static Func1 xor( @Nonnull final Func1 f1, @Nonnull final Func1 f2) { return new Func1() { @Override public Boolean invoke(T param1) { return f1.invoke(param1) ^ f2.invoke(param1); } }; } /** * Returns a function which takes the logical AND of the given two functions when invoked. * @param the first parameter type * @param the second parameter type * @param f1 the first function * @param f2 the second function * @return the combined function * @since 0.96.1 */ @Nonnull public static Func2 and( @Nonnull final Func2 f1, @Nonnull final Func2 f2) { return new Pred2() { @Override public Boolean invoke(T param1, U param2) { return f1.invoke(param1, param2) && f2.invoke(param1, param2); } }; } /** * Returns a function which takes the logical OR of the given two functions when invoked. * @param the first parameter type * @param the second parameter type * @param f1 the first function * @param f2 the second function * @return the combined function * @since 0.96.1 */ @Nonnull public static Func2 or( @Nonnull final Func2 f1, @Nonnull final Func2 f2) { return new Pred2() { @Override public Boolean invoke(T param1, U param2) { return f1.invoke(param1, param2) || f2.invoke(param1, param2); } }; } /** * Returns a function which takes the logical XOR of the given two functions when invoked. * @param the first parameter type * @param the second parameter type * @param f1 the first function * @param f2 the second function * @return the combined function * @since 0.96.1 */ @Nonnull public static Func2 xor( @Nonnull final Func2 f1, @Nonnull final Func2 f2) { return new Pred2() { @Override public Boolean invoke(T param1, U param2) { return f1.invoke(param1, param2) ^ f2.invoke(param1, param2); } }; } /** * Returns a function which creates the logical not of the wrapped function value for value t:T. * @param the parameter type * @param f the function to wrap * @return the new function * @since 0.96.1 */ @Nonnull public static Func1 not( @Nonnull final Func1 f) { return new Pred1() { @Override public Boolean invoke(T param1) { return !f.invoke(param1); } }; } /** * Returns a function which creates the logical not of the wrapped function value for value * t:T and u:U. * @param the first parameter type * @param the second parameter type * @param f the function to wrap * @return the new function * @since 0.96.1 */ @Nonnull public static Func2 not( @Nonnull final Func2 f) { return new Pred2() { @Override public Boolean invoke(T param1, U param2) { return !f.invoke(param1, param2); } }; } // #GWT-IGNORE-START /** * Wrap the given reference into a function. *

Note that the references may return null if their * contained object gets garbage collected.

* @param the referenced object type * @param ref the reference object * @return the function */ @Nonnull public static Func0 asFunc0(@Nonnull final Reference ref) { return new Func0() { @Override public T invoke() { return ref.get(); } }; } // #GWT-IGNORE-END /** * @return curried logical AND */ @Nonnull public static Func2 and() { return new Func2() { @Override public Boolean invoke(Boolean a, Boolean b) { return a && b; } }; } /** * Returns a function which takes the logical AND of the given two functions when invoked. * @param f1 the first function * @param f2 the second function * @return the combined function * @since 0.96.1 */ @Nonnull public static Func0 and( @Nonnull final Func0 f1, @Nonnull final Func0 f2) { return new Func0() { @Override public Boolean invoke() { return f1.invoke() && f2.invoke(); } }; } /** * Returns a function which takes the logical OR of the given two functions when invoked. * @param f1 the first function * @param f2 the second function * @return the combined function * @since 0.96.1 */ @Nonnull public static Func0 or( @Nonnull final Func0 f1, @Nonnull final Func0 f2) { return new Func0() { @Override public Boolean invoke() { return f1.invoke() || f2.invoke(); } }; } /** * Returns a function which takes the logical XOR of the given two functions when invoked. * @param f1 the first function * @param f2 the second function * @return the combined function * @since 0.96.1 */ @Nonnull public static Func0 xor( @Nonnull final Func0 f1, @Nonnull final Func0 f2) { return new Func0() { @Override public Boolean invoke() { return f1.invoke() ^ f2.invoke(); } }; } /** * Wraps the given zero parameter function into a 1 parameter function which * ignores its parameter. * @param the function parameter type (irrelevant) * @param the function return type * @param f the function to wrap * @return the new function * @since 0.96.1 */ @Nonnull public static Func1 asFunc1(@Nonnull final Func0 f) { return new Func1() { @Override public U invoke(T param1) { return f.invoke(); } }; } /** * Wraps the given zero parameter function into a 2 parameter function which * ignores its parameters. * @param the function first parameter type (irrelevant) * @param the function second parameter type (irrelevant) * @param the function return type * @param f the function to wrap * @return the new function * @since 0.96.1 */ @Nonnull public static Func2 asFunc2(@Nonnull final Func0 f) { return new Func2() { @Override public V invoke(T param1, U param2) { return f.invoke(); } }; } /** * Creates a constant function which always returns the given * value regardless of the parameters. * @param the first parameter type, irrelevant * @param the second parameter type, irrelevant * @param the return type * @param value the value to return * @return the created function */ @Nonnull public static Func2 constant2(final V value) { return new Func2() { @Override public V invoke(T param1, U param2) { return value; } }; } /** * Wraps the given Runnable into a function which calls the * action and then returns the result value. * @param the result type * @param action the action to invoke * @param result the result to present after the action invocation * @return the function * @since 0.96.1 */ @Nonnull public static Func0 asFunc0(final Runnable action, final T result) { return new Func0() { @Override public T invoke() { action.run(); return result; } }; } /** * Wraps the given Runnable into a function which calls the * action and then returns the result value. * @param the parameter type, irrelevant * @param the result type * @param action the action to invoke * @param result the result to present after the action invocation * @return the function * @since 0.96.1 */ @Nonnull public static Func1 asFunc1( @Nonnull final Runnable action, final U result) { return new Func1() { @Override public U invoke(T param1) { action.run(); return result; } }; } /** * Wraps the given action into a function which calls the * action and then returns the result value. * @param the parameter type, irrelevant * @param the result type * @param action the action to invoke * @param result the result to present after the action invocation * @return the function * @since 0.96.1 */ @Nonnull public static Func1 asFunc1( @Nonnull final Action0 action, final U result) { return new Func1() { @Override public U invoke(T param1) { action.invoke(); return result; } }; } /** * Wraps the given callable function into a 1 parameter function. *

If the callable throws an exception, it gets wrapped into RuntimeException and gets rethrown.

* @param the function parameter type, irrelevant * @param the return type * @param call the callable instance * @return the new function */ @Nonnull public static Func1 asFunc1( @Nonnull final Callable call) { return new Func1() { @Override public U invoke(T param1) { try { return call.call(); } catch (Exception ex) { throw new RuntimeException(ex); } } }; } /** * Wraps the given callable function into a 2 parameter function. *

If the callable throws an exception, it gets wrapped into RuntimeException and gets rethrown.

* @param the function first parameter type, irrelevant * @param the function second parameter type, irrelevant * @param the return type * @param call the callable instance * @return the new function */ public static Func2 asFunc2(@Nonnull final Callable call) { return new Func2() { @Override public V invoke(T param1, U param2) { try { return call.call(); } catch (Exception ex) { throw new RuntimeException(ex); } } }; } /** * Wraps the given action into a 2 parameter function which returns the supplied value. *

If the callable throws an exception, it gets wrapped into RuntimeException and gets rethrown.

* @param the function first parameter type, irrelevant * @param the function second parameter type, irrelevant * @param the return type * @param action the action to invoke * @param value the value to return * @return the new function */ @Nonnull public static Func2 asFunc2(@Nonnull final Action0 action, final V value) { return new Func2() { @Override public V invoke(T param1, U param2) { action.invoke(); return value; } }; } /** * Wraps the given Runnable into a 2 parameter function which returns the supplied value. *

If the callable throws an exception, it gets wrapped into RuntimeException and gets rethrown.

* @param the function first parameter type, irrelevant * @param the function second parameter type, irrelevant * @param the return type * @param action the action to invoke * @param value the value to return * @return the new function */ @Nonnull public static Func2 asFunc2(@Nonnull final Runnable action, final V value) { return new Func2() { @Override public V invoke(T param1, U param2) { action.run(); return value; } }; } /** * @param the first parameter type * @param the second parameter type * @return a function which pairs its arguments */ @Nonnull public static Func2> pairUp() { return new Func2>() { @Override public Pair invoke(T param1, U param2) { return Pair.of(param1, param2); } }; } /** * @return Returns a function that negates the incoming boolean value. */ @Nonnull public static Func1 negate() { return new Func1() { @Override public Boolean invoke(Boolean param1) { return param1 == Boolean.TRUE ? Boolean.FALSE : Boolean.TRUE; } }; } /** * Returns a function which calls the supplied * function with swapped parameter order. * @param the first parameter, becomes second in func * @param the second parameter, becomes first in func * @param the result type * @param func the function to swap around. * @return the swapping function. * @since 0.97 */ public static Func2 swap(final Func2 func) { return new Func2() { @Override public V invoke(T param1, U param2) { return func.invoke(param2, param1); } }; } /** * Casts an integer value to long. * @since 0.97 * @see #LONG_TO_INT_CHECKED * @see #LONG_TO_INT */ public static final Func1 INT_TO_LONG = new Func1() { @Override public Long invoke(Integer param1) { return param1.longValue(); } }; /** * Converts a long to integer or throws an ArithmeticException if * overflow or underflow happens. * @since 0.97 * @see #LONG_TO_INT * @see #INT_TO_LONG */ public static final Func1 LONG_TO_INT_CHECKED = new Func1() { @Override public Integer invoke(Long param1) { long v = param1.longValue(); if (v > Integer.MAX_VALUE || v < Integer.MIN_VALUE) { throw new ArithmeticException("Integer overflow"); } return param1.intValue(); } }; /** * Converts a long to integer, ignoring any overflow or underflow. * @since 0.97 * @see #LONG_TO_INT_CHECKED * @see #INT_TO_LONG */ public static final Func1 LONG_TO_INT = new Func1() { @Override public Integer invoke(Long param1) { return param1.intValue(); } }; /** Utility class. */ private Functions() { } }