-
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathServiceRegistry.java
More file actions
430 lines (384 loc) · 12.4 KB
/
ServiceRegistry.java
File metadata and controls
430 lines (384 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import static java.util.stream.Collectors.toUnmodifiableMap;
import java.util.*;
import org.jspecify.annotations.Nullable;
import io.jooby.exception.RegistryException;
import jakarta.inject.Provider;
/**
* Default registry that uses a simply key/value mechanism for storing and retrieving services.
*
* @author edgar
* @since 2.0.0
*/
public interface ServiceRegistry extends Registry {
/**
* Map binder, allow to partially register map entries.
*
* @param <K> Map key.
* @param <V> Map value.
*/
class MapBinder<K, V> implements Provider<Map<K, V>> {
private final Map<K, Provider<V>> services;
private MapBinder() {
this.services = new HashMap<>();
}
/**
* Put a service into a map.
*
* @param key Key.
* @param service Service value.
* @return This binder.
*/
public MapBinder<K, V> put(K key, V service) {
return put(key, () -> service);
}
/**
* Put a service into a map.
*
* @param key Key.
* @param service Service value.
* @return This binder.
*/
public MapBinder<K, V> put(K key, Provider<V> service) {
services.put(key, service);
return this;
}
@Override
public Map<K, V> get() {
return services.entrySet().stream()
.collect(toUnmodifiableMap(Map.Entry::getKey, e -> e.getValue().get()));
}
}
/**
* List/Set binder, allow to partially register service and fetch them all as list/set.
*
* @param <T> Item type.
*/
abstract class MultiBinder<T> implements Provider<Collection<T>> {
protected final Collection<Provider<T>> services;
private MultiBinder(Collection<Provider<T>> services) {
this.services = services;
}
/**
* Add a service to final list.
*
* @param service Service to add.
* @return This binder.
*/
public MultiBinder<T> add(T service) {
return add(() -> service);
}
/**
* Add a service to final list.
*
* @param service Service to add.
* @return This binder.
*/
public MultiBinder<T> add(Provider<T> service) {
services.add(service);
return this;
}
static <T> MultiBinder<T> list() {
return new MultiBinder<T>(new ArrayList<>()) {
@SuppressWarnings("unchecked")
@Override
public List<T> get() {
return (List<T>) List.of(services.stream().map(Provider::get).toArray());
}
};
}
static <T> MultiBinder<T> set() {
return new MultiBinder<T>(new HashSet<>()) {
@SuppressWarnings("unchecked")
@Override
public Set<T> get() {
return (Set<T>) Set.of(services.stream().map(Provider::get).distinct().toArray());
}
};
}
}
/**
* Registered service keys.
*
* @return Service keys.
*/
Set<ServiceKey<?>> keySet();
/**
* Registered service entries.
*
* @return Service entries.
*/
Set<Map.Entry<ServiceKey<?>, Provider<?>>> entrySet();
/**
* Retrieve a service/resource by key.
*
* @param key Service/resource key.
* @param <T> Service/resource type.
* @return Service.
* @throws RegistryException If there was a runtime failure while providing an instance.
*/
default <T> T get(ServiceKey<T> key) {
T service = getOrNull(key);
if (service == null) {
throw new RegistryException("Service not found: " + key);
}
return service;
}
/**
* Retrieve a service/resource by key.
*
* @param type Service/resource key.
* @param <T> Service/resource type.
* @return Service.
* @throws RegistryException If there was a runtime failure while providing an instance.
*/
default <T> T get(Class<T> type) {
return get(ServiceKey.key(type));
}
/**
* Retrieve a service/resource by key.
*
* @param type Service/resource key.
* @param <T> Service/resource type.
* @return Service.
* @throws RegistryException If there was a runtime failure while providing an instance.
*/
default <T> T get(Reified<T> type) {
return get(ServiceKey.key(type));
}
/**
* Retrieve an existing service or <code>null</code> if not exists.
*
* @param type Service/resource key.
* @param <T> Service/resource type.
* @return Service or <code>null</code>.
*/
default @Nullable <T> T getOrNull(Reified<T> type) {
return getOrNull(ServiceKey.key(type));
}
/**
* Retrieve an existing service or <code>null</code> if not exists.
*
* @param type Service/resource key.
* @param <T> Service/resource type.
* @return Service or <code>null</code>.
*/
default @Nullable <T> T getOrNull(Class<T> type) {
return getOrNull(ServiceKey.key(type));
}
/**
* Retrieve an existing service or <code>null</code> if not exists.
*
* @param key Service/resource key.
* @param <T> Service/resource type.
* @return Service or <code>null</code>.
*/
@Nullable <T> T getOrNull(ServiceKey<T> key);
/**
* List binder. You can gradually add service of the same type and retrieve them all as list.
*
* @param type Type.
* @return A new list binder.
* @param <T> Service type.
*/
default <T> MultiBinder<T> listOf(Class<T> type) {
return multiBinder(Reified.list(type), MultiBinder.list());
}
/**
* List binder. You can gradually add service of the same type and retrieve them all as list.
*
* @param type Type.
* @return A new list binder.
* @param <T> Service type.
*/
default <T> MultiBinder<T> listOf(Reified<T> type) {
return multiBinder(Reified.list(type.getType()), MultiBinder.list());
}
/**
* Set binder. You can gradually add service of the same type and retrieve them all as set.
*
* @param type Type.
* @return A new set binder.
* @param <T> Service type.
*/
default <T> MultiBinder<T> setOf(Class<T> type) {
return multiBinder(Reified.set(type), MultiBinder.set());
}
/**
* Set binder. You can gradually add service of the same type and retrieve them all as set.
*
* @param type Type.
* @return A new set binder.
* @param <T> Service type.
*/
default <T> MultiBinder<T> setOf(Reified<T> type) {
return multiBinder(Reified.set(type.getType()), MultiBinder.set());
}
/**
* Map binder. You can gradually put service of the same type and retrieve them all as map.
*
* @param keyType Key Type.
* @param valueType Service Type.
* @return A new map binder.
* @param <K> Key type.
* @param <V> Service type.
*/
default <K, V> MapBinder<K, V> mapOf(Class<K> keyType, Class<V> valueType) {
return multiBinder(Reified.map(keyType, valueType), new MapBinder<>());
}
/**
* Map binder. You can gradually put service of the same type and retrieve them all as map.
*
* @param keyType Key Type.
* @param valueType Service Type.
* @return A new map binder.
* @param <K> Key type.
* @param <V> Service type.
*/
default <K, V> MapBinder<K, V> mapOf(Class<K> keyType, Reified<V> valueType) {
return multiBinder(Reified.map(keyType, valueType.getType()), new MapBinder<>());
}
@SuppressWarnings({"rawtypes", "unchecked"})
private <P extends Provider> P multiBinder(Reified reified, P multibinder) {
ServiceKey<?> key = ServiceKey.key(reified);
var existing = putIfAbsent(key, multibinder);
if (existing != null) {
if (multibinder.getClass().isInstance(existing)) {
multibinder = (P) existing;
} else {
throw new RegistryException("Mismatch type for key: " + reified);
}
}
return multibinder;
}
/**
* Put a service in this registry. This method overrides any previous registered service.
*
* @param type Service/resource key.
* @param service Service instance.
* @param <T> Service type.
* @return Previously registered service or <code>null</code>.
*/
default @Nullable <T> T put(Class<T> type, Provider<T> service) {
return put(ServiceKey.key(type), service);
}
/**
* Put a service in this registry. This method overrides any previous registered service.
*
* @param key Service/resource key.
* @param service Service instance.
* @param <T> Service type.
* @return Previously registered service or <code>null</code>.
*/
@Nullable <T> T put(ServiceKey<T> key, Provider<T> service);
/**
* Put a service in this registry. This method overrides any previous registered service.
*
* @param type Service/resource key.
* @param service Service instance.
* @param <T> Service type.
* @return Previously registered service or <code>null</code>.
*/
default @Nullable <T> T put(Class<T> type, T service) {
return put(ServiceKey.key(type), service);
}
/**
* Put a service in this registry. This method overrides any previous registered service.
*
* @param key Service/resource key.
* @param service Service instance.
* @param <T> Service type.
* @return Previously registered service or <code>null</code>.
*/
@Nullable <T> T put(ServiceKey<T> key, T service);
/**
* If the specified key is not already associated with a service (or is mapped to null) associates
* it with the given value and returns null, else returns the current value.
*
* @param key Service/resource key.
* @param service Service instance.
* @param <T> Service type.
* @return Previously registered service or <code>null</code>.
*/
@Nullable <T> T putIfAbsent(ServiceKey<T> key, T service);
/**
* If the specified key is not already associated with a service (or is mapped to null) associates
* it with the given value and returns null, else returns the current value.
*
* @param type Service/resource key.
* @param service Service instance.
* @param <T> Service type.
* @return Previously registered service or <code>null</code>.
*/
default @Nullable <T> T putIfAbsent(Class<T> type, T service) {
return putIfAbsent(ServiceKey.key(type), service);
}
/**
* If the specified key is not already associated with a service (or is mapped to null) associates
* it with the given value and returns null, else returns the current value.
*
* @param type Service/resource key.
* @param service Service instance.
* @param <T> Service type.
* @return Previously registered service or <code>null</code>.
*/
default @Nullable <T> T putIfAbsent(Class<T> type, Provider<T> service) {
return putIfAbsent(ServiceKey.key(type), service);
}
/**
* If the specified key is not already associated with a service (or is mapped to null) associates
* it with the given value and returns null, else returns the current value.
*
* @param type Service/resource key.
* @param service Service instance.
* @param <T> Service type.
* @return Previously registered service or <code>null</code>.
*/
default @Nullable <T> T putIfAbsent(Reified<T> type, T service) {
return putIfAbsent(ServiceKey.key(type), service);
}
/**
* If the specified key is not already associated with a service (or is mapped to null) associates
* it with the given value and returns null, else returns the current value.
*
* @param type Service/resource key.
* @param service Service instance.
* @param <T> Service type.
* @return Previously registered service or <code>null</code>.
*/
default @Nullable <T> T putIfAbsent(Reified<T> type, Provider<T> service) {
return putIfAbsent(ServiceKey.key(type), service);
}
/**
* If the specified key is not already associated with a service (or is mapped to null) associates
* it with the given value and returns null, else returns the current value.
*
* @param key Service/resource key.
* @param service Service instance.
* @param <T> Service type.
* @return Previously registered service or <code>null</code>.
*/
@Nullable <T> T putIfAbsent(ServiceKey<T> key, Provider<T> service);
default @Override <T> T require(Class<T> type) {
return get(ServiceKey.key(type));
}
default @Override <T> T require(Class<T> type, String name) {
return get(ServiceKey.key(type, name));
}
default @Override <T> T require(ServiceKey<T> key) throws RegistryException {
return get(key);
}
@Override
default <T> T require(Reified<T> type, String name) throws RegistryException {
return get(ServiceKey.key(type, name));
}
@Override
default <T> T require(Reified<T> type) throws RegistryException {
return get(ServiceKey.key(type));
}
}