public final class Utilities
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
optional(java.util.List<?> objects)
Returns a
String representation of a List of Object s. |
static java.lang.String |
optional(java.util.List<?> objects,
java.lang.String left,
java.lang.String right,
java.lang.String separator)
Returns a
String representation of a List of Object s. |
static java.lang.String |
repeat(java.lang.String string,
int repetition)
Returns a
String that consists of string repeated repetition times. |
static <T> T[] |
requireDeepNonNull(T[] objects,
java.lang.String name)
Verifies that the array
objects and all elements in objects are not null . |
static <T> java.util.List<T> |
requireNonNullList(java.util.List<T> list)
Verifies that
list and all of its elements are not null . |
static <T> java.util.List<T> |
requireNonNullList(T object,
T[] objects)
Verifies that the
List created by the mandatory object and the optional non-null array objects contains no null elements. |
static int[] |
requireRange(int[] array,
int minimum,
int maximum)
Returns
array , but only if its elements are within the range of minimum (inclusive) and maximum (inclusive). |
static int |
requireRange(int value,
int minimum,
int maximum)
Checks that the specified value is within a given closed range, such as [minimum, maximum].
|
static int |
requireRange(int value,
int minimum,
int maximum,
java.lang.String name)
Checks that the specified value is within a given closed range, such as [minimum, maximum].
|
static <K,V extends java.lang.Comparable<? super V>> |
sortByValue(java.util.Map<K,V> map)
Returns a new
Map that is a value-sorted version of map . |
public static <K,V extends java.lang.Comparable<? super V>> java.util.Map<K,V> sortByValue(java.util.Map<K,V> map)
Map
that is a value-sorted version of map
.
If map
is null
, a NullPointerException
will be thrown.
K
- the generic key type of the Map
V
- the generic value type of the Map
map
- a Map
Map
that is a value-sorted version of map
java.lang.NullPointerException
- thrown if, and only if, map
is null
public static <T> java.util.List<T> requireNonNullList(java.util.List<T> list)
list
and all of its elements are not null
.
Returns the List
itself.
If list
or any of its elements are null
, a NullPointerException
will be thrown.
T
- the generic type of the List
list
- the List
to check for null
referencesList
itselfjava.lang.NullPointerException
- thrown if, and only if, list
or any of its elements are null
public static <T> java.util.List<T> requireNonNullList(T object, T[] objects)
List
created by the mandatory object
and the optional non-null
array objects
contains no null
elements.
Returns a new List
.
If object
, objects
or any of its elements are null
, a NullPointerException
will be thrown.
T
- the generic type of the List
object
- the mandatory Object
to addobjects
- the optional non-null
array to add after object
List
java.lang.NullPointerException
- thrown if, and only if, object
, objects
or any of its elements are null
public static java.lang.String optional(java.util.List<?> objects)
String
representation of a List
of Object
s.
If either objects
or an element in objects
are null
, a NullPointerException
will be thrown.
Calling this method is equivalent to the following:
Utilities.optional(objects, "", "", ", ");
objects
- a List
with Object
sString
representation of a List
of Object
sjava.lang.NullPointerException
- thrown if, and only if, either objects
or an element in objects
are null
public static java.lang.String optional(java.util.List<?> objects, java.lang.String left, java.lang.String right, java.lang.String separator)
String
representation of a List
of Object
s.
If either objects
, an element in objects
, left
, right
or separator
are null
, a NullPointerException
will be thrown.
The method is called optional
because, if objects
is empty, an empty String
will be returned. Otherwise, left
will be appended first, followed by the String
representation of each Object
in
objects
, all separated with separator
and finally, right
will be appended last.
objects
- a List
with Object
sleft
- the String
that will be appended first, if objects
is not emptyright
- the String
that will be appended last, if objects
is not emptyseparator
- the String
that will be used as separator or delimiter between the String
representation of each Object
in objects
String
representation of a List
of Object
sjava.lang.NullPointerException
- thrown if, and only if, either objects
, an element in objects
, left
, right
or separator
are null
public static <T> T[] requireDeepNonNull(T[] objects, java.lang.String name)
objects
and all elements in objects
are not null
.
Returns objects
.
If either objects
, an element in objects
or name
are null
, a NullPointerException
will be thrown.
T
- the generic type of the arrayobjects
- the array to verifyname
- the name of the variable that will be part of the message in the NullPointerException
objects
java.lang.NullPointerException
- thrown if, and only if, either objects
, an element in objects
or name
are null
public static int requireRange(int value, int minimum, int maximum)
Returns the value itself.
If value
is less than minimum
or greater than maximum
, an IllegalArgumentException
will be thrown.
This method is implemented in terms of requireRange(int, int, int, String)
, with a name of "value"
.
value
- the value to checkminimum
- the minimum (inclusive) bound of the rangemaximum
- the maximum (inclusive) bound of the rangejava.lang.IllegalArgumentException
- thrown if, and only if, value
is less than minimum
or greater than maximum
public static int requireRange(int value, int minimum, int maximum, java.lang.String name)
Returns the value itself.
If value
is less than minimum
or greater than maximum
, an IllegalArgumentException
will be thrown.
If name
is null
, a NullPointerException
will be thrown.
value
- the value to checkminimum
- the minimum (inclusive) bound of the rangemaximum
- the maximum (inclusive) bound of the rangename
- the name of the value, that will be part of the message to the IllegalArgumentException
, if thrownjava.lang.IllegalArgumentException
- thrown if, and only if, value
is less than minimum
or greater than maximum
java.lang.NullPointerException
- thrown if, and only if, name
is null
public static int[] requireRange(int[] array, int minimum, int maximum)
array
, but only if its elements are within the range of minimum
(inclusive) and maximum
(inclusive).
If one or more of its elements are not within said range, an IllegalArgumentException
will be thrown.
If array
is null
, a NullPointerException
will be thrown.
array
- the array with elements to verifyminimum
- the minimum value allowed (inclusive)maximum
- the maximum value allowed (inclusive)array
, but only if its elements are within the range of minimum
(inclusive) and maximum
(inclusive)java.lang.IllegalArgumentException
- thrown if, and only if, array
contains an element that is less than minimum
or greater than maximum
java.lang.NullPointerException
- thrown if, and only if, array
is null
public static java.lang.String repeat(java.lang.String string, int repetition)
String
that consists of string
repeated repetition
times.
If string
is null
, the String
literal "null"
will be repeated repetition
times.
If repetition
is less than or equal to 0
, an empty String
will be returned.
string
- the String
to repeatrepetition
- how many times string
should be repeatedString
that consists of string
repeated repetition
times