Skip to content

Commit 83d9045

Browse files
Javadoc-ed cloudevents-core (#182)
Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
1 parent 98a6b87 commit 83d9045

File tree

18 files changed

+150
-40
lines changed

18 files changed

+150
-40
lines changed

core/src/main/java/io/cloudevents/core/builder/CloudEventBuilder.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
import io.cloudevents.rw.CloudEventWriter;
2424

2525
import javax.annotation.Nonnull;
26+
import javax.annotation.ParametersAreNullableByDefault;
2627
import java.net.URI;
2728
import java.time.ZonedDateTime;
2829

2930
/**
3031
* Builder interface to build a {@link CloudEvent}.
3132
*/
33+
@ParametersAreNullableByDefault
3234
public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
3335

3436
/**
@@ -121,7 +123,7 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
121123
* @param value value of the extension attribute
122124
* @return self
123125
*/
124-
CloudEventBuilder withExtension(String key, String value);
126+
CloudEventBuilder withExtension(@Nonnull String key, String value);
125127

126128
/**
127129
* Set an extension with provided key and numeric value
@@ -130,7 +132,7 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
130132
* @param value value of the extension attribute
131133
* @return self
132134
*/
133-
CloudEventBuilder withExtension(String key, Number value);
135+
CloudEventBuilder withExtension(@Nonnull String key, Number value);
134136

135137
/**
136138
* Set an extension with provided key and boolean value
@@ -139,15 +141,15 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
139141
* @param value value of the extension attribute
140142
* @return self
141143
*/
142-
CloudEventBuilder withExtension(String key, boolean value);
144+
CloudEventBuilder withExtension(@Nonnull String key, boolean value);
143145

144146
/**
145147
* Add to the builder all the extension key/values of the provided extension
146148
*
147149
* @param extension materialized extension to set in the event
148150
* @return self
149151
*/
150-
CloudEventBuilder withExtension(Extension extension);
152+
CloudEventBuilder withExtension(@Nonnull Extension extension);
151153

152154
/**
153155
* Build the event
@@ -175,7 +177,7 @@ static io.cloudevents.core.v1.CloudEventBuilder v1() {
175177
* @param event event to bootstrap the builder
176178
* @return a new CloudEvent v1 builder filled with content of {@code event}
177179
*/
178-
static io.cloudevents.core.v1.CloudEventBuilder v1(CloudEvent event) {
180+
static io.cloudevents.core.v1.CloudEventBuilder v1(@Nonnull CloudEvent event) {
179181
return new io.cloudevents.core.v1.CloudEventBuilder(event);
180182
}
181183

@@ -190,7 +192,7 @@ static io.cloudevents.core.v03.CloudEventBuilder v03() {
190192
* @param event event to bootstrap the builder
191193
* @return a new CloudEvent v0.3 builder filled with content of {@code event}
192194
*/
193-
static io.cloudevents.core.v03.CloudEventBuilder v03(CloudEvent event) {
195+
static io.cloudevents.core.v03.CloudEventBuilder v03(@Nonnull CloudEvent event) {
194196
return new io.cloudevents.core.v03.CloudEventBuilder(event);
195197
}
196198

core/src/main/java/io/cloudevents/core/extensions/DistributedTracingExtension.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
import java.util.HashSet;
2727
import java.util.Set;
2828

29+
/**
30+
* This extension embeds context from Distributed Tracing so that distributed systems can include traces that span an event-driven system.
31+
*
32+
* @see <a href="https://github.com/cloudevents/spec/blob/master/extensions/distributed-tracing.md">https://github.com/cloudevents/spec/blob/master/extensions/distributed-tracing.md</a>
33+
*/
2934
public final class DistributedTracingExtension implements Extension {
3035

3136
public static final String TRACEPARENT = "traceparent";

core/src/main/java/io/cloudevents/core/format/EventDeserializationException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package io.cloudevents.core.format;
1919

20+
/**
21+
* Exception representing a deserialization error while using an {@link EventFormat}.
22+
*/
2023
public class EventDeserializationException extends RuntimeException {
2124
public EventDeserializationException(Throwable e) {
2225
super(e);

core/src/main/java/io/cloudevents/core/format/EventFormat.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,53 @@
1919

2020
import io.cloudevents.CloudEvent;
2121

22+
import javax.annotation.ParametersAreNonnullByDefault;
2223
import java.util.Collections;
2324
import java.util.Set;
2425

26+
/**
27+
* An <a href="https://github.com/cloudevents/spec/blob/v1.0/spec.md#event-format">Event format</a>
28+
* specifies how to serialize a CloudEvent as a sequence of bytes. <br/>
29+
* <p>
30+
* An implementation of this interface should support all specification versions of {@link CloudEvent}. <br/>
31+
* <p>
32+
* Implementations of this interface can be registered to the {@link io.cloudevents.core.provider.EventFormatProvider} to use them.
33+
*
34+
* @see io.cloudevents.core.provider.EventFormatProvider
35+
*/
36+
@ParametersAreNonnullByDefault
2537
public interface EventFormat {
2638

39+
/**
40+
* Serialize a {@link CloudEvent} to a byte array.
41+
*
42+
* @param event the event to serialize.
43+
* @return the byte representation of the provided event.
44+
* @throws EventSerializationException if something goes wrong during serialization.
45+
*/
2746
byte[] serialize(CloudEvent event) throws EventSerializationException;
2847

29-
CloudEvent deserialize(byte[] event) throws EventDeserializationException;
48+
/**
49+
* Deserialize a byte array to a {@link CloudEvent}.
50+
*
51+
* @param bytes the serialized event.
52+
* @return the deserialized event.
53+
* @throws EventDeserializationException if something goes wrong during deserialization.
54+
*/
55+
CloudEvent deserialize(byte[] bytes) throws EventDeserializationException;
3056

57+
/**
58+
* @return the set of content types this event format can deserialize. These content types are used
59+
* by the {@link io.cloudevents.core.provider.EventFormatProvider} to resolve an {@link EventFormat} starting
60+
* from the content type {@link String}.
61+
*/
3162
default Set<String> deserializableContentTypes() {
3263
return Collections.singleton(serializedContentType());
3364
}
3465

66+
/**
67+
* @return The content type to use when writing an event with this {@link EventFormat}.
68+
*/
3569
String serializedContentType();
3670

3771
}

core/src/main/java/io/cloudevents/core/format/EventSerializationException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package io.cloudevents.core.format;
1919

20+
/**
21+
* Exception representing a serialization error while using an {@link EventFormat}.
22+
*/
2023
public class EventSerializationException extends RuntimeException {
2124
public EventSerializationException(Throwable e) {
2225
super(e);

core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.cloudevents.core.builder.CloudEventBuilder;
2323
import io.cloudevents.rw.CloudEventRWException;
2424

25+
import javax.annotation.Nonnull;
2526
import java.net.URI;
2627
import java.util.HashMap;
2728
import java.util.Map;
@@ -75,22 +76,22 @@ public SELF withData(String dataContentType, URI dataSchema, byte[] data) {
7576
return this.self;
7677
}
7778

78-
public SELF withExtension(String key, String value) {
79+
public SELF withExtension(@Nonnull String key, String value) {
7980
this.extensions.put(key, value);
8081
return self;
8182
}
8283

83-
public SELF withExtension(String key, Number value) {
84+
public SELF withExtension(@Nonnull String key, Number value) {
8485
this.extensions.put(key, value);
8586
return self;
8687
}
8788

88-
public SELF withExtension(String key, boolean value) {
89+
public SELF withExtension(@Nonnull String key, boolean value) {
8990
this.extensions.put(key, value);
9091
return self;
9192
}
9293

93-
public SELF withExtension(Extension extension) {
94+
public SELF withExtension(@Nonnull Extension extension) {
9495
for (String key : extension.getKeys()) {
9596
Object value = extension.getValue(key);
9697
if (value != null) {

core/src/main/java/io/cloudevents/core/message/MessageReader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
import io.cloudevents.core.format.EventFormat;
2323
import io.cloudevents.rw.*;
2424

25+
import javax.annotation.ParametersAreNonnullByDefault;
26+
2527
/**
2628
* Represents a <a href="https://github.com/cloudevents/spec/blob/v1.0/spec.md#message">CloudEvent message</a>.
2729
*/
30+
@ParametersAreNonnullByDefault
2831
public interface MessageReader extends StructuredMessageReader, CloudEventReader {
2932

3033
/**

core/src/main/java/io/cloudevents/core/message/MessageWriter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
import io.cloudevents.rw.CloudEventWriter;
2525
import io.cloudevents.rw.CloudEventWriterFactory;
2626

27+
import javax.annotation.ParametersAreNonnullByDefault;
28+
2729
/**
2830
* Interface to write the {@link MessageReader} content (CloudEvents attributes, extensions and payload) to a new representation.
2931
*
3032
* @param <R> return value at the end of the write process.
3133
*/
34+
@ParametersAreNonnullByDefault
3235
public interface MessageWriter<CEV extends CloudEventWriter<R>, R> extends CloudEventWriterFactory<CEV, R>, StructuredMessageWriter<R> {
3336

3437
/**

core/src/main/java/io/cloudevents/core/message/StructuredMessageReader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
import io.cloudevents.core.message.impl.GenericStructuredMessageReader;
2323
import io.cloudevents.rw.CloudEventRWException;
2424

25+
import javax.annotation.ParametersAreNonnullByDefault;
26+
2527
/**
2628
* Represents a <a href="https://github.com/cloudevents/spec/blob/v1.0/spec.md#message">CloudEvent message</a> in structured mode.
2729
*/
2830
@FunctionalInterface
31+
@ParametersAreNonnullByDefault
2932
public interface StructuredMessageReader {
3033

3134
/**

core/src/main/java/io/cloudevents/core/message/StructuredMessageWriter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@
2020
import io.cloudevents.core.format.EventFormat;
2121
import io.cloudevents.rw.CloudEventRWException;
2222

23-
//TODO javadoc
23+
import javax.annotation.ParametersAreNonnullByDefault;
24+
25+
/**
26+
* Interface to write the {@link MessageReader} content (CloudEvents attributes, extensions and payload) to a new representation structured representation.
27+
*
28+
* @param <T> return value at the end of the write process.
29+
*/
30+
@ParametersAreNonnullByDefault
2431
@FunctionalInterface
2532
public interface StructuredMessageWriter<T> {
2633

27-
// TODO one day we'll convert this to some byte stream
34+
/**
35+
* Write an event using the provided {@link EventFormat}.
36+
*/
2837
T setEvent(EventFormat format, byte[] value) throws CloudEventRWException;
2938

3039
}

core/src/main/java/io/cloudevents/core/message/impl/MessageUtils.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,7 @@
3232
public class MessageUtils {
3333

3434
/**
35-
* Common flow to parse an incoming message that could be structured or binary
36-
*
37-
* @param contentTypeHeaderReader
38-
* @param structuredMessageFactory
39-
* @param specVersionHeaderReader
40-
* @param binaryMessageFactory
41-
* @param unknownMessageFactory
42-
* @return
35+
* Common flow to parse an incoming message that could be structured or binary.<br/>
4336
*/
4437
public static MessageReader parseStructuredOrBinaryMessage(
4538
Supplier<String> contentTypeHeaderReader,
@@ -72,7 +65,7 @@ public static MessageReader parseStructuredOrBinaryMessage(
7265
*
7366
* @param headerNameMapping mapper to generate the header name
7467
* @param <V> Header key type
75-
* @return
68+
* @return the generated mapping
7669
*/
7770
public static <V> Map<String, V> generateAttributesToHeadersMapping(Function<String, V> headerNameMapping) {
7871
return Stream.concat(

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,30 @@
1818
package io.cloudevents.core.provider;
1919

2020
import io.cloudevents.core.format.EventFormat;
21+
import io.cloudevents.lang.Nullable;
2122

23+
import javax.annotation.ParametersAreNonnullByDefault;
2224
import java.util.HashMap;
2325
import java.util.ServiceLoader;
2426
import java.util.stream.StreamSupport;
2527

28+
/**
29+
* Singleton holding the discovered {@link EventFormat} implementations through {@link ServiceLoader}.<br/>
30+
* <p>
31+
* You can resolve an event format using {@code EventFormatProvider.getInstance().resolveFormat(contentType)}.<br/>
32+
* <p>
33+
* You can programmatically add a new {@link EventFormat} implementation using {@link this#registerFormat(EventFormat)}.
34+
*/
35+
@ParametersAreNonnullByDefault
2636
public final class EventFormatProvider {
2737

2838
private static class SingletonContainer {
2939
private final static EventFormatProvider INSTANCE = new EventFormatProvider();
3040
}
3141

42+
/**
43+
* @return instance of {@link EventFormatProvider}
44+
*/
3245
public static EventFormatProvider getInstance() {
3346
return EventFormatProvider.SingletonContainer.INSTANCE;
3447
}
@@ -44,18 +57,30 @@ private EventFormatProvider() {
4457
).forEach(this::registerFormat);
4558
}
4659

60+
/**
61+
* Register a new {@link EventFormat} programmatically.
62+
*
63+
* @param format the new format to register
64+
*/
4765
public void registerFormat(EventFormat format) {
4866
for (String k : format.deserializableContentTypes()) {
4967
this.formats.put(k, format);
5068
}
5169
}
5270

53-
public EventFormat resolveFormat(String key) {
54-
int i = key.indexOf(';');
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(';');
5580
if (i != -1) {
56-
key = key.substring(0, i);
81+
contentType = contentType.substring(0, i);
5782
}
58-
return this.formats.get(key);
83+
return this.formats.get(contentType);
5984
}
6085

6186
}

0 commit comments

Comments
 (0)