Class Utils

java.lang.Object
com.amalgamasimulation.utils.Utils

public class Utils extends Object
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Deprecated, for removal: This API element is subject to removal in a future version.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <V, T, R> Function<V,R>
    compose(Function<V,T> first, Function<T,R> second)
    Deprecated, for removal: This API element is subject to removal in a future version.
    static <V, T1, T2, R>
    Function<V,R>
    compose(Function<V,T1> first, Function<T1,T2> second, Function<T2,R> third)
    Deprecated, for removal: This API element is subject to removal in a future version.
    static <V, R> Function<V,R>
    constant(R constant)
    Deprecated, for removal: This API element is subject to removal in a future version.
    static LocalTime
    getLocalTimeDifference(LocalTime localTime1, LocalTime localTime2)
     
    static int
    Returns the number of days in the month of the specified LocalDateTime.
    static <T> List<T>
    iteratorToList(Iterator<T> iterator)
    Converts an Iterator to a List containing all its elements.
    static <T> Stream<T>
    Converts an Iterator to a sequential Stream.
    static double
    limit(double lowerBound, double x, double upperBound)
    Deprecated.
    If you use Java-21+, switch to the Math.clamp(double, double, double) method.
    static int
    limit(int lowerBound, int x, int upperBound)
    Deprecated.
    If you use Java-21+, switch to the Math.clamp(long, int, int) method.
    static long
    limit(long lowerBound, long x, long upperBound)
    Deprecated.
    If you use Java-21+, switch to the Math.clamp(long, long, long) method.
    static <E> Supplier<E>
    supplier(E constant)
    Deprecated, for removal: This API element is subject to removal in a future version.
    static <T> List<T>
    toList(Stream<? extends T> stream)
    Deprecated, for removal: This API element is subject to removal in a future version.
    use standard JDK's Collectors.toList() method
    static <E> List<E>
    union(List<? extends E>... lists)
    Deprecated, for removal: This API element is subject to removal in a future version.
    static double
    weightedAverage(double weight1, double value1, double weight2, double value2)
    Returns the weighted average across the two specified values with respective specified weights, more specifically: (weight1 / (weight1 + weight2)) * value1 + (weight2 / (weight1 + weight2)) * value2.
    static double
    xidz(double a, double b, double x)
    Returns the result of a / b, and x if b == 0.
    static double
    zidz(double a, double b)
    Returns the result of a / b, and 0 if b == 0.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • zidz

      public static double zidz(double a, double b)
      Returns the result of a / b, and 0 if b == 0.
      Parameters:
      a -
      b -
      Returns:
      a / b, and 0 if b == 0
    • xidz

      public static double xidz(double a, double b, double x)
      Returns the result of a / b, and x if b == 0.
      Parameters:
      a -
      b -
      x -
      Returns:
      a / b, and x if b == 0
    • limit

      @Deprecated(since="Java-21") public static double limit(double lowerBound, double x, double upperBound)
      Deprecated.
      If you use Java-21+, switch to the Math.clamp(double, double, double) method. WARNING: mind the arguments order!
      Returns the specified x value limited by the specified lower and upper bounds, more specifically:
      • returns x if x is between lowerBound and upperBound, inclusively
      • returns lowerBound if x < lowerBound
      • returns upperBound if x > upperBound
      Parameters:
      lowerBound - specified lower bound
      x - specified value to be limited by the bounds
      upperBound - specified lower bound
      Returns:
      x value limited by the specified lower and upper bounds
    • limit

      @Deprecated(since="Java-21") public static long limit(long lowerBound, long x, long upperBound)
      Deprecated.
      If you use Java-21+, switch to the Math.clamp(long, long, long) method. WARNING: mind the arguments order!
      Returns the specified x value limited by the specified lower and upper bounds, more specifically:
      • returns x if x is between lowerBound and upperBound, inclusively
      • returns lowerBound if x < lowerBound
      • returns upperBound if x > upperBound
      Parameters:
      lowerBound - specified lower bound
      x - specified value to be limited by the bounds
      upperBound - specified lower bound
      Returns:
      x value limited by the specified lower and upper bounds
    • limit

      @Deprecated(since="Java-21") public static int limit(int lowerBound, int x, int upperBound)
      Deprecated.
      If you use Java-21+, switch to the Math.clamp(long, int, int) method. WARNING: mind the arguments order!
      Returns the specified x value limited by the specified lower and upper bounds, more specifically:
      • returns x if x is between lowerBound and upperBound, inclusively
      • returns lowerBound if x < lowerBound
      • returns upperBound if x > upperBound
      Parameters:
      lowerBound - specified lower bound
      x - specified value to be limited by the bounds
      upperBound - specified lower bound
      Returns:
      x value limited by the specified lower and upper bounds
    • weightedAverage

      public static double weightedAverage(double weight1, double value1, double weight2, double value2)
      Returns the weighted average across the two specified values with respective specified weights, more specifically: (weight1 / (weight1 + weight2)) * value1 + (weight2 / (weight1 + weight2)) * value2.
      Parameters:
      weight1 - weight of the first value
      value1 - first value
      weight2 - weight of the second value
      value2 - second value
      Returns:
      weighted average across the two specified values with respective specified weights
    • toList

      @Deprecated(forRemoval=true) public static <T> List<T> toList(Stream<? extends T> stream)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use standard JDK's Collectors.toList() method
      Converts the specified stream to a list. The result of method is identical to the result of the following expression: stream.collect(Collectors.toList())
      Type Parameters:
      T - type of elements of the stream
      Parameters:
      stream - specified stream
      Returns:
      list with elements extracted from the specified stream
    • union

      @SafeVarargs @Deprecated(forRemoval=true) public static <E> List<E> union(List<? extends E>... lists)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Concatenates several lists into a single list. A new list is created, so the original lists are not affected.
      Parameters:
      lists - lists to be combined into one list
      Returns:
      A single list that contains all elements from the lists
    • getMaxDaysOfCurrentMonth

      public static int getMaxDaysOfCurrentMonth(LocalDateTime localDateTime)
      Returns the number of days in the month of the specified LocalDateTime.
      Parameters:
      localDateTime - specified LocalDateTime
      Returns:
      number of days in the month of the specified LocalDateTime
    • getLocalTimeDifference

      public static LocalTime getLocalTimeDifference(LocalTime localTime1, LocalTime localTime2)
    • compose

      @Deprecated(forRemoval=true) public static <V, T, R> Function<V,R> compose(Function<V,T> first, Function<T,R> second)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Returns a function that is a composition of the two specified functions. The resulting function is an equivalent of the following function:
       Function<V, R> result = v -> {
                      T t = first.apply( v );
                      R r = second.apply( t );
                      return r;
       }
       
      Typical use case of this function is constructing a function for extracting and formating some value in one line, e.g.:
       compose( model.getEngine()::timeToDate, Formats.getDefaultFormats()::dayMonthHoursMinutes )
                                                              ^                                                                       ^
                                      Extract date from time                                          Format date
       
      Parameters:
      first - first function being composed
      second - second function being composed
      Returns:
      composition of the two specified functions
    • compose

      @Deprecated(forRemoval=true) public static <V, T1, T2, R> Function<V,R> compose(Function<V,T1> first, Function<T1,T2> second, Function<T2,R> third)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • constant

      @Deprecated(forRemoval=true) public static <V, R> Function<V,R> constant(R constant)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • supplier

      @Deprecated(forRemoval=true) public static <E> Supplier<E> supplier(E constant)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • iteratorToStream

      public static <T> Stream<T> iteratorToStream(Iterator<T> iterator)
      Converts an Iterator to a sequential Stream. The resulting stream preserves the iteration order and can only be consumed once.
      Type Parameters:
      T - the type of elements in the iterator and stream
      Parameters:
      iterator - the iterator to convert to a stream
      Returns:
      a sequential Stream containing the elements from the iterator
    • iteratorToList

      public static <T> List<T> iteratorToList(Iterator<T> iterator)
      Converts an Iterator to a List containing all its elements. This method consumes the iterator and collects all elements into a new list.
      Type Parameters:
      T - the type of elements in the iterator and resulting list
      Parameters:
      iterator - the iterator to convert to a list
      Returns:
      a new List containing all elements from the iterator in iteration order