Skip to content

Commit 2c04476

Browse files
committed
195: Priority not for sorting in ServiceProvider?
Task-Url: #195
1 parent 1c2055f commit 2c04476

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

src/main/java/javax/measure/spi/ServiceProvider.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private static final class Selector implements Predicate<ServiceProvider>, Compa
204204
priorityGetter = jakartaPriorityAnnotation.getMethod("value", (Class[]) null);
205205
} catch (ClassNotFoundException e) {
206206
// Ignore since Jakarta Annotations is an optional dependency.
207-
}
207+
}
208208
} catch (NoSuchMethodException e) {
209209
// Should never happen since value() is a standard public method of those annotations.
210210
throw new ServiceConfigurationError("Cannot get annotation value", e);
@@ -214,7 +214,7 @@ private static final class Selector implements Predicate<ServiceProvider>, Compa
214214
/**
215215
* Returns {@code true} if the given service provider has the name we are looking for.
216216
* This method shall be invoked only if a non-null name has been specified to the constructor.
217-
* This method looks for the {@value #NAMED_ANNOTATION} annotation, and if none are found fallbacks on
217+
* This method looks for the {@value #NAMED_ANNOTATION} annotation or {@value #JAKARTA_NAMED_ANNOTATION} annotation, and if none are found fallbacks on
218218
* {@link ServiceProvider#toString()}.
219219
*/
220220
@Override
@@ -248,7 +248,7 @@ public boolean test(ServiceProvider provider) {
248248
private int priority(ServiceProvider provider) {
249249
if (priorityGetter != null) {
250250
Annotation a = null;
251-
if (priorityAnnotation != null) {
251+
if (priorityAnnotation != null) {
252252
a = provider.getClass().getAnnotation(priorityAnnotation);
253253
} else if (jakartaPriorityAnnotation != null) {
254254
a = provider.getClass().getAnnotation(jakartaPriorityAnnotation);
@@ -269,9 +269,7 @@ private int priority(ServiceProvider provider) {
269269
*/
270270
@Override
271271
public int compare(final ServiceProvider p1, final ServiceProvider p2) {
272-
final int i1 = priority(p1);
273-
final int i2 = priority(p2);
274-
return Integer.compare(i2, i1); // reverse order, higher number first.
272+
return Integer.compare(priority(p2), priority(p1)); // reverse order, higher number first.
275273
}
276274

277275
/**
@@ -359,7 +357,7 @@ public static ServiceProvider of(String name) {
359357
if (first.isPresent()) {
360358
return first.get();
361359
} else {
362-
throw new IllegalArgumentException("No measurement ServiceProvider " + name + " found .");
360+
throw new IllegalArgumentException("No Measurement ServiceProvider " + name + " found .");
363361
}
364362
}
365363

@@ -384,7 +382,7 @@ public static final ServiceProvider current() {
384382
if (first.isPresent()) {
385383
p = first.get();
386384
} else {
387-
throw new IllegalStateException("No measurement ServiceProvider found.");
385+
throw new IllegalStateException("No Measurement ServiceProvider found.");
388386
}
389387
p = setDefault(p);
390388
}

src/main/jdk9/javax/measure/spi/ServiceProvider.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
* All the methods in this class are safe to use by multiple concurrent threads.
5555
* </p>
5656
*
57-
* @version 2.0, November 4, 2020
57+
* @version 2.1, November 16, 2020
5858
* @author Werner Keil
5959
* @author Martin Desruisseaux
6060
* @since 1.0
@@ -104,9 +104,9 @@ protected ServiceProvider() {
104104
* Allows to define a priority for a registered {@code ServiceProvider} instance.
105105
* When multiple providers are registered in the system, the provider with the highest priority value is taken.
106106
*
107-
* <p>If the {@value #PRIORITY_ANNOTATION} annotation (from JSR-250) is present on the {@code ServiceProvider}
108-
* implementation class, then that annotation is taken and this {@code getPriority()} method is ignored.
109-
* Otherwise – if the {@code Priority} annotation is absent – this method is used as a fallback.</p>
107+
* <p>If the {@value #PRIORITY_ANNOTATION} annotation (from JSR-250) or {@value #JAKARTA_PRIORITY_ANNOTATION} annotation (from Jakarta Annotations) is present on the {@code ServiceProvider}
108+
* implementation class, then that annotation (first if both were present) is taken and this {@code getPriority()} method is ignored.
109+
* Otherwise – if a {@code Priority} annotation is absent – this method is used as a fallback.</p>
110110
*
111111
* @return the provider's priority (default is 0).
112112
*/
@@ -212,14 +212,19 @@ private static final class Selector implements Predicate<ServiceProvider>, Compa
212212
/**
213213
* Returns {@code true} if the given service provider has the name we are looking for.
214214
* This method shall be invoked only if a non-null name has been specified to the constructor.
215-
* This method looks for the {@value #NAMED_ANNOTATION} annotation, and if none are found fallbacks on
215+
* This method looks for the {@value #NAMED_ANNOTATION} annotation or {@value #JAKARTA_NAMED_ANNOTATION} annotation, and if none are found fallbacks on
216216
* {@link ServiceProvider#toString()}.
217217
*/
218218
@Override
219219
public boolean test(ServiceProvider provider) {
220220
Object value = null;
221221
if (nameGetter != null) {
222-
Annotation a = provider.getClass().getAnnotation(namedAnnotation);
222+
Annotation a = null;
223+
if (namedAnnotation != null) {
224+
a = provider.getClass().getAnnotation(namedAnnotation);
225+
} else if (jakartaNamedAnnotation != null) {
226+
a = provider.getClass().getAnnotation(jakartaNamedAnnotation);
227+
}
223228
if (a != null) try {
224229
value = nameGetter.invoke(a, (Object[]) null);
225230
} catch (IllegalAccessException | InvocationTargetException e) {
@@ -235,12 +240,17 @@ public boolean test(ServiceProvider provider) {
235240

236241
/**
237242
* Returns the priority of the given service provider.
238-
* This method looks for the {@value #PRIORITY_ANNOTATION} annotation,
239-
* and if none are found fallbacks on {@link ServiceProvider#getPriority()}.
243+
* This method looks for the {@value #PRIORITY_ANNOTATION} annotation or {@value #JAKARTA_PRIORITY_ANNOTATION},
244+
* and if none are found falls back on {@link ServiceProvider#getPriority()}.
240245
*/
241246
private int priority(ServiceProvider provider) {
242247
if (priorityGetter != null) {
243-
Annotation a = provider.getClass().getAnnotation(priorityAnnotation);
248+
Annotation a = null;
249+
if (priorityAnnotation != null) {
250+
a = provider.getClass().getAnnotation(priorityAnnotation);
251+
} else if (jakartaPriorityAnnotation != null) {
252+
a = provider.getClass().getAnnotation(jakartaPriorityAnnotation);
253+
}
244254
if (a != null) try {
245255
return (Integer) priorityGetter.invoke(a, (Object[]) null);
246256
} catch (IllegalAccessException | InvocationTargetException e) {
@@ -257,7 +267,7 @@ private int priority(ServiceProvider provider) {
257267
*/
258268
@Override
259269
public int compare(final ServiceProvider p1, final ServiceProvider p2) {
260-
return Integer.compare(priority(p1), priority(p2));
270+
return Integer.compare(priority(p2), priority(p1)); // reverse order, higher number first.
261271
}
262272

263273
/**
@@ -345,7 +355,7 @@ public static ServiceProvider of(String name) {
345355
if (first.isPresent()) {
346356
return first.get();
347357
} else {
348-
throw new IllegalArgumentException("No measurement ServiceProvider " + name + " found .");
358+
throw new IllegalArgumentException("No Measurement ServiceProvider " + name + " found .");
349359
}
350360
}
351361

@@ -370,7 +380,7 @@ public static final ServiceProvider current() {
370380
if (first.isPresent()) {
371381
p = first.get();
372382
} else {
373-
throw new IllegalStateException("No measurement ServiceProvider found.");
383+
throw new IllegalStateException("No Measurement ServiceProvider found.");
374384
}
375385
p = setDefault(p);
376386
}

0 commit comments

Comments
 (0)