public final class LongMapperUtils
extends java.lang.Object
long types.| Modifier and Type | Method and Description |
|---|---|
static <T> java.util.function.Function<T,java.util.stream.LongStream> |
flatMapperToLong(java.util.function.Function<? super T,? extends long[]> toLongArrayMapper)
Given a
Function that takes an argument of type <T> and returns a long array, this
method builds a Function that takes the same argument, but returns a LongStream. |
static <U,R> java.util.function.LongFunction<R> |
inverseLongMapper(java.util.function.BiFunction<? super U,java.lang.Long,? extends R> biFunction,
U value)
Builds a
LongFunction from a passed BiFunction. |
static <T,U> java.util.function.ToLongFunction<T> |
inverseToLongMapper(java.util.function.ToLongBiFunction<? super U,? super T> biFunction,
U value)
Builds a
ToLongFunction from a passed ToLongBiFunction. |
static java.util.function.LongUnaryOperator |
longAdd(long toAdd)
Builds a
LongUnaryOperator that adds a constant value to a long parameter. |
static java.util.function.LongUnaryOperator |
longDivide(long divisor)
Builds a
LongUnaryOperator that divides its long parameter by a constant value. |
static java.util.function.LongFunction<java.util.stream.LongStream> |
longFlatMapper(java.util.function.LongFunction<? extends long[]> longMapper)
Given a
LongFunction that returns a long array, this method builds a
LongFunction that returns a LongStream. |
static <K,V> org.perro.functions.mapper.LongKeyValueMapper<K,V> |
longKeyValueMapper(java.util.function.LongFunction<K> keyMapper,
java.util.function.LongFunction<V> valueMapper)
Builds an object representing a pair of long functions, one to return a key in a
Map, and the other
to return its associated value. |
static <U,R> java.util.function.LongFunction<R> |
longMapper(java.util.function.BiFunction<java.lang.Long,? super U,? extends R> biFunction,
U value)
Builds a
LongFunction from a passed BiFunction. |
static <R> java.util.function.LongFunction<R> |
longMapper(java.util.function.LongFunction<R> function)
Simply casts a method reference, which takes a single parameter of type
long and returns <R>,
to a LongFunction. |
static java.util.function.LongUnaryOperator |
longModulo(long divisor)
Builds a
LongUnaryOperator that divides its long parameter by a constant value to
get a remainder. |
static java.util.function.LongUnaryOperator |
longMultiply(long factor)
Builds a
LongUnaryOperator that multiplies a constant value with a long parameter. |
static <U> java.util.function.LongFunction<LongObjectPair<U>> |
longPairOf(java.util.function.LongFunction<? extends U> rightFunction)
Given a
LongFunction that returns a value of type <U>, this method builds a
LongFunction that returns a value of type LongObjectPair<U>. |
static <U,V> java.util.function.LongFunction<Pair<U,V>> |
longPairOf(java.util.function.LongFunction<? extends U> leftFunction,
java.util.function.LongFunction<? extends V> rightFunction)
Given a pair of long functions, one that returns a value of type <U>, and the other that returns a value of
type <V>, this method builds a
LongFunction that returns a value of type
Pair<U, V>. |
static <U,V> java.util.function.LongFunction<Pair<U,V>> |
longPairOf(org.perro.functions.mapper.LongKeyValueMapper<U,V> keyValueMapper)
Given an object consisting of a pair of long functions, one that returns a value of type <U>, and the other
that returns a value of type <V>, this method builds a
LongFunction that returns a value of
type Pair<U, V>. |
static <R> java.util.function.LongFunction<LongObjectPair<R>> |
longPairWith(java.util.List<R> pairedList)
Given a
List<R>, this methods builds a LongFunction that returns a
LongObjectPair<R>. |
static <U,V> java.util.function.LongFunction<Pair<U,V>> |
longPairWith(java.util.function.LongFunction<? extends U> function,
java.util.List<V> pairedList)
Given a
LongFunction<U>, and a List<V>, this method builds a
LongFunction that returns a Pair<U, V>. |
static java.util.function.LongFunction<LongIndexPair> |
longPairWithIndex()
Builds a
LongFunction that returns an object that represents a pair of values, one being the long
value itself, and the other a primitive zero-based index of the long in encounter order. |
static <R> java.util.function.LongFunction<Pair<R,java.lang.Integer>> |
longPairWithIndex(java.util.function.LongFunction<? extends R> function)
Given a
LongFunction that returns a value of type <R>, this method builds a
LongFunction that returns an object that represents a pair of values, one being a value returned
from the passed long function, and the other a primitive zero-based index of the long value in
encounter order. |
static java.util.function.LongUnaryOperator |
longSubtract(long toSubtract)
Builds a
LongUnaryOperator that subtracts a constant value from a long parameter. |
static <R> java.util.function.LongFunction<R> |
longTernary(java.util.function.LongPredicate predicate,
org.perro.functions.mapper.LongTernaryMapper<R> trueFalseMappers)
Given a LongPredicate and an object consisting of a pair of long 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 a
LongFunction that evaluates the predicate and returns a
value produced by one or the other of the pair. |
static <R> org.perro.functions.mapper.LongTernaryMapper<R> |
longTrueFalseMappers(java.util.function.LongFunction<R> trueExtractor,
java.util.function.LongFunction<R> falseExtractor)
Builds an object representing a pair of functions, one to return a value if a long predicate evaluates to true,
the other to return an alternate value if it evaluates to false.
|
static <T,U> java.util.function.ToLongFunction<T> |
toLongMapper(java.util.function.ToLongBiFunction<? super T,? super U> biFunction,
U value)
Builds a
ToLongFunction from a passed ToLongBiFunction. |
static <T> java.util.function.ToLongFunction<T> |
toLongMapper(java.util.function.ToLongFunction<T> function)
Simply casts a method reference, which takes a single parameter of type <T> and returns
long,
to a ToLongFunction. |
static <T> java.util.function.ToLongFunction<T> |
toLongMapperDefault(java.util.function.ToLongFunction<? super T> function,
long defaultValue)
Builds a mapper
ToLongFunction 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.LongFunction<R> longMapper(java.util.function.LongFunction<R> function)
long and returns <R>,
to a LongFunction. 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 long instead. This method might be useful in a situation where you have a
LongStream, and the LongStream.mapToObj(LongFunction mapper) method is called to
convert the primitive to some generic type of object, converting the LongStream to an object stream.
Note that the difference between this method and toLongMapper(ToLongFunction) is that the
LongFunction built from this method takes a long and returns a generic type, where the
ToLongFunction built from toLongMapper(ToLongFunction) takes a generic type and returns a
long.
R - The type of the result of the LongFunction built by this method.function - A method reference to be cast to an LongFunction.public static <U,R> java.util.function.LongFunction<R> longMapper(java.util.function.BiFunction<java.lang.Long,? super U,? extends R> biFunction,
U value)
LongFunction 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 long instead. This method might be
useful in a situation where you have a LongStream, and the
LongStream.mapToObj(LongFunction mapper) method is called to convert the primitive to some generic
type of object, converting the LongStream to an object stream.
Note that the difference between this method and toLongMapper(ToLongBiFunction, Object) is that the
LongFunction built from this method takes a long and returns a generic type, where the
ToLongFunction built from toLongMapper(ToLongBiFunction, Object) takes a generic type and
returns a long.
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 LongFunction built by this method.biFunction - A method reference which is a BiFunction, taking two parameters - the first of type long, and
the second of type <U>, which can be any type. The method reference will be converted by
this method to a LongFunction, taking a single parameter of type long. 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.LongFunction<R> inverseLongMapper(java.util.function.BiFunction<? super U,java.lang.Long,? extends R> biFunction,
U value)
LongFunction 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 long instead. This method
might be useful in a situation where you have a LongStream, and the
LongStream.mapToObj(LongFunction mapper) method is called to convert the primitive to some generic
type of object, converting the LongStream to an object stream.
Note that the difference between this method and inverseToLongMapper(ToLongBiFunction, Object) is that
the LongFunction built from this method takes a long and returns a generic type, where
the ToLongFunction built from inverseToLongMapper(ToLongBiFunction, Object) takes a generic
type and returns a long.
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 LongFunction 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 long. The method reference will be converted by
this method to an LongFunction, taking a single parameter of type long. 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.ToLongFunction<T> toLongMapper(java.util.function.ToLongFunction<T> function)
long,
to a ToLongFunction. 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
long instead. This method might be useful in a situation where you have a Stream of a
generic object type, and the mapToLong(ToLongFunction mapper) method is called to convert the object
to a primitive long, converting the stream to a LongStream.
Note that the difference between this method and longMapper(LongFunction) is that the
ToLongFunction built from this method takes a generic type and returns a long, where
the LongFunction built from longMapper(LongFunction) takes a long and returns
a generic type.
T - The type of the single parameter to the ToLongFunction.function - A method reference to be cast to a ToLongFunction.public static <T> java.util.function.ToLongFunction<T> toLongMapperDefault(java.util.function.ToLongFunction<? super T> function,
long defaultValue)
ToLongFunction 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
long instead. This method might be useful in a situation where you have a Stream of a
generic object type, and the mapToLong(ToLongFunction mapper) method is called to convert the object
to a primitive long, converting the stream to a LongStream.T - The type of the target element on which the mapper ToLongFunction is to be called.function - A method reference which takes a single parameter of type <T>, and returns a value of
type long.defaultValue - A default value of type long, to be returned in case the target element, or the result of
the ToLongFunction call on the target element is null.public static <T,U> java.util.function.ToLongFunction<T> toLongMapper(java.util.function.ToLongBiFunction<? super T,? super U> biFunction,
U value)
ToLongFunction from a passed ToLongBiFunction. 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 long instead. This method might be useful in
a situation where you have a Stream of a generic object type, and the
mapToLong(ToLongFunction mapper) method is called to convert the object to a primitive
long, converting the stream to a LongStream.
Note that the difference between this method and longMapper(BiFunction, Object) is that the
ToLongFunction built from this method takes a generic type and returns a long, where
the LongFunction built from longMapper(BiFunction, Object) takes a long and
returns a generic type.
T - The type of the target element on which the mapper ToLongFunction 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 ToLongBiFunction, 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 ToLongFunction, taking a single parameter of
type <T>. Behind the scenes, this ToLongBiFunction 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.ToLongFunction<T> inverseToLongMapper(java.util.function.ToLongBiFunction<? super U,? super T> biFunction,
U value)
ToLongFunction from a passed ToLongBiFunction. 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 long instead. This method might
be useful in a situation where you have a Stream of a generic object type, and the
mapToLong(ToLongFunction mapper) method is called to convert the object to a primitive
long, converting the stream to a LongStream.
Note that the difference between this method and inverseLongMapper(BiFunction, Object) is that the
ToLongFunction built from this method takes a generic type and returns a long, where
the LongFunction built from inverseLongMapper(BiFunction, Object) takes a long
and returns a generic type.
T - The type of the target element on which the mapper ToLongFunction 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 ToLongBiFunction, taking two parameters - the first of type
<U> which can be any type, and the second of type long. The method reference will be
converted by this method to a ToLongFunction, 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.LongFunction<java.util.stream.LongStream> longFlatMapper(java.util.function.LongFunction<? extends long[]> longMapper)
LongFunction that returns a long array, this method builds a
LongFunction that returns a LongStream. This is useful in the
LongStream.flatMap() method. For a very contrived example, let's say you have a method,
MathUtils.getFactors(long product), that takes a long value and returns a long array
containing the factors of that number. You have lower and upper bound long values to create a range, and you want
to sum the factors of all of the individual long values:
private long getSumOfAllFactors(long startInclusive, long endExclusive) {
return LongStream.range(startInclusive, endExclusive)
.flatMap(LongMapperUtils.longFlatMapper(MathUtils::getFactors))
.sum();
}
Or, with static imports:
private long getSumOfAllFactors(long startInclusive, long endExclusive) {
return LongStream.range(startInclusive, endExclusive)
.flatMap(longFlatMapper(MathUtils::getFactors))
.sum();
}
longMapper - A LongFunction that returns an array of longs.public static <T> java.util.function.Function<T,java.util.stream.LongStream> flatMapperToLong(java.util.function.Function<? super T,? extends long[]> toLongArrayMapper)
Function that takes an argument of type <T> and returns a long array, this
method builds a Function that takes the same argument, but returns a LongStream. This is
useful in the Stream.flatMapToLong() 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 long getTotalLineItemQuantities(Collection<Order> customerOrders) {
return customerOrders.stream()
.flatMapToLong(LongMapperUtils.flatMapperToLong(this::getAllQuantities))
.sum();
}
private long[] getAllQuantities(Order order) {
...
}
Or, with static imports:
private long getTotalLineItemQuantities(Collection<Order> customerOrders) {
return customerOrders.stream()
.flatMapToLong(flatMapperToLong(this::getAllQuantities))
.sum();
}
T - The type of the argument to be passed to the given toLongArrayMapper function.toLongArrayMapper - A Function taking an argument of type <T>, that returns an array of longs.public static <U> java.util.function.LongFunction<LongObjectPair<U>> longPairOf(java.util.function.LongFunction<? extends U> rightFunction)
LongFunction that returns a value of type <U>, this method builds a
LongFunction that returns a value of type LongObjectPair<U>. This pair will
consist of the target long itself, and a value returned by the passed rightFunction.U - The type of the right element of the LongObjectPair to be returned by the LongFunction
built by this method.rightFunction - A LongFunction to extract the right value in the LongObjectPair<U> to be returned
by the LongFunction built by this method.public static <U,V> java.util.function.LongFunction<Pair<U,V>> longPairOf(org.perro.functions.mapper.LongKeyValueMapper<U,V> keyValueMapper)
LongFunction 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 a LongKeyValueMapper. For example, the implementation of the
LongTransformUtils.longTransformToMap(long[], LongKeyValueMapper) method is:
public static <K, V> Map<K, V> longTransformToMap(long[] longs, LongKeyValueMapper<K, V> keyValueMapper) {
return defaultLongStream(longs)
.mapToObj(longPairOf(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 LongFunction built by this
method.V - The type of the right element of the Pair to be returned by the LongFunction built by this
method.keyValueMapper - An object consisting of a pair of long functions that will be used to retrieve a left and
right value for a Pair that is a result of the LongFunction built by this method.public static <U,V> java.util.function.LongFunction<Pair<U,V>> longPairOf(java.util.function.LongFunction<? extends U> leftFunction, java.util.function.LongFunction<? extends V> rightFunction)
LongFunction that returns a value of type
Pair<U, V>. This pair will consist of the values returned by each of the long functions passed
to this method.U - The type of the left element of the Pair to be returned by the LongFunction built by this
method.V - The type of the right element of the Pair to be returned by the LongFunction built by this
method.leftFunction - A LongFunction that will be used to retrieve a left value for a Pair that is a result of
the LongFunction built by this method.rightFunction - A LongFunction that will be used to retrieve a right value for a Pair that is a result of
the LongFunction built by this method.public static <R> java.util.function.LongFunction<LongObjectPair<R>> longPairWith(java.util.List<R> pairedList)
List<R>, this methods builds a LongFunction that returns a
LongObjectPair<R>. It is intended to be used in a stream. The
LongObjectPair<R> built by this LongFunction will consist of a target long, and
an object of type <R> whose element in the passed List is associated with the current long, 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 longs 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 long array elements being streamed, by the
LongFunction built by this method.public static <U,V> java.util.function.LongFunction<Pair<U,V>> longPairWith(java.util.function.LongFunction<? extends U> function, java.util.List<V> pairedList)
LongFunction<U>, and a List<V>, this method builds a
LongFunction that returns a Pair<U, V>. It is intended to be used in a stream.
The Pair<U, V> built by this LongFunction 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 long, in encounter order. The long function returned from
this method is not intended to be used with parallel streams.
If the passed List has more elements than the long array being streamed, the extra elements are
ignored. If it has fewer elements, any values returned by the passed long 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 - A LongFunction that will return a value of type <U>, which will become the left element
in a Pair, returned by the LongFunction built by this method.pairedList - A List whose elements are to be paired with elements retrieved by the passed long function.public static java.util.function.LongFunction<LongIndexPair> longPairWithIndex()
LongFunction that returns an object that represents a pair of values, one being the long
value itself, and the other a primitive zero-based index of the long in encounter order. The
LongFunction 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.LongFunction<Pair<R,java.lang.Integer>> longPairWithIndex(java.util.function.LongFunction<? extends R> function)
LongFunction that returns a value of type <R>, this method builds a
LongFunction that returns an object that represents a pair of values, one being a value returned
from the passed long function, and the other a primitive zero-based index of the long value in
encounter order. The LongFunction 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 long function.function - A LongFunction that returns a value of type <R>.public static <R> java.util.function.LongFunction<R> longTernary(java.util.function.LongPredicate predicate,
org.perro.functions.mapper.LongTernaryMapper<R> trueFalseMappers)
LongFunction 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 long functions in the passed
trueFalseMappers.predicate - A long predicate to be evaluated, determining which of the pair of long functions below
will return a resulting value.trueFalseMappers - An object consisting of a pair of long 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.LongTernaryMapper<R> longTrueFalseMappers(java.util.function.LongFunction<R> trueExtractor,
java.util.function.LongFunction<R> falseExtractor)
longTernary(LongPredicate, LongTernaryMapper) method.R - The type of the value to be returned by the extractor methods below.trueExtractor - Retrieves a value to be returned by the
longTernary(LongPredicate, LongTernaryMapper) method when its predicate evaluates
to true.falseExtractor - Retrieves a value to be returned by the
longTernary(LongPredicate, LongTernaryMapper) method when its predicate evaluates
to false.public static <K,V> org.perro.functions.mapper.LongKeyValueMapper<K,V> longKeyValueMapper(java.util.function.LongFunction<K> keyMapper,
java.util.function.LongFunction<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
LongTransformUtils.longTransformToMap(long[], LongKeyValueMapper) 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 - LongFunction to retrieve a value to be used as a key in a Map.valueMapper - LongFunction to retrieve a value associated with a key in a Map.public static java.util.function.LongUnaryOperator longAdd(long toAdd)
LongUnaryOperator that adds a constant value to a long parameter.toAdd - A constant long value to be added to the parameter of a LongUnaryOperator.public static java.util.function.LongUnaryOperator longSubtract(long toSubtract)
LongUnaryOperator that subtracts a constant value from a long parameter.toSubtract - A constant long value to be subtracted from the parameter of a LongUnaryOperator.public static java.util.function.LongUnaryOperator longMultiply(long factor)
LongUnaryOperator that multiplies a constant value with a long parameter.factor - A constant long value to be multiplied with the parameter of a LongUnaryOperator.public static java.util.function.LongUnaryOperator longDivide(long divisor)
LongUnaryOperator that divides its long parameter by a constant value.divisor - A constant long value to be divided into the parameter of a LongUnaryOperator.public static java.util.function.LongUnaryOperator longModulo(long divisor)
LongUnaryOperator that divides its long parameter by a constant value to
get a remainder.divisor - A constant long value to be divided into the parameter of a LongUnaryOperator to get a
remainder.