Class OrderUtil

java.lang.Object
io.micronaut.core.order.OrderUtil

public class OrderUtil extends Object
Apply the Ordered interface to lists or arrays.
Since:
1.0
Author:
Graeme Rocher
  • Field Details

    • COMPARATOR

      public static final Comparator<Object> COMPARATOR
      Orders objects using getOrder(Object), i.e. objects that don't implement Ordered and which are not AnnotationMetadata will be placed non-deterministically in the final position.

      You probably want to use COMPARATOR_ZERO instead, as with this comparator no object can sort itself to the back of the collection.

    • COMPARATOR_ZERO

      public static final Comparator<Object> COMPARATOR_ZERO
      Orders objects using getOrderWithDefaultPrecedence(Object, int) using zero as the default precedence.
    • ORDERED_COMPARATOR

      public static final Comparator<Ordered> ORDERED_COMPARATOR
      The comparator of elements implementing Ordered.
    • REVERSE_ORDERED_COMPARATOR

      public static final Comparator<Ordered> REVERSE_ORDERED_COMPARATOR
      The reverse comparator of elements implementing Ordered.
    • REVERSE_COMPARATOR

      public static final Comparator<Object> REVERSE_COMPARATOR
      Provide a comparator, in reversed order, for collections.

      You probably want to use REVERSE_COMPARATOR_ZERO instead, as with this comparator no object can sort itself to the front of the collection.

    • REVERSE_COMPARATOR_ZERO

      public static final Comparator<Object> REVERSE_COMPARATOR_ZERO
      Provide a comparator, in reversed order, for collections.
  • Constructor Details

    • OrderUtil

      public OrderUtil()
  • Method Details

    • sort

      public static void sort(List<?> list)
      Sort the given list using COMPARATOR.

      You should probably not use this method. Prefer calling list.sort(OrderUtil.COMPARATOR_ZERO) instead, which offers more intuitive behavior for beans that don't expose an ordering.

      Parameters:
      list - The list to sort
    • sort

      public static <T> Stream<T> sort(Stream<T> list)
      Sort the given list.

      You should probably not use this method. Prefer calling stream.sorted(OrderUtil.COMPARATOR_ZERO) instead, which offers more intuitive behavior for beans that don't expose an ordering.

      Type Parameters:
      T - The stream generic type
      Parameters:
      list - The list to sort
      Returns:
      The sorted stream
    • sortOrdered

      public static <T extends Ordered> Stream<T> sortOrdered(Stream<T> list)
      Sort the given stream.
      Type Parameters:
      T - The stream generic type
      Parameters:
      list - The list to sort
      Returns:
      The sorted stream
      Since:
      4.4.0
    • sortOrderedCollection

      public static <T extends Ordered> List<T> sortOrderedCollection(Collection<T> list)
      Sort the given list.
      Type Parameters:
      T - The type
      Parameters:
      list - The list to sort
      Returns:
      The sorted collection
      Since:
      4.4.0
    • sortOrdered

      public static <T extends Ordered> void sortOrdered(List<T> list)
      Sort the given list.
      Type Parameters:
      T - The type
      Parameters:
      list - The list to sort
      Since:
      4.4.0
    • reverseSortOrdered

      public static <T extends Ordered> void reverseSortOrdered(List<T> list)
      Sort the given list.
      Type Parameters:
      T - The type
      Parameters:
      list - The list to sort
      Since:
      4.4.0
    • reverseSort

      public static void reverseSort(List<?> list)
      Sort the given list.

      You should probably not use this method. Prefer calling list.sort(OrderUtil.REVERSE_COMPARATOR_ZERO) instead, which offers more intuitive behavior for beans that don't expose an ordering.

      Parameters:
      list - The list to sort
    • reverseSort

      public static void reverseSort(Object[] array)
      Sort the given array in reverse order.
      Parameters:
      array - The array to sort
    • sort

      public static void sort(Ordered... objects)
      Sort the given array.

      You should probably not use this method. Prefer calling Arrays.sort(objects, OrderUtil.COMPARATOR_ZERO) instead, which offers more intuitive behavior for beans that don't expose an ordering.

      Parameters:
      objects - The array to sort
    • sort

      public static void sort(Object[] objects)
      Sort the given array.

      You should probably not use this method. Prefer calling Arrays.sort(objects, OrderUtil.COMPARATOR_ZERO) instead, which offers more intuitive behavior for beans that don't expose an ordering.

      Parameters:
      objects - The array to sort
    • getOrder

      public static int getOrder(Object o)
      Get the order for the given bean instance or AnnotationMetadata object.

      You should probably not use this method. Prefer calling getOrderWithDefaultPrecedence(Object, int) with 0 instead, which offers more intuitive behavior for beans that don't expose an ordering.

      Parameters:
      o - The object
      Returns:
      Ordered.getOrder() when object is instance of Ordered, or the order of the AnnotationMetadata, or Ordered.LOWEST_PRECEDENCE if the parameter is neither of those.
    • getOrderWithDefaultPrecedence

      public static int getOrderWithDefaultPrecedence(Object o, int defaultPrecedence)
      Get the order for the given bean instance if it implements Ordered, or for the given AnnotationMetadata object.
      Parameters:
      o - The object
      defaultPrecedence - What to return if the object is neither an ordered bean nor an annotation metadata object.
      Returns:
      Ordered.getOrder() when object is instance of Ordered, or the order of the AnnotationMetadata, or defaultPrecedence if the parameter is neither of those.
    • getOrder

      public static int getOrder(AnnotationMetadata annotationMetadata, Object o)
      Get the order of the given object. Objects implementing Ordered have precedence over annotation metadata with Order.
      Parameters:
      annotationMetadata - The annotation metadata
      o - The object
      Returns:
      The order of the object. If no order is found, Ordered.LOWEST_PRECEDENCE is returned.
    • getOrder

      public static int getOrder(@NonNull @NonNull AnnotationMetadata annotationMetadata)
      Get the order for the given annotation metadata.
      Parameters:
      annotationMetadata - The metadata
      Returns:
      The order or zero if there is no Order annotation.
      Since:
      3.0.0
    • getOrder

      @Deprecated(since="4.4.0", forRemoval=true) public static int getOrder(Ordered o)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Inline method
      Get the order for the given Ordered object.
      Parameters:
      o - The ordered object
      Returns:
      the order