@@ -50,17 +50,40 @@ default int compareTo(final T that) {
5050 if (that == null ) return 1 ;
5151
5252 // compare priorities
53- final int priorityCompare = Priority . compare (this , that );
53+ final int priorityCompare = compare (this , that );
5454 if (priorityCompare != 0 ) return priorityCompare ;
5555
56- // compare classes
57- // TODO: This functionality is copied from ClassUtils.compare(Class<?> c1,
58- // Class<?> c2). We should use this method again when it is migrated to
59- // SciJava
60- // 3
56+ // compare class names as a tiebreaker
6157 String thisName = getClass ().getName ();
6258 String thatName = that .getClass ().getName ();
6359 return thisName .compareTo (thatName );
6460 }
6561
62+
63+ /**
64+ * Compares two {@link Prioritized} objects.
65+ * <p>
66+ * Note: this method provides a natural ordering that may be inconsistent with
67+ * equals. That is, two unequal objects may often have the same priority, and
68+ * thus return 0 when compared in this fashion. Hence, if this method is used
69+ * as a basis for implementing {@link Comparable#compareTo} or
70+ * {@link java.util.Comparator#compare}, that implementation may want to
71+ * impose logic beyond that of this method, for breaking ties, if a total
72+ * ordering consistent with equals is always required.
73+ * </p>
74+ *
75+ * @return -1 if {@code p1}'s priority is higher than {@code p2}'s, 1 if
76+ * {@code p2}'s priority is higher than {@code p1}'s, or 0 if they
77+ * have the same priority.
78+ */
79+ static <T extends Prioritized <T >> int compare (
80+ final Prioritized <T > p1 , final Prioritized <T > p2 )
81+ {
82+ final double priority1 = p1 == null ? Double .NEGATIVE_INFINITY : p1 .priority ();
83+ final double priority2 = p2 == null ? Double .NEGATIVE_INFINITY : p2 .priority ();
84+ if (priority1 == priority2 ) return 0 ;
85+ // NB: We invert the ordering here, so that large values come first,
86+ // rather than the typical natural ordering of smaller values first.
87+ return priority1 > priority2 ? -1 : 1 ;
88+ }
6689}
0 commit comments