Skip to content

Commit f05418c

Browse files
author
Dave Syer
authored
Allow user to enumerate supported content types (#350)
Signed-off-by: Dave Syer <[email protected]>
1 parent 23cd08f commit f05418c

File tree

2 files changed

+68
-48
lines changed

2 files changed

+68
-48
lines changed

core/src/main/java/io/cloudevents/core/provider/EventFormatProvider.java

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,70 +17,85 @@
1717

1818
package io.cloudevents.core.provider;
1919

20-
import io.cloudevents.core.format.EventFormat;
21-
import io.cloudevents.lang.Nullable;
22-
23-
import javax.annotation.ParametersAreNonnullByDefault;
2420
import java.util.HashMap;
2521
import java.util.ServiceLoader;
22+
import java.util.Set;
23+
import java.util.TreeSet;
2624
import java.util.stream.StreamSupport;
2725

26+
import javax.annotation.ParametersAreNonnullByDefault;
27+
28+
import io.cloudevents.core.format.EventFormat;
29+
import io.cloudevents.lang.Nullable;
30+
2831
/**
29-
* Singleton holding the discovered {@link EventFormat} implementations through {@link ServiceLoader}.
32+
* Singleton holding the discovered {@link EventFormat} implementations through
33+
* {@link ServiceLoader}.
3034
* <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)}.
3237
* <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)}.
3440
*/
3541
@ParametersAreNonnullByDefault
3642
public final class EventFormatProvider {
3743

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+
}
4154

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;
4856

49-
private final HashMap<String, EventFormat> formats;
57+
private EventFormatProvider() {
58+
this.formats = new HashMap<>();
5059

51-
private EventFormatProvider() {
52-
this.formats = new HashMap<>();
60+
StreamSupport.stream(ServiceLoader.load(EventFormat.class).spliterator(), false)
61+
.forEach(this::registerFormat);
62+
}
5363

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+
}
5974

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+
}
7085

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+
}
85100

86101
}

core/src/test/java/io/cloudevents/core/provider/EventFormatProviderTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ void resolveCSVWithParams() {
3636
.isInstanceOf(CSVFormat.class);
3737
}
3838

39+
@Test
40+
void listTypes() {
41+
assertThat(EventFormatProvider.getInstance().getContentTypes()).hasSize(1);
42+
}
43+
3944
}

0 commit comments

Comments
 (0)