Class DataUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> Function <K, V> asFunction
(Map<K, V> map, V defaultValue) Compute the change of mappings between two maps.static <T> List
<T> Collect items obtained from an iterable into a List.static <T> List
<T> Collect items from an iterator into a List.static <T> T[]
collectArray
(Iterable<T> iterable) Collect items obtained from an iterable into an array.static <T> T[]
collectArray
(Iterator<T> iterator) Collect items from an iterator into an array.static <T extends @Nullable Object>
TConvert an object to a different class.static <T> @Nullable T
Convert an object to a different class.static <T,
U> List <U> convert
(Collection<T> data, Class<U> targetClass) Convert Collection to List.convert
(Collection<T> data, Class<U> targetClass, boolean useConstructor) Convert Collection to list.static <T,
U> List <U> convert
(Collection<T> data, Function<? super T, ? extends U> mapper) Convert Collection to list.static <T,
U, C extends Collection<U>>
CconvertCollection
(Collection<T> data, Class<U> targetClass, Supplier<C> supplier) Convert Collection.static <T,
U, C extends Collection<U>>
CconvertCollection
(Collection<T> data, Class<U> targetClass, Supplier<C> supplier, boolean useConstructor) Convert Collection to list.static <T,
U> U[] convertToArray
(Collection<T> data, Class<? extends U> targetClass) Convert Collection to array.convertToArray
(Collection<T> data, Class<U> targetClass, boolean useConstructor) Convert Collection to array.static <U,
V> Map <U, V> Compute the difference of mappings between two maps.static <U,
V> Map <U, V> diff
(Map<? extends U, ? extends V> a, Map<? extends U, ? extends V> b, Supplier<? extends Map<U, V>> mapFactory) Compute the difference of mappings between two maps.static <T> Iterator
<T> Create a filtering iterator that only lets through items matching a predicate.static <T,
U> boolean Execute action if key is mapped to a non-null value.static <T,
U> void Execute action if key is mapped.static <T> boolean
isSorted
(Iterable<T> collection, Comparator<T> comparator) Determines if the given collection is sorted according to the order defined by the provided comparator.static <T extends Comparable<T>>
booleanisSorted
(Collection<T> collection) Determines if the elements within the given collection are sorted in natural order.static <T,
U> Iterator <U> Create a mapping iterator that converts elements on the fly.
-
Method Details
-
convert
Convert an object to a different class.Conversion works as follows:
- if value is
null
,null
is returned; - if the target class is assignment compatible, a simple cast is performed;
- if the target class is
String
,Object.toString()
is used; - if the target class is an integer type and the value is of type double, a conversion without loss of precision is tried;
- if the value is of type
String
and the target class provides a methodpublic static T valueOf(String)
, that method is invoked; - otherwise an exception is thrown.
- Type Parameters:
T
- target type- Parameters:
value
- the object to converttargetClass
- the target class- Returns:
- the object converted to the target class
- if value is
-
convert
public static <T> @Nullable T convert(@Nullable Object value, Class<T> targetClass, boolean useConstructor) Convert an object to a different class.Conversion works as follows:
- if value is
null
,null
is returned; - if the target class is assignment compatible, a simple cast is performed;
- if the target class is
String
,Object.toString()
is used; - if the target class is an integer type and the value is of type double, a conversion without loss of precision is tried;
- if the target class is
LocalDate
and the source class isString
, use DateTimeFormatter.ISO_DATE; - if the target class is
LocalDateTime
and the source class isString
, use DateTimeFormatter.ISO_DATE_TIME; - if the source and target classes is any of URI, URL, File, Path, the standard conversion rules are applied;
- if the source class is
String
and the target class is any of URI, URL, File, Path, the standard conversion rules are applied; - if the target class provides a method
public static T valueOf(U)
and {value instanceof U}, that method is invoked; - if
useConstructor
istrue
and the target class provides a constructor taking a single argument of value's type, that constructor is used; - otherwise an exception is thrown.
- Type Parameters:
T
- target type- Parameters:
value
- the object to converttargetClass
- the target classuseConstructor
- flag whether a public constructorT(U)
should be used in conversion if present where `U` is the value's class- Returns:
- the object converted to the target class
- if value is
-
convertToArray
Convert Collection to array.Converts a
Collection<T>
toU[]
by usingconvert(Object, Class)
on the elements contained in the collection.- Type Parameters:
T
- the element source typeU
- the element target type- Parameters:
data
- the collection to converttargetClass
- the element target class- Returns:
- array containing the converted elements
-
convertToArray
public static <T extends @Nullable Object,U extends @Nullable Object> U[] convertToArray(Collection<T> data, Class<U> targetClass, boolean useConstructor) Convert Collection to array.Converts a
Collection<T>
toU[]
by usingconvert(Object, Class)
on the elements contained in the collection.- Type Parameters:
T
- the element source typeU
- the element target type- Parameters:
data
- the collection to converttargetClass
- the element target classuseConstructor
- flag whether a public constructorU(T)
should be used in conversion if present- Returns:
- array containing the converted elements
-
convert
Convert Collection to list.Converts a
Collection<T>
toList<U>
by using the supplied mapper function on the elements contained in the collection.- Type Parameters:
T
- the element source typeU
- the element target type- Parameters:
data
- the collection to convertmapper
- the mapping function- Returns:
- list containing the converted elements
-
convert
Convert Collection to List.Converts a
Collection<T>
toList<U>
by usingconvert(Object, Class)
on the elements contained in the collection.The source collection must not contain
null
values. UseconvertCollection(Collection, Class, Supplier)
if the source collection containsnull
values.- Type Parameters:
T
- the element source typeU
- the element target type- Parameters:
data
- the collection to converttargetClass
- the element target class- Returns:
- list containing the converted elements
-
convert
public static <T extends @Nullable Object,U extends @Nullable Object> List<U> convert(Collection<T> data, Class<U> targetClass, boolean useConstructor) Convert Collection to list.Converts a
Collection<T>
toList<U>
by usingconvert(Object, Class)
on the elements contained in the collection.The source collection must not contain
null
values. UseconvertCollection(Collection, Class, Supplier, boolean)
if the source collection containsnull
values.- Type Parameters:
T
- the element source typeU
- the element target type- Parameters:
data
- the collection to converttargetClass
- the element target classuseConstructor
- flag whether a public constructorU(T)
should be used in conversion if present- Returns:
- list containing the converted elements
-
convertCollection
public static <T,U, C convertCollectionC extends Collection<U>> (Collection<T> data, Class<U> targetClass, Supplier<C> supplier) Convert Collection.Converts a
Collection<T>
toCollection<U>
by usingconvert(Object, Class)
on the elements contained in the collection.- Type Parameters:
T
- the element source typeU
- the element target typeC
- the target collection type- Parameters:
data
- the collection to converttargetClass
- the element target classsupplier
- the collection supplier, i. e.ArrayList::new
- Returns:
- collection containing the converted elements
-
convertCollection
public static <T,U, C convertCollectionC extends Collection<U>> (Collection<T> data, Class<U> targetClass, Supplier<C> supplier, boolean useConstructor) Convert Collection to list.Converts a
Collection<T>
toCollection<U>
by usingconvert(Object, Class)
on the elements contained in the collection.- Type Parameters:
T
- the element source typeU
- the element target typeC
- the target collection type- Parameters:
data
- the collection to converttargetClass
- the element target classsupplier
- the collection supplier, i. e.ArrayList::new
useConstructor
- flag whether a public constructorU(T)
should be used in conversion if present- Returns:
- collection containing the converted elements
-
filter
Create a filtering iterator that only lets through items matching a predicate.- Type Parameters:
T
- the item type- Parameters:
iterator
- the base iteratorpredicate
- the predicate to test items with- Returns:
- iterator instance that skips items not matching the predicate
-
map
Create a mapping iterator that converts elements on the fly.- Type Parameters:
T
- the source iterator item typeU
- the target iterator item type- Parameters:
iterator
- the base iteratormapping
- the mapping to apply to elements- Returns:
- iterator instance that converts items of type
T
toU
-
collect
Collect items obtained from an iterable into a List.- Type Parameters:
T
- the element type- Parameters:
iterable
- the iterable- Returns:
- list of elements
-
collect
Collect items from an iterator into a List.- Type Parameters:
T
- the element type- Parameters:
iterator
- the iterator- Returns:
- list of elements
-
collectArray
Collect items obtained from an iterable into an array.- Type Parameters:
T
- the element type- Parameters:
iterable
- the iterable- Returns:
- array of elements
-
collectArray
Collect items from an iterator into an array.- Type Parameters:
T
- the element type- Parameters:
iterator
- the iterator- Returns:
- array of elements
-
asFunction
- Type Parameters:
K
- type of keyV
- type of value- Parameters:
map
- the mapdefaultValue
- the value to return if the lookup key is not present- Returns:
- a Function instance returning the map entries
-
changes
public static <U,V extends @Nullable Object> Map<U,Pair<V, changesV>> (Map<? extends U, ? extends V> a, Map<? extends U, ? extends V> b) Compute the change of mappings between two maps. The result is a mapping from keys to pairs(value a, value b)
of the changes. See alsodiff(Map, Map)
.- Type Parameters:
U
- the key typeV
- the value type- Parameters:
a
- the first mapb
- the second map- Returns:
- a new map that contains the changes as pairs (value in
a
, value inb
)
-
diff
Compute the difference of mappings between two maps. The result is a map that maps keys to the new values for all changed keys. See alsochanges(Map, Map)
.- Type Parameters:
U
- the key typeV
- the value type- Parameters:
a
- the first mapb
- the second map- Returns:
- a new map that contains the changed mappings (k -> mapped value in b)
-
diff
public static <U,V> Map<U,V> diff(Map<? extends U, ? extends V> a, Map<? extends U, ? extends V> b, Supplier<? extends Map<U, V>> mapFactory) Compute the difference of mappings between two maps. The result is a map obtained by callingmapFactory.get()
that maps keys to the new values for all changed keys. See alsochanges(Map, Map)
.- Type Parameters:
U
- the key typeV
- the value type- Parameters:
a
- the first mapb
- the second mapmapFactory
- the Map factory- Returns:
- a new map that contains the changed mappings (k -> mapped value in b)
-
ifPresent
Execute action if key is mapped. See alsoifMapped(Map, Object, Consumer)
.- Type Parameters:
T
- the key typeU
- the value type- Parameters:
map
- the mapkey
- the keyaction
- the action
-
ifMapped
Execute action if key is mapped to a non-null value. See alsoifPresent(Map, Object, Consumer)
.- Type Parameters:
T
- the key typeU
- the value type- Parameters:
map
- the mapkey
- the keyaction
- the action- Returns:
- true, if action was called
-
isSorted
Determines if the elements within the given collection are sorted in natural order.This method has a time complexity of O(n) for collections that do not implement
SortedSet
and use the natural order. Its main purpose is to be used in assertions on method parameters that have to be sorted.- Type Parameters:
T
- the type of elements in the collection, which must extend Comparable- Parameters:
collection
- the collection to check for sorted order- Returns:
true
if the collection is sorted in natural order,false
otherwise
-
isSorted
Determines if the given collection is sorted according to the order defined by the provided comparator.This method has a time complexity of O(n). Its main purpose is to be used in assertions on method parameters that have to be sorted.
- Type Parameters:
T
- the type of objects in the collection- Parameters:
collection
- the collection of elements to check for sortingcomparator
- the comparator used to define the sorting order- Returns:
true
if the collection is sorted in the order defined by the comparator, otherwisefalse
-