public class ColUtils
extends java.lang.Object
Created by covers1624 on 5/08/18.
| Constructor and Description |
|---|
ColUtils() |
| Modifier and Type | Method and Description |
|---|---|
static <T> java.util.List<T> |
addAllNoNull(T[] array,
java.util.List<T> list)
Adds all elements from the array that are not null to the list.
|
static <T> T[] |
addToArrayFirstNull(T[] array,
T value)
Adds the value at the first null index in the array.
|
static <T> boolean |
allMatch(java.lang.Iterable<T> col,
java.util.function.Predicate<T> func)
Returns true if all elements in the provided Iterable match the
given predicate.
|
static <T> boolean |
allMatch(T[] col,
java.util.function.Predicate<T> func)
Returns true if all elements in the provided array match the
given predicate.
|
static <T> boolean |
anyMatch(java.lang.Iterable<T> col,
java.util.function.Predicate<T> func)
Returns true if any element in the provided Iterable matches the
given predicate.
|
static void |
arrayCopy(java.lang.Object src,
int srcPos,
java.lang.Object dst,
int destPos,
int length)
Basically a wrapper for System.arraycopy with support for Copyable's
|
static <T> boolean |
contains(T[] input,
T element)
Checks if an array contains any of the specified element.
|
static <T> boolean |
containsKeys(java.util.Map<T,?> map,
T... keys)
Checks if a map contains all keys passed in.
|
static <T> int |
count(T[] array,
java.util.function.Function<T,java.lang.Boolean> check)
Counts elements in the array that conform to the Function check.
|
static <T> int |
countNonNull(T[] array)
Counts the elements in the array that are not null.
|
static <T> T[] |
createNewArray(T[] array)
Create a new array using the provided array as a template for both type and length.
|
static <T> T[] |
createNewArray(T[] array,
int length)
Create a new array using the provided array as a template for the type and with the provided length.
|
static <T> T[] |
fill(T[] array,
T value)
Fills the array with the specified value.
|
static <T> void |
fillArray(T[] array,
T value,
java.util.function.Function<T,java.lang.Boolean> check)
Fills the array with the specified value.
|
static <T> java.util.Optional<T> |
findFirst(java.lang.Iterable<T> col,
java.util.function.Predicate<T> func)
Finds the first element in the Iterable that matches the given predicate.
|
static <T> T |
head(java.lang.Iterable<T> col)
Asserts the
Iterable is not empty and returns the first element. |
static <T> java.util.Optional<T> |
headOption(java.lang.Iterable<T> col)
Attempts to get the first element of the Iterable.
|
static <T> T |
headOrDefault(java.lang.Iterable<T> col)
Returns the first element in the
Iterable otherwise
null. |
static <T> T |
headOrDefault(java.lang.Iterable<T> col,
T _default)
Returns the first element in the
Iterable otherwise
the supplied default. |
static <T> int |
indexOf(T[] array,
T object)
Returns the index of the first occurrence of the specified element in the array.
|
static <T> int |
indexOfRef(T[] array,
T object)
Returns the index of the first occurrence of the specified element in the array
with the same identity.
|
static <T> T[] |
inverse(T[] input,
T[] allElements)
Creates the inverse of an array.
|
static <T> boolean |
isEmpty(T[] array)
Checks if the array is all null.
|
static <T> boolean |
isNullOrContainsNull(T[] input)
Checks if the specified array is empty or contains a null entry.
|
static <E> java.lang.Iterable<E> |
iterable(java.util.Enumeration<E> enumeration)
Represents this Enumeration as an Iterable.
|
static <E> java.lang.Iterable<E> |
iterable(java.util.stream.Stream<E> stream)
Deprecated.
|
static <T> java.util.Iterator<T> |
iterator(T[] arr)
Return an iterator for an array.
|
static <T> java.util.Iterator<T> |
iterator(T[] arr,
int from,
int until)
Return an iterator for an array slice.
|
static <T> T |
maxBy(java.lang.Iterable<T> col,
java.util.function.ToIntFunction<T> func)
Deprecated.
|
static <T> T |
maxBy(T[] col,
java.util.function.ToIntFunction<T> func)
Returns the element in the array with the highest value returned by the supplied function.
|
static <T> T |
maxByOrDefault(java.lang.Iterable<T> col,
java.util.function.ToIntFunction<T> func,
T default_)
Returns the element in the Iterable with the highest value returned by the supplied function.
|
static <T> T[] |
newArray(java.lang.Class<T> arrayClass,
int length)
Creates a new array form its component class.
|
static <T> T |
only(java.lang.Iterable<T> col)
Assert the
Iterable contains a single element, and return it. |
static <T> T |
only(java.util.stream.Stream<T> stream)
Deprecated.
|
static <T> T |
onlyOrDefault(java.lang.Iterable<T> iterable)
Returns the first element found in the
Iterable if it's the first and only element in the iterable,
otherwise null is returned. |
static <T> T |
onlyOrDefault(java.lang.Iterable<T> iterable,
T _default)
Returns the first element found in the
Iterable if it's the first and only element in the iterable,
otherwise the default value is returned. |
static <T> T |
onlyOrDefault(java.util.stream.Stream<T> stream)
Deprecated.
|
static <T> T |
onlyOrDefault(java.util.stream.Stream<T> stream,
T _default)
Deprecated.
|
static <E> java.util.stream.Stream<E> |
parallelStream(java.lang.Iterable<E> iter)
Deprecated.
|
static <T> T |
requireMaxBy(java.lang.Iterable<T> col,
java.util.function.ToIntFunction<T> func)
Returns the element in the Iterable with the highest value returned by the supplied function.
|
static <T> void |
reverse(T[] array)
Reverse the provided array.
|
static <T> T[] |
rollArray(T[] input,
int shift)
Rolls the array based on the shift.
|
static <T> T[] |
slice(T[] arr,
int from,
int until)
Slices the given array.
|
static <E> java.util.stream.Stream<E> |
stream(java.lang.Iterable<E> iter)
Deprecated.
|
static <T> T |
tail(java.lang.Iterable<T> col)
Asserts the
Iterable is not empty and returns the last element. |
static <T> java.util.Optional<T> |
tailOption(java.lang.Iterable<T> col)
Attempts to get the last element of the Iterable.
|
static <T> T |
tailOrDefault(java.lang.Iterable<T> col)
Returns the last element in the
Iterable otherwise
null. |
static <T> T |
tailOrDefault(java.lang.Iterable<T> col,
T _default)
Returns the last element in the
Iterable otherwise
the supplied default. |
static java.util.List<java.lang.Integer> |
toList(int[] arr)
Convert an int array to a list of Integers.
|
public static <T> T[] slice(T[] arr,
int from,
int until)
arr - The Array to slice.from - The from index.until - The until index.@Nullable
public static <T> T maxBy(T[] col,
java.util.function.ToIntFunction<T> func)
col - The array.func - The function.@Nullable
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion="0.5.0")
public static <T> T maxBy(java.lang.Iterable<T> col,
java.util.function.ToIntFunction<T> func)
col - The array.func - The function.public static <T> T requireMaxBy(java.lang.Iterable<T> col,
java.util.function.ToIntFunction<T> func)
col - The collection.func - The function.@Nullable
@Contract(value="_,_,!null -> !null")
public static <T> T maxByOrDefault(java.lang.Iterable<T> col,
java.util.function.ToIntFunction<T> func,
@Nullable
T default_)
col - The collection.func - The function.default_ - The default value to return if the collection is empty.public static <T> boolean allMatch(T[] col,
java.util.function.Predicate<T> func)
col - The collection to match.func - The function to apply.public static <T> boolean allMatch(java.lang.Iterable<T> col,
java.util.function.Predicate<T> func)
col - The collection to match.func - The function to apply.public static <T> boolean anyMatch(java.lang.Iterable<T> col,
java.util.function.Predicate<T> func)
col - The collection to match.func - The function to apply.public static <T> java.util.Optional<T> findFirst(java.lang.Iterable<T> col,
java.util.function.Predicate<T> func)
col - The collection.func - The predicate.public static <T> java.util.Optional<T> headOption(java.lang.Iterable<T> col)
col - The collection.public static <T> T head(java.lang.Iterable<T> col)
Iterable is not empty and returns the first element.col - The Iterable.java.lang.IllegalArgumentException - If the Iterable is empty.@Nullable public static <T> T headOrDefault(java.lang.Iterable<T> col)
Iterable otherwise
null.col - The Iterable.null.@Nullable
@Contract(value="_,!null -> !null")
public static <T> T headOrDefault(java.lang.Iterable<T> col,
@Nullable
T _default)
Iterable otherwise
the supplied default.col - The Iterable._default - The default if the Iterable is empty.public static <T> java.util.Optional<T> tailOption(java.lang.Iterable<T> col)
col - The collection.public static <T> T tail(java.lang.Iterable<T> col)
Iterable is not empty and returns the last element.col - The Iterable.java.lang.IllegalArgumentException - If the Iterable is empty.@Nullable public static <T> T tailOrDefault(java.lang.Iterable<T> col)
Iterable otherwise
null.col - The Iterable.null.@Nullable
@Contract(value="_,!null -> !null")
public static <T> T tailOrDefault(java.lang.Iterable<T> col,
@Nullable
T _default)
Iterable otherwise
the supplied default.col - The Iterable._default - The default if the Iterable is empty.@SafeVarargs
public static <T> boolean containsKeys(java.util.Map<T,?> map,
T... keys)
T - The type of data in the map key.map - Map to check.keys - Keys that must exist.public static <T> T[] addToArrayFirstNull(T[] array,
T value)
T - Type of value.array - Array to add to.value - Value to add.public static <T> java.util.List<T> addAllNoNull(T[] array,
java.util.List<T> list)
T - What we are dealing with.array - Array to grab from.list - List to add to.public static <T> boolean isEmpty(T[] array)
T - What we are dealing with.array - The array to check.public static <T> int countNonNull(T[] array)
T - What we are dealing with.array - The array to check.public static <T> int count(T[] array,
java.util.function.Function<T,java.lang.Boolean> check)
T - What we are dealing with.array - The array to check.check - The Function to apply to each element.public static <T> T[] fill(T[] array,
T value)
T - What we are dealing with.array - Array to fill.value - Value to fill with.public static <T> void fillArray(T[] array,
T value,
java.util.function.Function<T,java.lang.Boolean> check)
T - What we are dealing with.array - Array to fill.value - Value to fill with.check - Called to decide if the value should be replaced.public static void arrayCopy(java.lang.Object src,
int srcPos,
java.lang.Object dst,
int destPos,
int length)
src - The source array.srcPos - Starting position in the source array.dst - The destination array.destPos - Starting position in the destination array.length - The number of elements to copy.public static <T> int indexOf(T[] array,
T object)
T - What we are dealing with.array - The array to search.object - Element to find.public static <T> int indexOfRef(T[] array,
T object)
array - The array to search.object - Element to find.public static <T> T[] createNewArray(T[] array)
T - The type.array - The template.public static <T> T[] createNewArray(T[] array,
int length)
T - The type.array - The type template.length - The new array's length.public static <T> T[] newArray(java.lang.Class<T> arrayClass,
int length)
T - The thing.arrayClass - The component class.length - The length.public static <T> T[] rollArray(T[] input,
int shift)
T - The thing.input - The input array.shift - The shift amount.public static <T> boolean contains(T[] input,
T element)
T - The thing.input - The inputelement - The thing to test against.public static <T> T[] inverse(T[] input,
T[] allElements)
T - The thing.input - The input.allElements - All possible values.public static <T> boolean isNullOrContainsNull(T[] input)
T - The thing.input - The input.public static java.util.List<java.lang.Integer> toList(int[] arr)
arr - in.public static <E> java.lang.Iterable<E> iterable(java.util.Enumeration<E> enumeration)
enumeration - The Enumeration.@Deprecated @ApiStatus.ScheduledForRemoval(inVersion="0.5.0") public static <E> java.lang.Iterable<E> iterable(java.util.stream.Stream<E> stream)
It is important to note, that a Stream can only be consumed once, and thus, the returned Iterable can only be consumed once.
stream - The Stream.@Deprecated @ApiStatus.ScheduledForRemoval(inVersion="0.5.0") public static <E> java.util.stream.Stream<E> stream(java.lang.Iterable<E> iter)
iter - The iterable.@Deprecated @ApiStatus.ScheduledForRemoval(inVersion="0.5.0") public static <E> java.util.stream.Stream<E> parallelStream(java.lang.Iterable<E> iter)
iter - The iterable.@Deprecated @ApiStatus.ScheduledForRemoval(inVersion="0.5.0") @Nullable public static <T> T onlyOrDefault(java.util.stream.Stream<T> stream)
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion="0.5.0")
@Nullable
@Contract(value="_,!null -> !null")
public static <T> T onlyOrDefault(java.util.stream.Stream<T> stream,
@Nullable
T _default)
stream - The stream._default - The default value, in the event the stream is empty, or has more than one element.@Nullable public static <T> T onlyOrDefault(java.lang.Iterable<T> iterable)
Iterable if it's the first and only element in the iterable,
otherwise null is returned.iterable - The Iterable.null@Nullable
@Contract(value="_,!null -> !null")
public static <T> T onlyOrDefault(java.lang.Iterable<T> iterable,
@Nullable
T _default)
Iterable if it's the first and only element in the iterable,
otherwise the default value is returned.iterable - The Iterable._default - The default value, in the event the iterable is empty, or has more than one element.@Deprecated @ApiStatus.ScheduledForRemoval(inVersion="0.5.0") public static <T> T only(java.util.stream.Stream<T> stream)
stream - The stream.java.lang.IllegalArgumentException - If the Stream contains no or more than one element.public static <T> T only(java.lang.Iterable<T> col)
Iterable contains a single element, and return it.col - The iterable.java.lang.IllegalArgumentException - If the Iterable contains no or more than one element.public static <T> java.util.Iterator<T> iterator(T[] arr)
arr - The array.Iterator.public static <T> java.util.Iterator<T> iterator(T[] arr,
int from,
int until)
arr - The array to iterate.from - The offset in the array.until - The end offset in the array.Iterator.public static <T> void reverse(T[] array)
array - The array to reverse.