|
17 | 17 |
|
18 | 18 | package io.cloudevents.core.provider;
|
19 | 19 |
|
20 |
| -import io.cloudevents.core.format.EventFormat; |
21 |
| -import io.cloudevents.lang.Nullable; |
22 |
| - |
23 |
| -import javax.annotation.ParametersAreNonnullByDefault; |
24 | 20 | import java.util.HashMap;
|
25 | 21 | import java.util.ServiceLoader;
|
| 22 | +import java.util.Set; |
| 23 | +import java.util.TreeSet; |
26 | 24 | import java.util.stream.StreamSupport;
|
27 | 25 |
|
| 26 | +import javax.annotation.ParametersAreNonnullByDefault; |
| 27 | + |
| 28 | +import io.cloudevents.core.format.EventFormat; |
| 29 | +import io.cloudevents.lang.Nullable; |
| 30 | + |
28 | 31 | /**
|
29 |
| - * Singleton holding the discovered {@link EventFormat} implementations through {@link ServiceLoader}. |
| 32 | + * Singleton holding the discovered {@link EventFormat} implementations through |
| 33 | + * {@link ServiceLoader}. |
30 | 34 | * <p>
|
31 |
| - * You can resolve an event format using {@code EventFormatProvider.getInstance().resolveFormat(contentType)}. |
| 35 | + * You can resolve an event format using |
| 36 | + * {@code EventFormatProvider.getInstance().resolveFormat(contentType)}. |
32 | 37 | * <p>
|
33 |
| - * You can programmatically add a new {@link EventFormat} implementation using {@link #registerFormat(EventFormat)}. |
| 38 | + * You can programmatically add a new {@link EventFormat} implementation using |
| 39 | + * {@link #registerFormat(EventFormat)}. |
34 | 40 | */
|
35 | 41 | @ParametersAreNonnullByDefault
|
36 | 42 | public final class EventFormatProvider {
|
37 | 43 |
|
38 |
| - private static class SingletonContainer { |
39 |
| - private final static EventFormatProvider INSTANCE = new EventFormatProvider(); |
40 |
| - } |
| 44 | + private static class SingletonContainer { |
| 45 | + private final static EventFormatProvider INSTANCE = new EventFormatProvider(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return instance of {@link EventFormatProvider} |
| 50 | + */ |
| 51 | + public static EventFormatProvider getInstance() { |
| 52 | + return EventFormatProvider.SingletonContainer.INSTANCE; |
| 53 | + } |
41 | 54 |
|
42 |
| - /** |
43 |
| - * @return instance of {@link EventFormatProvider} |
44 |
| - */ |
45 |
| - public static EventFormatProvider getInstance() { |
46 |
| - return EventFormatProvider.SingletonContainer.INSTANCE; |
47 |
| - } |
| 55 | + private final HashMap<String, EventFormat> formats; |
48 | 56 |
|
49 |
| - private final HashMap<String, EventFormat> formats; |
| 57 | + private EventFormatProvider() { |
| 58 | + this.formats = new HashMap<>(); |
50 | 59 |
|
51 |
| - private EventFormatProvider() { |
52 |
| - this.formats = new HashMap<>(); |
| 60 | + StreamSupport.stream(ServiceLoader.load(EventFormat.class).spliterator(), false) |
| 61 | + .forEach(this::registerFormat); |
| 62 | + } |
53 | 63 |
|
54 |
| - StreamSupport.stream( |
55 |
| - ServiceLoader.load(EventFormat.class).spliterator(), |
56 |
| - false |
57 |
| - ).forEach(this::registerFormat); |
58 |
| - } |
| 64 | + /** |
| 65 | + * Register a new {@link EventFormat} programmatically. |
| 66 | + * |
| 67 | + * @param format the new format to register |
| 68 | + */ |
| 69 | + public void registerFormat(EventFormat format) { |
| 70 | + for (String k : format.deserializableContentTypes()) { |
| 71 | + this.formats.put(k, format); |
| 72 | + } |
| 73 | + } |
59 | 74 |
|
60 |
| - /** |
61 |
| - * Register a new {@link EventFormat} programmatically. |
62 |
| - * |
63 |
| - * @param format the new format to register |
64 |
| - */ |
65 |
| - public void registerFormat(EventFormat format) { |
66 |
| - for (String k : format.deserializableContentTypes()) { |
67 |
| - this.formats.put(k, format); |
68 |
| - } |
69 |
| - } |
| 75 | + /** |
| 76 | + * Enumerate the supported content types. |
| 77 | + * |
| 78 | + * @return an alphabetically sorted list of content types |
| 79 | + */ |
| 80 | + public Set<String> getContentTypes() { |
| 81 | + Set<String> types = new TreeSet<>(); |
| 82 | + types.addAll(this.formats.keySet()); |
| 83 | + return types; |
| 84 | + } |
70 | 85 |
|
71 |
| - /** |
72 |
| - * Resolve an event format starting from the content type. |
73 |
| - * |
74 |
| - * @param contentType the content type to resolve the event format |
75 |
| - * @return null if no format was found for the provided content type |
76 |
| - */ |
77 |
| - @Nullable |
78 |
| - public EventFormat resolveFormat(String contentType) { |
79 |
| - int i = contentType.indexOf(';'); |
80 |
| - if (i != -1) { |
81 |
| - contentType = contentType.substring(0, i); |
82 |
| - } |
83 |
| - return this.formats.get(contentType); |
84 |
| - } |
| 86 | + /** |
| 87 | + * Resolve an event format starting from the content type. |
| 88 | + * |
| 89 | + * @param contentType the content type to resolve the event format |
| 90 | + * @return null if no format was found for the provided content type |
| 91 | + */ |
| 92 | + @Nullable |
| 93 | + public EventFormat resolveFormat(String contentType) { |
| 94 | + int i = contentType.indexOf(';'); |
| 95 | + if (i != -1) { |
| 96 | + contentType = contentType.substring(0, i); |
| 97 | + } |
| 98 | + return this.formats.get(contentType); |
| 99 | + } |
85 | 100 |
|
86 | 101 | }
|
0 commit comments