Class CompatUtil
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Stream<T>iterateStream(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next) Returns a sequential orderedStreamproduced by iterative application of the givennextfunction to an initial element, conditioned on satisfying the givenhasNextpredicate.static <T> List<T>listOf(T... items) Creates anArrayListthat contains the given items in the same order.static StringRepeats the given string a number of times.static <T> Set<T>setOf(T item) Creates aHashSetthat contains the given item.static <T> Set<T>setOf(T... items) Creates aHashSetthat contains the given items.static <T> Set<T>setOf(T itemA, T itemB) Creates aHashSetthat contains the given items.
-
Constructor Details
-
CompatUtil
public CompatUtil()
-
-
Method Details
-
repeat
Repeats the given string a number of times. From https://stackoverflow.com/a/1235213- Parameters:
str- The string to repeatn- The number of times to repeat the string- Returns:
- The given string repeated n times
-
setOf
Creates aHashSetthat contains the given items. The heap pollution warning is suppressed as it is inArrays.- Type Parameters:
T- The type of the items- Parameters:
items- The items to add to the set- Returns:
- The set with the given items
-
setOf
Creates aHashSetthat contains the given item.- Type Parameters:
T- The type of the item- Parameters:
item- The item to add to the set- Returns:
- The set with the given item
-
setOf
Creates aHashSetthat contains the given items.- Type Parameters:
T- The type of the items- Parameters:
itemA- The first item to add to the setitemB- The second item to add to the set- Returns:
- The set with the given items
-
listOf
Creates anArrayListthat contains the given items in the same order. This is likely cheaper for making a collection than usingsetOf(Object...).- Type Parameters:
T- The type of the items- Parameters:
items- The items to create the list out of- Returns:
- The list with the given items
-
iterateStream
public static <T> Stream<T> iterateStream(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next) Returns a sequential orderedStreamproduced by iterative application of the givennextfunction to an initial element, conditioned on satisfying the givenhasNextpredicate. The stream terminates as soon as thehasNextpredicate returns false.Stream.iterateshould produce the same sequence of elements as produced by the corresponding for-loop:for (T index=seed; hasNext.test(index); index = next.apply(index)) { ... }The resulting sequence may be empty if the
hasNextpredicate does not hold on the seed value. Otherwise the first element will be the suppliedseedvalue, the next element (if present) will be the result of applying thenextfunction to theseedvalue, and so on iteratively until thehasNextpredicate indicates that the stream should terminate.The action of applying the
hasNextpredicate to an element happens-before the action of applying thenextfunction to that element. The action of applying thenextfunction for one element happens-before the action of applying thehasNextpredicate for subsequent elements. For any given element an action may be performed in whatever thread the library chooses.- Type Parameters:
T- the type of stream elements- Parameters:
seed- the initial elementhasNext- a predicate to apply to elements to determine when the stream must terminate.next- a function to be applied to the previous element to produce a new element- Returns:
- a new sequential
Stream - Implementation Note:
- The implementation was taken from the JDK 9 source code.
-