Class XComparisonUtils

java.lang.Object
de.xima.fc.utils.XComparisonUtils

public class XComparisonUtils extends Object
Utilities for Comparator and Comparable.
Since:
2.1.0
Author:
XIMA MEDIA GmbH
  • Method Details

    • comparingArray

      public static <T> Comparator<T[]> comparingArray(Comparator<T> itemComparator)
      Creates a comparator that compares arrays by comparing their items in order. Starts by comparing the first items of each array, and returns that result if the two items are different. If the two items are equal, it compares the second items of each array, and returns that result if the two items are different. If equal, it proceeds with the third items, and so on. If the arrays are of different lengths, the shorter array is considered less than the longer array. A null array is treated as an empty array. The behavior of null items is determined by the provided item comparator.
      Type Parameters:
      T - Type of the items.
      Parameters:
      itemComparator - Comparator for the items.
      Returns:
      A comparator that compares arrays by comparing their items in order.
    • comparingList

      public static <T> Comparator<List<T>> comparingList(Comparator<T> itemComparator)
      Creates a comparator that compares lists by comparing their items in order. Starts by comparing the first items of each array, and returns that result if the two items are different. If the two items are equal, it compares the second items of each array, and returns that result if the two items are different. If equal, it proceeds with the third items, and so on. If the lists are of different lengths, the shorter array is considered less than the longer array. A null array is treated as an empty array. The behavior of null items is determined by the provided item comparator.
      Type Parameters:
      T - Type of the items.
      Parameters:
      itemComparator - Comparator for the items.
      Returns:
      A comparator that compares lists by comparing their items in order.
    • comparingFalseFirst

      public static <T> Comparator<T> comparingFalseFirst(com.google.common.base.Predicate<? super T> predicate)
      Compares two items by applying the given predicate to each item. Items for which the predicate returns false come before items for which the predicate returns true.
      Type Parameters:
      T - Type of the items to compare.
      Parameters:
      predicate - Predicate to apply to each item to determine its order.
      Returns:
      A comparator that compares items based on whether the match the given test function.
    • comparingFalseLast

      public static <T> Comparator<T> comparingFalseLast(com.google.common.base.Predicate<? super T> predicate)
      Compares two items by applying the given predicate to each item. Items for which the predicate returns false come after items for which the predicate returns true.
      Type Parameters:
      T - Type of the items to compare.
      Parameters:
      predicate - Predicate to apply to each item to determine its order.
      Returns:
      A comparator that compares items based on whether the match the given test function.
    • comparingInStages

      @SafeVarargs public static <T> Comparator<T> comparingInStages(Comparator<? super T>... stages)
      Creates a comparator that compares items in stages. Compared to building the comparator via Comparator.thenComparing(Comparator), this method better support type parameter inference.
      Type Parameters:
      T - Type of the items.
      Parameters:
      stages - Stages by which to compare.
      Returns:
      A comparator that compares first by the first comparator, then by the second comparator if they are equal, then by the third comparator etc.
    • comparingNullsFirst

      public static <Item, Key extends Comparable<Key>> Comparator<Item> comparingNullsFirst(Function<Item,Key> keyExtractor)
      Similar to Comparator#comparing, but takes care of nulls by sorting nulls first. Handles both the case when the item is null and when the extracted key is null.
      Type Parameters:
      Item - Type of the items.
      Key - Type of each item's key.
      Parameters:
      keyExtractor - Function to extract the key to compare.
      Returns:
      A comparator that compares the keys extracted by the key extractor, with nulls first.
    • comparingNullsLast

      public static <Item, Key extends Comparable<Key>> Comparator<Item> comparingNullsLast(Function<Item,Key> keyExtractor)
      Similar to Comparator#comparing, but takes care of nulls by sorting nulls last. Handles both the case when the item is null and when the extracted key is null.
      Type Parameters:
      Item - Type of the items.
      Key - Type of each item's key.
      Parameters:
      keyExtractor - Function to extract the key to compare.
      Returns:
      A comparator that compares the keys extracted by the key extractor, with nulls last.
    • comparingTrueFirst

      public static <T> Comparator<T> comparingTrueFirst(com.google.common.base.Predicate<? super T> predicate)
      Compares two items by applying the given predicate to each item. Items for which the predicate returns true come before items for which the predicate returns false.
      Type Parameters:
      T - Type of the items to compare.
      Parameters:
      predicate - Predicate to apply to each item to determine its order.
      Returns:
      A comparator that compares items based on whether the match the given test function.
    • comparingTrueLast

      public static <T> Comparator<T> comparingTrueLast(com.google.common.base.Predicate<? super T> predicate)
      Compares two items by applying the given predicate to each item. Items for which the predicate returns true come after items for which the predicate returns false.
      Type Parameters:
      T - Type of the items to compare.
      Parameters:
      predicate - Predicate to apply to each item to determine its order.
      Returns:
      A comparator that compares items based on whether the match the given test function.
    • constrainToRange

      public static <T extends Comparable<T>> T constrainToRange(T value, T min, T max)
      Constrains a comparable value to a range.
      Type Parameters:
      T - Type of the value.
      Parameters:
      value - Value to constrain.
      min - Minimum value.
      max - Maximum value.
      Returns:
      The value if it is within the range, the minimum value if it is below the range, the maximum value if it is above the range.
    • constrainToRange

      public static <T extends Comparable<T>> T constrainToRange(T value, T min, T max, T defaultValue)
      Constrains a comparable value to a range.
      Type Parameters:
      T - Type of the value.
      Parameters:
      value - Value to constrain.
      min - Minimum value.
      max - Maximum value.
      defaultValue - Default value if the value is null.
      Returns:
      The value if it is within the range, the minimum value if it is below the range, the maximum value if it is above the range, and the default value if the value is null.