Skip to content

Commit c943118

Browse files
committed
Bootstrap may now return nul for single services.
Bootstrap weas simplified. Added priority method to bootstrap to enable replacement of bootstrap without any additinional dep. Removed former ServicePriority (duplicated) annotation.
1 parent 37dc790 commit c943118

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

src/main/java/javax/money/MonetaryCurrencies.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static MonetaryCurrenciesSingletonSpi loadMonetaryCurrenciesSingletonSpi
5252
() -> new DefaultMonetaryCurrenciesSingletonSpi());
5353
} catch (Exception e) {
5454
Logger.getLogger(MonetaryCurrencies.class.getName())
55-
.log(Level.SEVERE, "Failed to load MonetaryCurrenciesSingletonSpi, using default.", e);
55+
.log(Level.INFO, "Failed to load MonetaryCurrenciesSingletonSpi, using default.", e);
5656
return new DefaultMonetaryCurrenciesSingletonSpi();
5757
}
5858
}

src/main/java/javax/money/spi/Bootstrap.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,16 @@ public static <T> Collection<T> getServices(Class<T> serviceType) {
111111
* Delegate method for {@link ServiceProvider#getServices(Class)}.
112112
*
113113
* @param serviceType the service type.
114-
* @return the service found, never {@code null}.
114+
* @return the service found, or {@code null}.
115115
* @see ServiceProvider#getServices(Class)
116116
*/
117117
public static <T> T getService(Class<T> serviceType) {
118118
List<T> services = getServiceProvider().getServices(serviceType);
119119
return services
120120
.stream()
121-
.findFirst()
122-
.orElseThrow(
123-
() -> new MonetaryException("No such service found: "
124-
+ serviceType));
121+
.sorted((o1, o2) -> o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName()))
122+
.findFirst()
123+
.orElse(null);
125124
}
126125

127126
}

src/main/java/javax/money/spi/DefaultServiceProvider.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class DefaultServiceProvider implements ServiceProvider {
2626
/** List of services loaded, per class. */
2727
private final ConcurrentHashMap<Class, List<Object>> servicesLoaded = new ConcurrentHashMap<>();
2828

29+
@Override
30+
public int getPriority() {
31+
return 0;
32+
}
33+
2934
/**
3035
* Loads and registers services.
3136
*

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@
2020
* @author Anatole Tresch
2121
* @author Werner Keil
2222
*/
23-
@FunctionalInterface
2423
public interface ServiceProvider {
2524

25+
/**
26+
* This method allows to define a priority for a registered ServiceProvider instance. When multiple providers are
27+
* registered in the system the provider with the highest priority value is taken.
28+
*
29+
* @return the provider's priority (default is 0).
30+
*/
31+
public int getPriority();
32+
2633
/**
2734
* Access a list of services, given its type. The bootstrap mechanism should
2835
* order the instance for precedence, hereby the most significant should be
@@ -35,4 +42,17 @@ public interface ServiceProvider {
3542
*/
3643
<T> List<T> getServices(Class<T> serviceType);
3744

45+
/**
46+
* Access a single services, given its type. The bootstrap mechanism should
47+
* order the instance for precedence, hereby the most significant should be
48+
* first in order and returned. If no such services are found, null is
49+
* returned.
50+
*
51+
* @param serviceType the service type.
52+
* @return The instance, (with highest precedence) or {@code null}, if no such service is available.
53+
*/
54+
default <T> T getService(Class<T> serviceType) {
55+
return getServices(serviceType).stream().findFirst().orElse(null);
56+
}
57+
3858
}

src/test/java/javax/money/spi/BootstrapTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package javax.money.spi;
22

3-
import static org.testng.Assert.assertEquals;
4-
import static org.testng.Assert.assertFalse;
5-
import static org.testng.Assert.assertNotNull;
6-
import static org.testng.Assert.assertTrue;
7-
83
import java.util.Arrays;
94
import java.util.Collection;
105
import java.util.List;
@@ -14,6 +9,8 @@
149

1510
import javax.money.MonetaryException;
1611

12+
import static org.testng.Assert.*;
13+
1714
/**
1815
* Created by Anatole on 04.03.14.
1916
*/
@@ -65,9 +62,9 @@ public void testGetService() throws Exception {
6562
assertTrue(num.equals(5));
6663
}
6764

68-
@Test(expectedExceptions = {MonetaryException.class})
65+
@Test
6966
public void testGetService_BadCase() throws Exception {
70-
Bootstrap.getService(Locale.class);
67+
assertNull(Bootstrap.getService(Locale.class));
7168
}
7269

7370
public final static class TestServiceProvider extends DefaultServiceProvider

0 commit comments

Comments
 (0)