Class StreamUtils
java.lang.Object
com.thanlinardos.spring_enterprise_library.objects.utils.StreamUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) Will filter items in distinct call by key instead of using equals or hashcode methods.static <T> Predicate<T> duplicateByKey(Function<? super T, ?> keyExtractor) Will filter items for duplicates by key.static <T,R> Predicate <T> filterExactlyOneByKey(Function<? super T, R> keyExtractor, String errorMessage, String... strings) Applies a filter to the stream, which ensures that based on the keyExtractor only one element is present.static <T,R> Predicate <T> filterExactlyOneByKey(Function<? super T, R> keyExtractor, Function<R, String> errorFactory, String... strings) Applies a filter to the stream, which ensures that based on the keyExtractor only one element is present.static <T> Collector<T, ?, T> findExactlyOne(String message, String... strings) Finds a single object.static <T> Collector<T, ?, T> findExactlyOne(String message, Supplier<String[]> stringSupplier) Finds a single object.findExactlyOneOrNone(ErrorCode errorCode, String message, String... strings) Finds either a single object or no object.findExactlyOneOrNone(String message, String... strings) Finds either a single object or no object.findExactlyOneOrNone(String message, Supplier<String[]> stringSupplier) Finds either a single object or no object.Finds the object if there is only a single element in the stream, otherwise if finds nothing.getDebugStringSuppliers(T... items) Returns a Supplier of Strings, derived viaObjectUtils.getDebugString(Object), based on the given object instances.isEmpty()Collects the result of the stream and appliesCollectionUtils.isEmpty(Collection)to itCollects the result of the stream and appliesCollectionUtils.isNotEmpty(Collection)to it.static <T,R> Stream <R> of(T object, Function<T, Collection<R>> extractor) Gets the stream of collection from a non-null objectstatic <T> Stream<T> ofNullable(Collection<T> collection) Gets the stream of the collection or empty stream if the collection is null.static <T> Stream<T> ofNullable(T[] collection) Gets the stream of the array or empty stream if the array is null.static <T,R> Stream <R> ofNullable(T object, Function<T, Collection<R>> extractor) Gets the stream of a collection from the object or empty stream if the collection or object is null.removingDuplicates(Comparator<T> comparator) Removes duplicates in a stream based on a givenComparator
-
Constructor Details
-
StreamUtils
public StreamUtils()
-
-
Method Details
-
distinctByKey
Will filter items in distinct call by key instead of using equals or hashcode methods.Example filtering entities by ID in a list to get a list of distinct entities:
list.stream().filter(StreamUtils.distinctByKey(it -> it.getId())).collect(Collectors.toList())- Type Parameters:
T- The type of the input to the predicate.- Parameters:
keyExtractor- The function used to extract the key that should be applied- Returns:
- A predicate that filters duplicates
-
duplicateByKey
Will filter items for duplicates by key. Will return multiple duplicates if they exist for the same key, if you only want the first duplicate, then use distinctByKey after.Example filtering entities by ID in a list to get a list of duplicates:
list.stream().filter(StreamUtils.duplicateByKey(it -> it.getId())).collect(Collectors.toList())Example filtering entities by ID in a list to get a list of distinct duplicates:
list.stream().filter(StreamUtils.duplicateByKey(it -> it.getId())).filter(StreamUtils.distinctByKey(it -> it.getId())).collect(Collectors.toList())
or
list.stream().filter(StreamUtils.duplicateByKey(it -> it.getId())).collect(Collectors.toSet())- Type Parameters:
T- The type of the input to the predicate.- Parameters:
keyExtractor- The function used to extract the key that should be applied- Returns:
- A predicate that filters uniques
-
findExactlyOneOrNone
public static <T> Collector<T,?, findExactlyOneOrNoneOptional<T>> (String message, String... strings) Finds either a single object or no object. Throws a MORE_THAN_ONE_FOUND exception if there are more than one object.- Type Parameters:
T- the element type.- Parameters:
message- the error message.strings- the strings to pass to the core exception method.- Returns:
- optional which either contains one element or is empty.
-
removingDuplicates
Removes duplicates in a stream based on a givenComparator- Type Parameters:
T- the element type.- Parameters:
comparator- the comparator used to determine whether something is a duplicate- Returns:
- list of non-duplicates.
-
findExactlyOneOrNone
public static <T> Collector<T,?, findExactlyOneOrNoneOptional<T>> (String message, Supplier<String[]> stringSupplier) Finds either a single object or no object. Throws a exception based on the errorCode if there are more than one object.- Type Parameters:
T- element type- Parameters:
message- the error message.stringSupplier- supplier of the strings to pass to the core exception method.- Returns:
- optional which either contains one element or is empty.
-
findExactlyOneOrNone
public static <T> Collector<T,?, findExactlyOneOrNoneOptional<T>> (ErrorCode errorCode, String message, String... strings) Finds either a single object or no object. Throws a exception based on the errorCode if there are more than one object.- Type Parameters:
T- the element type.- Parameters:
errorCode- the error code.message- the error message.strings- the strings to pass to the core exception method.- Returns:
- optional which either contains one element or is empty.
-
findExactlyOne
Finds a single object. Throws a MORE_THAN_ONE_FOUND exception if there are more than one object. Throws a NONE_FOUND exception if no objects are in the stream.- Type Parameters:
T- the element type.- Parameters:
message- the error message.strings- the strings to pass to the core exception method.- Returns:
- the single element.
-
findExactlyOne
public static <T> Collector<T,?, findExactlyOneT> (String message, Supplier<String[]> stringSupplier) Finds a single object. Throws a MORE_THAN_ONE_FOUND exception if there are more than one object. Throws a NONE_FOUND exception if no objects are in the stream.- Type Parameters:
T- the element type.- Parameters:
message- the error message.stringSupplier- the supplier of the strings to pass to the core exception method.- Returns:
- the single element.
-
filterExactlyOneByKey
public static <T,R> Predicate<T> filterExactlyOneByKey(Function<? super T, R> keyExtractor, String errorMessage, String... strings) Applies a filter to the stream, which ensures that based on the keyExtractor only one element is present. If two elements with the same key is present, it throws MORE_THAN_ONE_FOUND exception.- Type Parameters:
T- the type of the input to the predicate.R- the type of the key extracted.- Parameters:
keyExtractor- the function used to extract the key that should be appliederrorMessage- the function used to create the error message for the exception.strings- the strings to pass to the core exception method.- Returns:
- A predicate that filters throws exception on duplicates
-
filterExactlyOneByKey
public static <T,R> Predicate<T> filterExactlyOneByKey(Function<? super T, R> keyExtractor, Function<R, String> errorFactory, String... strings) Applies a filter to the stream, which ensures that based on the keyExtractor only one element is present. If two elements with the same key is present, it throws MORE_THAN_ONE_FOUND exception.- Type Parameters:
T- the type of the input to the predicate.R- the type of the key extracted.- Parameters:
keyExtractor- the function used to extract the key that should be appliederrorFactory- the function used to create the error message for the exception.strings- the strings to pass to the core exception method.- Returns:
- A predicate that filters throws exception on duplicates
-
findExactlyOneOtherwiseNone
Finds the object if there is only a single element in the stream, otherwise if finds nothing. This method returns an empty optional if the stream contains more than one element. If it should throw an exception on more than one element usefindExactlyOneOrNone(String, String...)instead.- Type Parameters:
T- the element type.- Returns:
- optional which either contains one element or is empty.
-
isEmpty
Collects the result of the stream and appliesCollectionUtils.isEmpty(Collection)to it- Type Parameters:
T- the element type.- Returns:
- Collector with true if the stream contains no elements otherwise false
-
isNotEmpty
Collects the result of the stream and appliesCollectionUtils.isNotEmpty(Collection)to it.- Type Parameters:
T- the element type.- Returns:
- Collector with true if the stream contains any elements, otherwise false
-
ofNullable
Gets the stream of the collection or empty stream if the collection is null.- Type Parameters:
T- the element type.- Parameters:
collection- the collection which could be null.- Returns:
- either stream of the collection or else empty stream.
-
ofNullable
Gets the stream of the array or empty stream if the array is null.- Type Parameters:
T- the element type.- Parameters:
collection- the collection which could be null.- Returns:
- either stream of the collection or else empty stream.
-
ofNullable
Gets the stream of a collection from the object or empty stream if the collection or object is null.- Type Parameters:
T- the type of the object.R- the element type of the collection.- Parameters:
object- the object to extract the collection from.extractor- the function to extract the collection from the object.- Returns:
- either stream of the collection or else empty stream.
-
of
Gets the stream of collection from a non-null object- Type Parameters:
T- the type of the object.R- the element type of the collection.- Parameters:
object- the object to extract the collection from.extractor- the function to extract the collection from the object- Returns:
- either stream of the collection or else empty stream.
- Throws:
NullPointerException- if input is null
-
getDebugStringSuppliers
Returns a Supplier of Strings, derived viaObjectUtils.getDebugString(Object), based on the given object instances.- Type Parameters:
T- object type- Parameters:
items- objects- Returns:
- a supplier of strings for the given objects
-