23
23
*/
24
24
@ Slf4j
25
25
public final class CacheApiSettings {
26
- private static volatile CacheApiSettings INSTANCE ;
27
-
28
26
private final Map <Class <? extends CacheProvider >, CacheProvider > providers = Collections .synchronizedMap (new IdentityHashMap <>(16 ));
29
27
private final AtomicReference <Class <? extends CacheProvider >> defaultCacheProvider = new AtomicReference <>();
30
28
@@ -38,7 +36,7 @@ public final class CacheApiSettings {
38
36
private volatile MisconfigurationPolicy defaultMisconfigurationPolicy = MisconfigurationPolicy .IGNORE ;
39
37
40
38
private CacheApiSettings () {
41
- // restrict instantiation
39
+ this . populateProviders ();
42
40
}
43
41
44
42
/**
@@ -102,31 +100,14 @@ public void registerCacheProvider(@NonNull Class<? extends CacheProvider> cacheP
102
100
}
103
101
}
104
102
105
- /**
106
- * @return the cache api settings singleton
107
- */
108
- @ NotNull
109
- public static CacheApiSettings getInstance () {
110
- if (INSTANCE == null ) {
111
- synchronized (CacheApiSettings .class ) {
112
- if (INSTANCE == null ) {
113
- CacheApiSettings cacheApiSettings = new CacheApiSettings ();
114
- populateProviders (cacheApiSettings );
115
- INSTANCE = cacheApiSettings ;
116
- }
117
- }
118
- }
119
- return INSTANCE ;
120
- }
121
-
122
- private static void populateProviders (CacheApiSettings cacheApiSettings ) {
103
+ private void populateProviders () {
123
104
log .debug ("Xanthic: Registering canonical cache providers from the classpath..." );
124
105
125
106
AtomicInteger registered = new AtomicInteger ();
126
107
Consumer <String > loadImpl = (providerClass ) -> {
127
108
try {
128
109
Class <? extends CacheProvider > clazz = Class .forName (providerClass ).asSubclass (CacheProvider .class );
129
- cacheApiSettings . registerCacheProvider (clazz , null ); // lazy, init if needed
110
+ registerCacheProvider (clazz , null ); // lazy, init if needed
130
111
registered .incrementAndGet ();
131
112
} catch (ClassNotFoundException cx ) {
132
113
log .trace ("Xanthic: Could not find optional cache provider " + providerClass );
@@ -148,4 +129,16 @@ private static void populateProviders(CacheApiSettings cacheApiSettings) {
148
129
149
130
log .debug ("Xanthic: Loaded {} canonical cache provider(s) on settings construction!" , registered .get ());
150
131
}
132
+
133
+ /**
134
+ * @return the cache api settings singleton
135
+ */
136
+ @ NotNull
137
+ public static CacheApiSettings getInstance () {
138
+ return SingletonHolder .INSTANCE ;
139
+ }
140
+
141
+ private static class SingletonHolder {
142
+ private static final CacheApiSettings INSTANCE = new CacheApiSettings ();
143
+ }
151
144
}
0 commit comments