Class PriorityComparator

  • All Implemented Interfaces:
    java.util.Comparator<java.lang.Object>

    public class PriorityComparator
    extends java.lang.Object
    implements java.util.Comparator<java.lang.Object>
    A Comparator implementation that sorts objects based on the value of the Priority annotation.

    If an object does not have the Priority annotation, it is treated as having a default priority value of -1. If both objects lack the annotation, they are considered equal in priority.

    Example Usage

    
     List<Object> list = new ArrayList<>();
     list.add(new HighPriorityService());
     list.add(new LowPriorityService());
     Collections.sort(list, PriorityComparator.INSTANCE);
     

    Note: The comparison is consistent with equals only if the compared objects are of the same class or both lack the Priority annotation.

    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    Priority
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(java.lang.Object o1, java.lang.Object o2)
      Compares two objects based on the value of their Priority annotation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Comparator

        equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
    • Constructor Detail

      • PriorityComparator

        public PriorityComparator()
    • Method Detail

      • compare

        public int compare​(java.lang.Object o1,
                           java.lang.Object o2)
        Compares two objects based on the value of their Priority annotation.

        Example Usage

        
           PriorityComparator comparator = PriorityComparator.INSTANCE;
           int result = comparator.compare(serviceA, serviceB);
           if (result < 0) {
               System.out.println("serviceA has higher priority");
           }
         
        Specified by:
        compare in interface java.util.Comparator<java.lang.Object>
        Parameters:
        o1 - the first object to compare
        o2 - the second object to compare
        Returns:
        a negative integer, zero, or a positive integer as the first object's priority is less than, equal to, or greater than the second object's priority
        Since:
        1.0.0