Class DataUtil

java.lang.Object
com.dua3.utility.data.DataUtil

public final class DataUtil extends Object
Utility class for data handling and conversion.
  • Method Details

    • convert

      public static <T extends @Nullable Object> T convert(@Nullable Object value, Class<T> targetClass)
      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 method public static T valueOf(String), that method is invoked;
      • otherwise an exception is thrown.
      Type Parameters:
      T - target type
      Parameters:
      value - the object to convert
      targetClass - the target class
      Returns:
      the object converted to the target class
    • 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 is String, use DateTimeFormatter.ISO_DATE;
      • if the target class is LocalDateTime and the source class is String, 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 is true 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 convert
      targetClass - the target class
      useConstructor - flag whether a public constructor T(U) should be used in conversion if present where `U` is the value's class
      Returns:
      the object converted to the target class
    • convertToArray

      public static <T, U> U[] convertToArray(Collection<T> data, Class<? extends U> targetClass)
      Convert Collection to array.

      Converts a Collection<T> to U[] by using convert(Object, Class) on the elements contained in the collection.

      Type Parameters:
      T - the element source type
      U - the element target type
      Parameters:
      data - the collection to convert
      targetClass - 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> to U[] by using convert(Object, Class) on the elements contained in the collection.

      Type Parameters:
      T - the element source type
      U - the element target type
      Parameters:
      data - the collection to convert
      targetClass - the element target class
      useConstructor - flag whether a public constructor U(T) should be used in conversion if present
      Returns:
      array containing the converted elements
    • convert

      public static <T, U> List<U> convert(Collection<T> data, Function<? super T,? extends U> mapper)
      Convert Collection to list.

      Converts a Collection<T> to List<U> by using the supplied mapper function on the elements contained in the collection.

      Type Parameters:
      T - the element source type
      U - the element target type
      Parameters:
      data - the collection to convert
      mapper - the mapping function
      Returns:
      list containing the converted elements
    • convert

      public static <T, U> List<U> convert(Collection<T> data, Class<U> targetClass)
      Convert Collection to List.

      Converts a Collection<T> to List<U> by using convert(Object, Class) on the elements contained in the collection.

      The source collection must not contain null values. Use convertCollection(Collection, Class, Supplier) if the source collection contains null values.

      Type Parameters:
      T - the element source type
      U - the element target type
      Parameters:
      data - the collection to convert
      targetClass - 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> to List<U> by using convert(Object, Class) on the elements contained in the collection.

      The source collection must not contain null values. Use convertCollection(Collection, Class, Supplier, boolean) if the source collection contains null values.

      Type Parameters:
      T - the element source type
      U - the element target type
      Parameters:
      data - the collection to convert
      targetClass - the element target class
      useConstructor - flag whether a public constructor U(T) should be used in conversion if present
      Returns:
      list containing the converted elements
    • convertCollection

      public static <T, U, C extends Collection<U>> C convertCollection(Collection<T> data, Class<U> targetClass, Supplier<C> supplier)
      Convert Collection.

      Converts a Collection<T> to Collection<U> by using convert(Object, Class) on the elements contained in the collection.

      Type Parameters:
      T - the element source type
      U - the element target type
      C - the target collection type
      Parameters:
      data - the collection to convert
      targetClass - the element target class
      supplier - the collection supplier, i. e. ArrayList::new
      Returns:
      collection containing the converted elements
    • convertCollection

      public static <T, U, C extends Collection<U>> C convertCollection(Collection<T> data, Class<U> targetClass, Supplier<C> supplier, boolean useConstructor)
      Convert Collection to list.

      Converts a Collection<T> to Collection<U> by using convert(Object, Class) on the elements contained in the collection.

      Type Parameters:
      T - the element source type
      U - the element target type
      C - the target collection type
      Parameters:
      data - the collection to convert
      targetClass - the element target class
      supplier - the collection supplier, i. e. ArrayList::new
      useConstructor - flag whether a public constructor U(T) should be used in conversion if present
      Returns:
      collection containing the converted elements
    • filter

      public static <T> Iterator<T> filter(Iterator<T> iterator, Predicate<T> predicate)
      Create a filtering iterator that only lets through items matching a predicate.
      Type Parameters:
      T - the item type
      Parameters:
      iterator - the base iterator
      predicate - the predicate to test items with
      Returns:
      iterator instance that skips items not matching the predicate
    • map

      public static <T, U> Iterator<U> map(Iterator<T> iterator, Function<? super T,? extends U> mapping)
      Create a mapping iterator that converts elements on the fly.
      Type Parameters:
      T - the source iterator item type
      U - the target iterator item type
      Parameters:
      iterator - the base iterator
      mapping - the mapping to apply to elements
      Returns:
      iterator instance that converts items of type T to U
    • collect

      public static <T> List<T> collect(Iterable<? extends T> iterable)
      Collect items obtained from an iterable into a List.
      Type Parameters:
      T - the element type
      Parameters:
      iterable - the iterable
      Returns:
      list of elements
    • collect

      public static <T> List<T> collect(Iterator<? extends T> iterator)
      Collect items from an iterator into a List.
      Type Parameters:
      T - the element type
      Parameters:
      iterator - the iterator
      Returns:
      list of elements
    • collectArray

      public static <T> T[] collectArray(Iterable<T> iterable)
      Collect items obtained from an iterable into an array.
      Type Parameters:
      T - the element type
      Parameters:
      iterable - the iterable
      Returns:
      array of elements
    • collectArray

      public static <T> T[] collectArray(Iterator<T> iterator)
      Collect items from an iterator into an array.
      Type Parameters:
      T - the element type
      Parameters:
      iterator - the iterator
      Returns:
      array of elements
    • asFunction

      public static <K, V> Function<K,V> asFunction(Map<K,V> map, V defaultValue)
      Convert Map to Function.
      Type Parameters:
      K - type of key
      V - type of value
      Parameters:
      map - the map
      defaultValue - 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,V>> changes(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 also diff(Map, Map).
      Type Parameters:
      U - the key type
      V - the value type
      Parameters:
      a - the first map
      b - the second map
      Returns:
      a new map that contains the changes as pairs (value in a, value in b)
    • diff

      public static <U, V> Map<U,V> diff(Map<? extends U,? extends V> a, Map<? extends U,? extends V> b)
      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 also changes(Map, Map).
      Type Parameters:
      U - the key type
      V - the value type
      Parameters:
      a - the first map
      b - 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 calling mapFactory.get() that maps keys to the new values for all changed keys. See alsochanges(Map, Map).
      Type Parameters:
      U - the key type
      V - the value type
      Parameters:
      a - the first map
      b - the second map
      mapFactory - the Map factory
      Returns:
      a new map that contains the changed mappings (k -> mapped value in b)
    • ifPresent

      public static <T, U> void ifPresent(Map<T,U> map, T key, Consumer<? super U> action)
      Execute action if key is mapped. See also ifMapped(Map, Object, Consumer).
      Type Parameters:
      T - the key type
      U - the value type
      Parameters:
      map - the map
      key - the key
      action - the action
    • ifMapped

      public static <T, U> boolean ifMapped(Map<T,U> map, T key, Consumer<? super U> action)
      Execute action if key is mapped to a non-null value. See also ifPresent(Map, Object, Consumer).
      Type Parameters:
      T - the key type
      U - the value type
      Parameters:
      map - the map
      key - the key
      action - the action
      Returns:
      true, if action was called
    • isSorted

      public static <T extends Comparable<T>> boolean isSorted(Collection<T> collection)
      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

      public 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.

      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 sorting
      comparator - the comparator used to define the sorting order
      Returns:
      true if the collection is sorted in the order defined by the comparator, otherwise false