saker.build Documentation TaskDoc JavaDoc Packages
public final class Functionals
Utility class containing various helper functions for dealing with functional interfaces.

Every object returned from the functions in this class is serializable, unless noted otherwise. They are often not serializable, if they're dealing with stateful objects. (E.g. with iterators, enumerations)

Methods
public static <T> Predicate<T>
public static <T, E> Function<T, ArrayList<E>>
Gets computer that creates an ArrayList regardless of the argument it applies to.
public static <T, K, V> Function<T, ConcurrentHashMap<K, V>>
Gets computer that creates an ConcurrentHashMap regardless of the argument it applies to.
public static <T, K, V> Function<T, ConcurrentSkipListMap<K, V>>
Gets computer that creates an ConcurrentSkipListMap regardless of the argument it applies to.
public static <T, E> Function<T, ConcurrentSkipListSet<E>>
Gets computer that creates an ConcurrentSkipListSet regardless of the argument it applies to.
public static <T> Supplier<T>
defaultedSupplier(Supplier<extends T> supplier, T defaultvalue)
Creates a supplier that will return the specified default value if the supplier returns null.
public static <K, V, E extends Entry<K, V>> Comparator<E>
entryKeyComparator(Comparator<super K> comparator)
Converts the argument comparator to a Map.Entry key comparator.
public static <K, V, E extends Entry<K, V>> Comparator<E>
Gets a comparator functional that compares its Map.Entry argument by the natural order of their keys.
public static <T, K extends Enum<K>, V> Function<T, EnumMap<K, V>>
enumMapComputer(Class<K> enumtype)
Gets a computer that creates an EnumMap regardless of the argument it applies to.
public static <T, E extends Enum<E>> Function<T, EnumSet<E>>
enumSetComputer(Class<E> enumtype)
Gets a computer that creates an EnumSet regardless of the argument it applies to.
public static <T, K, V> Function<T, HashMap<K, V>>
Gets computer that creates an HashMap regardless of the argument it applies to.
public static <T, E> Function<T, HashSet<E>>
Gets computer that creates an HashSet regardless of the argument it applies to.
public static <T> Predicate<T>
Gets a predicate that compares its argument to this function argument by identity.
public static <T extends R, R> Function<T, R>
public static <T, K, V> Function<T, IdentityHashMap<K, V>>
Gets computer that creates an IdentityHashMap regardless of the argument it applies to.
public static <T, E> Function<T, Set<E>>
Gets computer that creates an identity hash set regardless of the argument it applies to.
public static <T, K, V> Function<T, LinkedHashMap<K, V>>
Gets computer that creates an LinkedHashMap regardless of the argument it applies to.
public static <T, E> Function<T, LinkedHashSet<E>>
Gets computer that creates an LinkedHashSet regardless of the argument it applies to.
public static <T> Predicate<T>
public static <T, U> BiConsumer<T, U>
public static <T, U, R> BiFunction<T, U, R>
public static <T> Consumer<T>
public static <T, R> Function<T, R>
public static Runnable
public static <T> Supplier<T>
public static <T extends Comparable<super T>> Comparator<T>
Gets a comparator that sorts its non-null arguments by natural order, else the null values first.
public static <T extends Comparable<super T>> Comparator<T>
Gets a comparator that sorts its non-null arguments by natural order, else the null values last.
public static <T> Function<T, Object>
Gets computer that creates an Object regardless of the argument it applies to.
public static <T> Function<Integer, ArrayList<T>>
Gets a function that creates an ArrayList with for the capacity it is applied to.
public static <T> Function<Integer, List<T>>
Gets a function that creates a list with for the capacity it is applied to.
public static Runnable
Converts the argument throwing runnable to a simple Runnable by rethrowing any exceptions sneakily.
public static <K, T> Function<K, T>
toComputer(Supplier<extends T> supplier)
Converts the argument Supplier to a computer function.
public static <T> Iterable<T>
toIterable(Supplier<extends Iterator<T>> itsupplier)
Converts a supplier of iterators to an Iterable.
public static <T> Supplier<T>
toLockedRemovingSupplier(Iterator<extends T> iterator)
Converts the argument iterator to a Supplier that removes the elements after retrieving them.
public static <T> Supplier<T>
toLockedSupplier(Enumeration<extends T> enumeration)
Converts the argument enumeration to a Supplier.
public static <T> Supplier<T>
toLockedSupplier(Iterator<extends T> iterator)
Converts the argument iterator to a Supplier.
public static <T> Supplier<T>
toRemovingSupplier(Iterator<extends T> iterator)
Converts the argument iterator to a Supplier that removes the elements after retrieving them.
public static <T> Supplier<T>
toSupplier(Enumeration<extends T> enumeration)
Converts the argument enumeration to a Supplier.
public static <T> Supplier<T>
toSupplier(Iterator<extends T> iterator)
Converts the argument iterator to a Supplier.
public static <T> Supplier<T>
Converts the argument iterator to a Supplier that removes the elements after retrieving them.
public static <T> Supplier<T>
toSynchronizedSupplier(Enumeration<extends T> enumeration)
Converts the argument enumeration to a Supplier.
public static <T> Supplier<T>
toSynchronizedSupplier(Iterator<extends T> iterator)
Converts the argument iterator to a Supplier.
public static <T, K, V> Function<T, TreeMap<K, V>>
Gets computer that creates an TreeMap regardless of the argument it applies to.
public static <T, E> Function<T, TreeSet<E>>
Gets computer that creates an TreeSet regardless of the argument it applies to.
public static <T> Supplier<T>
Gets a Supplier that always returns the same value.
public static <T> Predicate<T> alwaysPredicate()
TThe type of the predicate argument.
A Predicate that always returns true.
public static <T, E> Function<T, ArrayList<E>> arrayListComputer()
Gets computer that creates an ArrayList regardless of the argument it applies to.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
EThe element type.
The computer.
public static <T, K, V> Function<T, ConcurrentHashMap<K, V>> concurrentHashMapComputer()
Gets computer that creates an ConcurrentHashMap regardless of the argument it applies to.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
KThe key type.
VThe value type.
The computer.
Gets computer that creates an ConcurrentSkipListMap regardless of the argument it applies to.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
KThe key type.
VThe value type.
The computer.
Gets computer that creates an ConcurrentSkipListSet regardless of the argument it applies to.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
EThe element type.
The computer.
public static <T> Supplier<T> defaultedSupplier(Supplier<extends T> supplier, T defaultvalue)
Creates a supplier that will return the specified default value if the supplier returns null.

The returned supplier will return the result of the argument supplier if non-null, else the specified default value. If the supplier argument is null, the default value will always be returned.

TThe type of the supplied object.
supplierThe supplier.
defaultvalueThe default value to use.
The defaulting supplier.
public static <K, V, E extends Entry<K, V>> Comparator<E> entryKeyComparator(Comparator<super K> comparator)
Converts the argument comparator to a Map.Entry key comparator.

If the argument is null, entryKeyNaturalComparator() is returned.

KThe key type.
VThe value type.
EThe map entry type.
comparatorThe key comparator.
A comparator that compares the entries by their keys using the argument comparator.
public static <K, V, E extends Entry<K, V>> Comparator<E> entryKeyNaturalComparator()
Gets a comparator functional that compares its Map.Entry argument by the natural order of their keys.
KThe key type.
VThe value type.
EThe map entry type.
The comparator.
public static <T, K extends Enum<K>, V> Function<T, EnumMap<K, V>> enumMapComputer(Class<K> enumtype)
Gets a computer that creates an EnumMap regardless of the argument it applies to.

The enum map will have the argument enum class as its key type.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
KThe key enum type.
VThe value type.
The computer.
public static <T, E extends Enum<E>> Function<T, EnumSet<E>> enumSetComputer(Class<E> enumtype)
Gets a computer that creates an EnumSet regardless of the argument it applies to.

The enum set will have the argument enum class as its element type.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
EThe enum type.
enumtypeThe enum class.
The computer.
public static <T, K, V> Function<T, HashMap<K, V>> hashMapComputer()
Gets computer that creates an HashMap regardless of the argument it applies to.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
KThe key type.
VThe value type.
The computer.
public static <T, E> Function<T, HashSet<E>> hashSetComputer()
Gets computer that creates an HashSet regardless of the argument it applies to.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
EThe element type.
The computer.
public static <T> Predicate<T> identityEqualsPredicate(Object o)
Gets a predicate that compares its argument to this function argument by identity.
TThe type that the predicate tests.
oThe object to identity compare the tested object to.
The predicate.
public static <T extends R, R> Function<T, R> identityFunction()
TThe type of the function argument.
RThe type of the function result.
A Function that returns the argument it applies to.
public static <T, K, V> Function<T, IdentityHashMap<K, V>> identityHashMapComputer()
Gets computer that creates an IdentityHashMap regardless of the argument it applies to.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
KThe key type.
VThe value type.
The computer.
public static <T, E> Function<T, Set<E>> identityHashSetComputer()
Gets computer that creates an identity hash set regardless of the argument it applies to.

The identity hash set is created by calling ObjectUtils.setFromMap(new IdentityHashMap<>()).

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
EThe element type.
The computer.
public static <T, K, V> Function<T, LinkedHashMap<K, V>> linkedHashMapComputer()
Gets computer that creates an LinkedHashMap regardless of the argument it applies to.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
KThe key type.
VThe value type.
The computer.
public static <T, E> Function<T, LinkedHashSet<E>> linkedHashSetComputer()
Gets computer that creates an LinkedHashSet regardless of the argument it applies to.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
EThe element type.
The computer.
public static <T> Predicate<T> neverPredicate()
TThe type of the predicate argument.
A Predicate that always returns false.
public static <T, U> BiConsumer<T, U> nullBiConsumer()
TThe type of the first consumer argument.
UThe type of the second consumer argument.
A BiConsumer that does nothing.
public static <T, U, R> BiFunction<T, U, R> nullBiFunction()
TThe type of the first function argument.
UThe type of the second function argument.
RThe type of the function result.
A BiFunction that does nothing.
public static <T> Consumer<T> nullConsumer()
TThe type of the consumer argument.
A Consumer that does nothing.
public static <T, R> Function<T, R> nullFunction()
TThe type of the function argument.
RThe type of the function result.
A Function that does nothing.
public static Runnable nullRunnable()
A Runnable that does nothing.
public static <T> Supplier<T> nullSupplier()
TThe type of the supplied object.
A Supplier that always returns null.
public static <T extends Comparable<super T>> Comparator<T> nullsFirstNaturalComparator()
Gets a comparator that sorts its non-null arguments by natural order, else the null values first.
TThe type of the compared objects.
The comparator.
public static <T extends Comparable<super T>> Comparator<T> nullsLastNaturalComparator()
Gets a comparator that sorts its non-null arguments by natural order, else the null values last.
TThe type of the compared objects.
The comparator.
public static <T> Function<T, Object> objectComputer()
Gets computer that creates an Object regardless of the argument it applies to.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
The computer.
Gets a function that creates an ArrayList with for the capacity it is applied to.

Same as:

 Function<Integer, ArrayList<T>> creator = ArrayList::new;
 
TThe array list element type.
A creator function.
Gets a function that creates a list with for the capacity it is applied to.

The result of the function is an immutable empty list if the capacity is 0, else it is a list that can hold at least up to capacity number of elements.

TThe array list element type.
A creator function.
Converts the argument throwing runnable to a simple Runnable by rethrowing any exceptions sneakily.

This method should be very rarely used if ever. Any exception that is thrown by the ThrowingRunnable.run() method will be rethrown out of the returned Runnable.run() method. If the thrown exception is checked, the caller of the Runnable.run() method may not be able to actually catch the exception.

runThe throwing runnable.
The sneakly throwing created runnable.
public static <K, T> Function<K, T> toComputer(Supplier<extends T> supplier)
Converts the argument Supplier to a computer function.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

The returned computer will call Supplier.get() regardless of the argument it applies to.

KThe type of the function argument.
TThe type of the supplied object.
supplierThe supplier.
The computer.
public static <T> Iterable<T> toIterable(Supplier<extends Iterator<T>> itsupplier)
Converts a supplier of iterators to an Iterable.

The returned iterable will call Supplier.get() for every iterator it creates.

TThe element type.
itsupplierThe iterator supplier.
The converted iterable.
public static <T> Supplier<T> toLockedRemovingSupplier(Iterator<extends T> iterator)
Converts the argument iterator to a Supplier that removes the elements after retrieving them.

Same as toRemovingSupplier(Iterator<extends T>), but the Supplier.get() calls of the returned supplier is locked for multi threaded access. Clients shouldn't expect the lock to be reentrant.

The returned supplier is not serializable.

TThe element type.
iteratorThe iterator.
The created supplier.
saker.util 0.8.4
public static <T> Supplier<T> toLockedSupplier(Enumeration<extends T> enumeration)
Converts the argument enumeration to a Supplier.

Same as toSupplier(Enumeration<extends T>), but the Supplier.get() calls of the returned supplier is locked for multi threaded access. Clients shouldn't expect the lock to be reentrant.

The returned supplier is not serializable.

TThe element type.
enumerationThe enumeration.
The created supplier.
saker.util 0.8.4
public static <T> Supplier<T> toLockedSupplier(Iterator<extends T> iterator)
Converts the argument iterator to a Supplier.

Same as toSupplier(Iterator<extends T>), but the Supplier.get() calls of the returned supplier is locked for multi threaded access. Clients shouldn't expect the lock to be reentrant.

The returned supplier is not serializable.

TThe element type.
iteratorThe iterator.
The created supplier.
saker.util 0.8.4
public static <T> Supplier<T> toRemovingSupplier(Iterator<extends T> iterator)
Converts the argument iterator to a Supplier that removes the elements after retrieving them.

The returned supplier will return null if there are no more elements in the iterator. If the iterator contains null elements, the code that uses the created supplier may not work properly.

The returned supplier will call Iterator.remove() after retrieving each element. If the iterator is unmodifiable, then the exception will be propagated to the caller.

The returned supplier is not serializable.

TThe element type.
iteratorThe iterator.
The created supplier.
public static <T> Supplier<T> toSupplier(Enumeration<extends T> enumeration)
Converts the argument enumeration to a Supplier.

The returned supplier will return null if there are no more elements in the enumeration. If the iterator contains null elements, the code that uses the created supplier may not work properly.

The returned supplier is not serializable.

TThe element type.
enumerationThe enumeration.
The created supplier.
public static <T> Supplier<T> toSupplier(Iterator<extends T> iterator)
Converts the argument iterator to a Supplier.

The returned supplier will return null if there are no more elements in the iterator. If the iterator contains null elements, the code that uses the created supplier may not work properly.

The returned supplier is not serializable.

TThe element type.
iteratorThe iterator.
The created supplier.
public static <T> Supplier<T> toSynchronizedRemovingSupplier(Iterator<extends T> iterator)
Converts the argument iterator to a Supplier that removes the elements after retrieving them.

Same as toRemovingSupplier(Iterator<extends T>), but the Supplier.get() calls of the returned supplier is synchronized on itself.

The returned supplier is not serializable.

TThe element type.
iteratorThe iterator.
The created supplier.
public static <T> Supplier<T> toSynchronizedSupplier(Enumeration<extends T> enumeration)
Converts the argument enumeration to a Supplier.

Same as toSupplier(Enumeration<extends T>), but the Supplier.get() calls of the returned supplier is synchronized on itself.

The returned supplier is not serializable.

TThe element type.
enumerationThe enumeration.
The created supplier.
public static <T> Supplier<T> toSynchronizedSupplier(Iterator<extends T> iterator)
Converts the argument iterator to a Supplier.

Same as toSupplier(Iterator<extends T>), but the Supplier.get() calls of the returned supplier is synchronized on itself.

The returned supplier is not serializable.

TThe element type.
iteratorThe iterator.
The created supplier.
public static <T, K, V> Function<T, TreeMap<K, V>> treeMapComputer()
Gets computer that creates an TreeMap regardless of the argument it applies to.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
KThe key type.
VThe value type.
The computer.
public static <T, E> Function<T, TreeSet<E>> treeSetComputer()
Gets computer that creates an TreeSet regardless of the argument it applies to.

Computers can be used in Map.computeIfAbsent(K, Function<super K, ? extends V>) method.

TThe type of the function argument.
EThe element type.
The computer.
public static <T> Supplier<T> valSupplier(T val)
Gets a Supplier that always returns the same value.
TThe type of the argument.
valThe value.
The supplier.