public final class IntMapperUtils
extends java.lang.Object
int types.| Modifier and Type | Method and Description |
|---|---|
static <T> java.util.function.Function<T,java.util.stream.IntStream> |
flatMapperToInt(java.util.function.Function<? super T,? extends int[]> toIntArrayMapper)
Given a
Function that takes an argument of type <T> and returns an int array,
this method builds a Function that takes the same argument, but returns an IntStream. |
static java.util.function.IntUnaryOperator |
intAdd(int toAdd)
Builds an
IntUnaryOperator that adds a constant value to an int parameter. |
static java.util.function.IntUnaryOperator |
intDivide(int divisor)
Builds an
IntUnaryOperator that divides its int parameter by a constant value. |
static java.util.function.IntFunction<java.util.stream.IntStream> |
intFlatMapper(java.util.function.IntFunction<? extends int[]> intMapper)
Given an
IntFunction that returns an int array, this method builds an
IntFunction that returns an IntStream. |
static <K,V> org.perro.functions.mapper.IntKeyValueMapper<K,V> |
intKeyValueMapper(java.util.function.IntFunction<K> keyMapper,
java.util.function.IntFunction<V> valueMapper)
Builds an object representing a pair of int functions, one to return a key in a
Map, and the other
to return its associated value. |
static <U,R> java.util.function.IntFunction<R> |
intMapper(java.util.function.BiFunction<java.lang.Integer,? super U,? extends R> biFunction,
U value)
Builds an
IntFunction from a passed BiFunction. |
static <R> java.util.function.IntFunction<R> |
intMapper(java.util.function.IntFunction<R> function)
Simply casts a method reference, which takes a single parameter of type
int and returns <R>,
to an IntFunction. |
static java.util.function.IntUnaryOperator |
intModulo(int divisor)
Builds an
IntUnaryOperator that divides its int parameter by a constant value to
get a remainder. |
static java.util.function.IntUnaryOperator |
intMultiply(int factor)
Builds an
IntUnaryOperator that multiplies a constant value with an int parameter. |
static <U> java.util.function.IntFunction<IntObjectPair<U>> |
intPairOf(java.util.function.IntFunction<? extends U> rightFunction)
Given an
IntFunction that returns a value of type <U>, this method builds an
IntFunction that returns a value of type IntObjectPair<U>. |
static <U,V> java.util.function.IntFunction<Pair<U,V>> |
intPairOf(java.util.function.IntFunction<? extends U> leftFunction,
java.util.function.IntFunction<? extends V> rightFunction)
Given a pair of int functions, one that returns a value of type <U>, and the other that returns a value of
type <V>, this method builds a
DoubleFunction that returns a value of type
Pair<U, V>. |
static <U,V> java.util.function.IntFunction<Pair<U,V>> |
intPairOf(org.perro.functions.mapper.IntKeyValueMapper<U,V> keyValueMapper)
Given an object consisting of a pair of int functions, one that returns a value of type <U>, and the
other that returns a value of type <V>, this method builds an
IntFunction that returns a value
of type Pair<U, V>. |
static <U,V> java.util.function.IntFunction<Pair<U,V>> |
intPairWith(java.util.function.IntFunction<? extends U> function,
java.util.List<V> pairedList)
Given an
IntFunction<U>, and a List<V>, this method builds an
IntFunction that returns a Pair<U, V>. |
static <R> java.util.function.IntFunction<IntObjectPair<R>> |
intPairWith(java.util.List<R> pairedList)
Given a
List<R>, this methods builds an IntFunction that returns an
IntObjectPair<R>. |
static java.util.function.IntFunction<IntIndexPair> |
intPairWithIndex()
Builds an
IntFunction that returns an object that represents a pair of values, one being the int
value itself, and the other a primitive zero-based index of the int in encounter order. |
static <R> java.util.function.IntFunction<Pair<R,java.lang.Integer>> |
intPairWithIndex(java.util.function.IntFunction<? extends R> function)
Given an
IntFunction that returns a value of type <R>, this method builds an
IntFunction that returns an object that represents a pair of values, one being a value returned
from the passed int function, and the other a primitive zero-based index of the int value in
encounter order. |
static java.util.function.IntUnaryOperator |
intSubtract(int toSubtract)
Builds an
IntUnaryOperator that subtracts a constant value from an int parameter. |
static <R> java.util.function.IntFunction<R> |
intTernary(java.util.function.IntPredicate predicate,
org.perro.functions.mapper.IntTernaryMapper<R> trueFalseMappers)
Given an IntPredicate and an object consisting of a pair of int functions, each returning a value of type
<R>, one to return a value if the predicate is true, the other returning an alternate value if the
predicate is false, this method builds an
IntFunction that evaluates the predicate and returns a
value produced by one or the other of the pair. |
static <R> org.perro.functions.mapper.IntTernaryMapper<R> |
intTrueFalseMappers(java.util.function.IntFunction<R> trueExtractor,
java.util.function.IntFunction<R> falseExtractor)
Builds an object representing a pair of functions, one to return a value if an int predicate evaluates to true,
the other to return an alternate value if it evaluates to false.
|
static <U,R> java.util.function.IntFunction<R> |
inverseIntMapper(java.util.function.BiFunction<? super U,java.lang.Integer,? extends R> biFunction,
U value)
Builds an
IntFunction from a passed BiFunction. |
static <T,U> java.util.function.ToIntFunction<T> |
inverseToIntMapper(java.util.function.ToIntBiFunction<? super U,? super T> biFunction,
U value)
Builds a
ToIntFunction from a passed ToIntBiFunction. |
static <T,U> java.util.function.ToIntFunction<T> |
toIntMapper(java.util.function.ToIntBiFunction<? super T,? super U> biFunction,
U value)
Builds a
ToIntFunction from a passed ToIntBiFunction. |
static <T> java.util.function.ToIntFunction<T> |
toIntMapper(java.util.function.ToIntFunction<T> function)
Simply casts a method reference, which takes a single parameter of type <T> and returns
int,
to a ToIntFunction. |
static <T> java.util.function.ToIntFunction<T> |
toIntMapperDefault(java.util.function.ToIntFunction<? super T> function,
int defaultValue)
Builds a mapper
ToIntFunction that, if the target element is null, or the result of the
Function call on the target element is null, then the passed default value is returned. |
public static <R> java.util.function.IntFunction<R> intMapper(java.util.function.IntFunction<R> function)
int and returns <R>,
to an IntFunction. Everything said about the MapperUtils.mapper(Function) method applies
here. The difference is that instead of an element of type <T> being streamed through, it would be a
primitive int instead. This method might be useful in a situation where you have an
IntStream, and the IntStream.mapToObj(IntFunction mapper) method is called to convert
the primitive to some generic type of object, converting the IntStream to an object stream.
Note that the difference between this method and toIntMapper(ToIntFunction) is that the
IntFunction built from this method takes an int and returns a generic type, where the
ToIntFunction built from toIntMapper(ToIntFunction) takes a generic type and returns an
int.
R - The type of the result of the IntFunction built by this method.function - A method reference to be cast to an IntFunction.public static <U,R> java.util.function.IntFunction<R> intMapper(java.util.function.BiFunction<java.lang.Integer,? super U,? extends R> biFunction,
U value)
IntFunction from a passed BiFunction. Everything said about the
MapperUtils.mapper(BiFunction, Object) method applies here. The difference is that instead of an element
of type <T> being streamed through, it would be a primitive int instead. This method might be
useful in a situation where you have an IntStream, and the
IntStream.mapToObj(IntFunction mapper) method is called to convert the primitive to some generic
type of object, converting the IntStream to an object stream.
Note that the difference between this method and toIntMapper(ToIntBiFunction, Object) is that the
IntFunction built from this method takes an int and returns a generic type, where the
ToIntFunction built from toIntMapper(ToIntBiFunction, Object) takes a generic type and
returns an int.
U - The type of the constant value to be passed as the second parameter to each invocation of
biFunction.R - The type of the result of the IntFunction built by this method.biFunction - A method reference which is a BiFunction, taking two parameters - the first of type int, and
the second of type <U>, which can be any type. The method reference will be converted by
this method to an IntFunction, taking a single parameter of type int. Behind the scenes, this
BiFunction will be called, passing the constant value to each invocation as the second
parameter.value - A constant value, in that it will be passed to every invocation of the passed biFunction as the
second parameter to it, and will have the same value for each of them.public static <U,R> java.util.function.IntFunction<R> inverseIntMapper(java.util.function.BiFunction<? super U,java.lang.Integer,? extends R> biFunction,
U value)
IntFunction from a passed BiFunction. Everything said about the
MapperUtils.inverseMapper(BiFunction, Object) method applies here. The difference is that instead of an
element of type <T> being streamed through, it would be a primitive int instead. This method
might be useful in a situation where you have an IntStream, and the
IntStream.mapToObj(IntFunction mapper) method is called to convert the primitive to some generic
type of object, converting the IntStream to an object stream.
Note that the difference between this method and inverseToIntMapper(ToIntBiFunction, Object) is that the
IntFunction built from this method takes an int and returns a generic type, where the
ToIntFunction built from inverseToIntMapper(ToIntBiFunction, Object) takes a generic type
and returns an int.
U - The type of the constant value to be passed as the first parameter to each invocation of
biFunction.R - The type of the result of the IntFunction built by this method.biFunction - A method reference which is a BiFunction, taking two parameters - the first of type <U>
which can be any type, and the second of type int. The method reference will be converted by
this method to an IntFunction, taking a single parameter of type int. Behind the scenes, this
biFunction will be called, passing the constant value to each invocation as the first
parameter.value - A constant value, in that it will be passed to every invocation of the passed biFunction as the
first parameter to it, and will have the same value for each of them.public static <T> java.util.function.ToIntFunction<T> toIntMapper(java.util.function.ToIntFunction<T> function)
int,
to a ToIntFunction. Everything said about the MapperUtils.mapper(Function) method applies
here. The difference is that instead of returning a result of a generic object type, it returns a primitive
int instead. This method might be useful in a situation where you have a Stream of a
generic object type, and the mapToInt(ToIntFunction mapper) method is called to convert the object
to a primitive int, converting the stream to an IntStream.
Note that the difference between this method and intMapper(IntFunction) is that the
ToIntFunction built from this method takes a generic type and returns an int, where the
IntFunction built from intMapper(IntFunction) takes an int and returns a
generic type.
T - The type of the single parameter to the ToIntFunction.function - A method reference to be cast to a ToIntFunction.public static <T> java.util.function.ToIntFunction<T> toIntMapperDefault(java.util.function.ToIntFunction<? super T> function,
int defaultValue)
ToIntFunction that, if the target element is null, or the result of the
Function call on the target element is null, then the passed default value is returned.
Everything said about the MapperUtils.mapperDefault(Function, Object) method applies here. The difference
is that instead of returning a result of a generic object type, it returns a primitive int instead.
This method might be useful in a situation where you have a Stream of a generic object type, and the
mapToInt(ToIntFunction mapper) method is called to convert the object to a primitive
int, converting the stream to an IntStream.T - The type of the target element on which the mapper ToIntFunction is to be called.function - A method reference which takes a single parameter of type <T>, and returns a value of
type int.defaultValue - A default value of type int, to be returned in case the target element, or the result of
the ToIntFunction call on the target element is null.public static <T,U> java.util.function.ToIntFunction<T> toIntMapper(java.util.function.ToIntBiFunction<? super T,? super U> biFunction,
U value)
ToIntFunction from a passed ToIntBiFunction. Everything said about the
MapperUtils.mapper(BiFunction, Object) method applies here. The difference is that instead of returning a
result of a generic object type, it returns a primitive int instead. This method might be useful in
a situation where you have a Stream of a generic object type, and the
mapToInt(ToIntFunction mapper) method is called to convert the object to a primitive
int, converting the stream to an IntStream.
Note that the difference between this method and intMapper(BiFunction, Object) is that the
ToIntFunction built from this method takes a generic type and returns an int,
where the IntFunction built from intMapper(BiFunction, Object) takes an int
and returns a generic type.
T - The type of the target element on which the mapper ToIntFunction is to be called.U - The type of the constant value to be passed as the second parameter to each invocation of
biFunction.biFunction - A method reference which is a ToIntBiFunction, taking two parameters - the first of type
<T>, and the second of type <U>, both of which can be any type. The method
reference will be converted by this method to a ToIntFunction, taking a single parameter of
type <T>. Behind the scenes, this ToIntBiFunction will be called, passing the constant
value to each invocation as the second parameter.value - A constant value, in that it will be passed to every invocation of the passed biFunction as the
second parameter to it, and will have the same value for each of them.public static <T,U> java.util.function.ToIntFunction<T> inverseToIntMapper(java.util.function.ToIntBiFunction<? super U,? super T> biFunction,
U value)
ToIntFunction from a passed ToIntBiFunction. Everything said about the
MapperUtils.inverseMapper(BiFunction, Object) method applies here. The difference is that instead of
returning a result of a generic object type, it returns a primitive int instead. This method might
be useful in a situation where you have a Stream of a generic object type, and the
mapToInt(ToIntFunction mapper) method is called to convert the object to a primitive
int, converting the stream to an IntStream.
Note that the difference between this method and inverseIntMapper(BiFunction, Object) is that the
ToIntFunction built from this method takes a generic type and returns an int, where the
IntFunction built from inverseIntMapper(BiFunction, Object) takes an int and
returns a generic type.
T - The type of the target element on which the mapper ToIntFunction is to be called.U - The type of the constant value to be passed as the first parameter to each invocation of
biFunction.biFunction - A method reference which is a ToIntBiFunction, taking two parameters - the first of type
<U> which can be any type, and the second of type int. The method reference will be
converted by this method to a ToIntFunction, taking a single parameter of type <T>.
Behind the scenes, this biFunction will be called, passing the constant value to each
invocation as the first parameter.value - A constant value, in that it will be passed to every invocation of the passed biFunction as the
first parameter to it, and will have the same value for each of them.public static java.util.function.IntFunction<java.util.stream.IntStream> intFlatMapper(java.util.function.IntFunction<? extends int[]> intMapper)
IntFunction that returns an int array, this method builds an
IntFunction that returns an IntStream. This is useful in the
IntStream.flatMap() method. For a very contrived example, let's say you have a method,
MathUtils.getFactors(int product), that takes an int value and returns an int array
containing the factors of that number. You have lower and upper bound int values to create a range, and you want
to sum the factors of all of the individual int values:
private int getSumOfAllFactors(int startInclusive, int endExclusive) {
return IntStream.range(startInclusive, endExclusive)
.flatMap(IntMapperUtils.intFlatMapper(MathUtils::getFactors))
.sum();
}
Or, with static imports:
private int getSumOfAllFactors(int startInclusive, int endExclusive) {
return IntStream.range(startInclusive, endExclusive)
.flatMap(intFlatMapper(MathUtils::getFactors))
.sum();
}
intMapper - An IntFunction that returns an array of ints.public static <T> java.util.function.Function<T,java.util.stream.IntStream> flatMapperToInt(java.util.function.Function<? super T,? extends int[]> toIntArrayMapper)
Function that takes an argument of type <T> and returns an int array,
this method builds a Function that takes the same argument, but returns an IntStream.
This is useful in the Stream.flatMapToInt() method. For example, let's say you have a collection of
objects representing all the orders for a particular customer. You want to total the quantities for each of the
line items in all of the orders contained in the collection:
private int getTotalLineItemQuantities(Collection<Order> customerOrders) {
return customerOrders.stream()
.flatMapToInt(IntMapperUtils.flatMapperToInt(this::getAllQuantities))
.sum();
}
private int[] getAllQuantities(Order order) {
...
}
Or, with static imports:
private int getTotalLineItemQuantities(Collection<Order> customerOrders) {
return customerOrders.stream()
.flatMapToInt(flatMapperToInt(this::getAllQuantities))
.sum();
}
T - The type of the argument to be passed to the given toIntArrayMapper function.toIntArrayMapper - A Function taking an argument of type <T>, that returns an array of ints.public static <U> java.util.function.IntFunction<IntObjectPair<U>> intPairOf(java.util.function.IntFunction<? extends U> rightFunction)
IntFunction that returns a value of type <U>, this method builds an
IntFunction that returns a value of type IntObjectPair<U>. This pair will consist
of the target int itself, and a value returned by the passed rightFunction.U - The type of the right element of the IntObjectPair to be returned by the IntFunction built
by this method.rightFunction - An IntFunction to extract the right value in the IntObjectPair<U> to be returned by
the IntFunction built by this method.public static <U,V> java.util.function.IntFunction<Pair<U,V>> intPairOf(org.perro.functions.mapper.IntKeyValueMapper<U,V> keyValueMapper)
IntFunction that returns a value
of type Pair<U, V>. This pair will consist of the values returned by each of the functions in
the passed keyValueMapper. This method does the same thing as the overload that takes a
leftFunction and rightFunction, and is included as a convenience when a method already
takes an IntKeyValueMapper. For example, the implementation of the
IntTransformUtils.intTransformToMap(int[], IntKeyValueMapper) method is:
public static <K, V> Map<K, V> intTransformToMap(int[] ints, IntKeyValueMapper<K, V> keyValueMapper) {
return defaultIntStream(ints)
.mapToObj(intPairOf(keyValueMapper))
.collect(toMapFromEntry());
}
This works because the Pair object implements the Java Map.Entry interface.U - The type of the left element of the Pair to be returned by the IntFunction built by this
method.V - The type of the right element of the Pair to be returned by the IntFunction built by this
method.keyValueMapper - An object consisting of a pair of int functions that will be used to retrieve a left and
right value for a Pair that is a result of the IntFunction built by this method.public static <U,V> java.util.function.IntFunction<Pair<U,V>> intPairOf(java.util.function.IntFunction<? extends U> leftFunction, java.util.function.IntFunction<? extends V> rightFunction)
DoubleFunction that returns a value of type
Pair<U, V>. This pair will consist of the values returned by each of the int functions passed
to this method.U - The type of the left element of the Pair to be returned by the IntFunction built by this
method.V - The type of the right element of the Pair to be returned by the IntFunction built by this
method.leftFunction - An IntFunction that will be used to retrieve a left value for a Pair that is a result of
the IntFunction built by this method.rightFunction - A IntFunction that will be used to retrieve a right value for a Pair that is a result of
the IntFunction built by this method.public static <R> java.util.function.IntFunction<IntObjectPair<R>> intPairWith(java.util.List<R> pairedList)
List<R>, this methods builds an IntFunction that returns an
IntObjectPair<R>. It is intended to be used in a stream. The
IntObjectPair<R> built by this IntFunction will consist of a target int, and an
object of type <R> whose element in the passed List is associated with the current int, in
encounter order. The function returned from this method is not intended to be used with parallel streams.
If the passed List has more elements than the array of ints being streamed, the extra elements are
ignored. If it has fewer elements, any target values that do not have associated values in the list, will be
paired with a null value.
R - The type of the elements in the passed pairedList parameter.pairedList - A List whose elements are to be paired with int array elements being streamed, by the
IntFunction built by this method.public static <U,V> java.util.function.IntFunction<Pair<U,V>> intPairWith(java.util.function.IntFunction<? extends U> function, java.util.List<V> pairedList)
IntFunction<U>, and a List<V>, this method builds an
IntFunction that returns a Pair<U, V>. It is intended to be used in a stream.
The Pair<U, V> built by this IntFunction will consist of an element returned
by the passed function, and an object of type <V> whose element in the passed
List is associated with the current int, in encounter order. The int function returned from this
method is not intended to be used with parallel streams.
If the passed List has more elements than the int array being streamed, the extra elements are
ignored. If it has fewer elements, any values returned by the passed int function, that do not
have associated values in the list, will be paired with a null value.
U - The type of the left element, retrieved by the passed function.V - The type of the right element, retrieved from the passed List.function - An IntFunction that will return a value of type <U>, which will become the left element
in a Pair, returned by the IntFunction built by this method.pairedList - A List whose elements are to be paired with elements retrieved by the passed int function.public static java.util.function.IntFunction<IntIndexPair> intPairWithIndex()
IntFunction that returns an object that represents a pair of values, one being the int
value itself, and the other a primitive zero-based index of the int in encounter order. The
IntFunction built by this method is intended to be used in a stream, but is not intended to
be used with parallel streams.public static <R> java.util.function.IntFunction<Pair<R,java.lang.Integer>> intPairWithIndex(java.util.function.IntFunction<? extends R> function)
IntFunction that returns a value of type <R>, this method builds an
IntFunction that returns an object that represents a pair of values, one being a value returned
from the passed int function, and the other a primitive zero-based index of the int value in
encounter order. The IntFunction built by this method is intended to be used in a stream, but is
not intended to be used with parallel streams.R - The type of a value retrieved from the passed int function.function - An IntFunction that returns a value of type <R>.public static <R> java.util.function.IntFunction<R> intTernary(java.util.function.IntPredicate predicate,
org.perro.functions.mapper.IntTernaryMapper<R> trueFalseMappers)
IntFunction that evaluates the predicate and returns a
value produced by one or the other of the pair. It may be difficult to think of an example where this may be
useful, but this method is included here for the sake of completeness.R - The type of the values returned by the pair of int functions in the passed
trueFalseMappers.predicate - An int predicate to be evaluated, determining which of the pair of double functions below
will return a resulting value.trueFalseMappers - An object consisting of a pair of int functions, one to return a value if the passed
predicate evaluates to true, and the other to return an alternate value if it evaluates
to false.public static <R> org.perro.functions.mapper.IntTernaryMapper<R> intTrueFalseMappers(java.util.function.IntFunction<R> trueExtractor,
java.util.function.IntFunction<R> falseExtractor)
intTernary(IntPredicate, IntTernaryMapper) method.R - The type of the value to be returned by the extractor methods below.trueExtractor - Retrieves a value to be returned by the
intTernary(IntPredicate, IntTernaryMapper) method when its predicate evaluates to
true.falseExtractor - Retrieves a value to be returned by the
intTernary(IntPredicate, IntTernaryMapper) method when its predicate evaluates to
false.public static <K,V> org.perro.functions.mapper.IntKeyValueMapper<K,V> intKeyValueMapper(java.util.function.IntFunction<K> keyMapper,
java.util.function.IntFunction<V> valueMapper)
Map, and the other
to return its associated value. This method is meant to be used to build the second parameter to the
IntTransformUtils.intTransformToMap(int[], IntKeyValueMapper) method.K - The type of a key value for a Map.V - The type of a value to be associated with a key in a Map.keyMapper - IntFunction to retrieve a value to be used as a key in a Map.valueMapper - IntFunction to retrieve a value associated with a key in a Map.public static java.util.function.IntUnaryOperator intAdd(int toAdd)
IntUnaryOperator that adds a constant value to an int parameter.toAdd - A constant int value to be added to the parameter of an IntUnaryOperator.public static java.util.function.IntUnaryOperator intSubtract(int toSubtract)
IntUnaryOperator that subtracts a constant value from an int parameter.toSubtract - A constant int value to be subtracted from the parameter of an IntUnaryOperator.public static java.util.function.IntUnaryOperator intMultiply(int factor)
IntUnaryOperator that multiplies a constant value with an int parameter.factor - A constant int value to be multiplied with the parameter of an IntUnaryOperator.public static java.util.function.IntUnaryOperator intDivide(int divisor)
IntUnaryOperator that divides its int parameter by a constant value.divisor - A constant int value to be divided into the parameter of an IntUnaryOperator.public static java.util.function.IntUnaryOperator intModulo(int divisor)
IntUnaryOperator that divides its int parameter by a constant value to
get a remainder.divisor - A constant int value to be divided into the parameter of an IntUnaryOperator to get a
remainder.