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.
hu.akarnokd.reactive4java.util.Functions Maven / Gradle / Ivy
/*
* 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 extends T> 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 super T, ? super T, Integer> 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 super T> 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 super T, ? super U> 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 extends T> 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 super T> 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 extends T> 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 super T> 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 BigInteger
s.
*/
@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 super T> 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 super T> 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 BigInteger
s.
*/
@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 super T> 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 super T> 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 super T> 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 super T> 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 super K, ? extends V> 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 super K1, ? extends Map super K2, ? extends V>> map) {
return new Func2() {
@Override
public V invoke(K1 param1, K2 param2) {
Map super K2, ? extends V> 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 super K> 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