Skip to content

Commit ae21509

Browse files
committed
scijava-priority: move compare method into iface
It compares two Prioritized objects, so putting it in the Prioritized interface itself as a static method makes more sense than putting it into the bag of Priority constant values.
1 parent 471f836 commit ae21509

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

scijava-priority/src/main/java/org/scijava/priority/Prioritized.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

scijava-priority/src/main/java/org/scijava/priority/Priority.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -82,33 +82,4 @@ private Priority() {
8282
* </p>
8383
*/
8484
public static final double LAST = -1e300;
85-
86-
/**
87-
* Compares two {@link Prioritized} objects.
88-
* <p>
89-
* Note: this method provides a natural ordering that may be inconsistent with
90-
* equals. That is, two unequal objects may often have the same priority, and
91-
* thus return 0 when compared in this fashion. Hence, if this method is used
92-
* as a basis for implementing {@link Comparable#compareTo} or
93-
* {@link java.util.Comparator#compare}, that implementation may want to
94-
* impose logic beyond that of this method, for breaking ties, if a total
95-
* ordering consistent with equals is always required.
96-
* </p>
97-
*
98-
* @return -1 if {@code p1}'s priority is higher than {@code p2}'s, 1 if
99-
* {@code p2}'s priority is higher than {@code p1}'s, or 0 if they
100-
* have the same priority.
101-
*/
102-
public static <T extends Prioritized<T>> int compare(final Prioritized<T> p1,
103-
final Prioritized<T> p2)
104-
{
105-
final double priority1 = p1 == null ? Double.NEGATIVE_INFINITY : p1
106-
.priority();
107-
final double priority2 = p2 == null ? Double.NEGATIVE_INFINITY : p2
108-
.priority();
109-
if (priority1 == priority2) return 0;
110-
// NB: We invert the ordering here, so that large values come first,
111-
// rather than the typical natural ordering of smaller values first.
112-
return priority1 > priority2 ? -1 : 1;
113-
}
11485
}

0 commit comments

Comments
 (0)