Skip to content

Commit f9e31ef

Browse files
Introduce CloudEventData (#250)
* Introduce CloudEventData Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Javadocs Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Removed to Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Applied changes throughout the sdk Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Fix javadoc Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
1 parent a09b03b commit f9e31ef

File tree

26 files changed

+196
-68
lines changed

26 files changed

+196
-68
lines changed

api/src/main/java/io/cloudevents/CloudEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ public interface CloudEvent extends CloudEventAttributes, CloudEventExtensions {
2828
* The event data
2929
*/
3030
@Nullable
31-
byte[] getData();
31+
CloudEventData getData();
3232
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.cloudevents;
2+
3+
/**
4+
* Interface that defines a wrapper for CloudEvent data.
5+
*/
6+
public interface CloudEventData {
7+
8+
/**
9+
* @return this CloudEventData, represented as bytes. Note: this operation may be expensive, depending on the internal representation of data
10+
*/
11+
byte[] toBytes();
12+
13+
}

api/src/main/java/io/cloudevents/rw/CloudEventWriter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package io.cloudevents.rw;
1919

20+
import io.cloudevents.CloudEventData;
21+
2022
/**
2123
* Interface to write the content (CloudEvents attributes, extensions and payload) from a
2224
* {@link io.cloudevents.rw.CloudEventReader} to a new representation.
@@ -30,7 +32,7 @@ public interface CloudEventWriter<R> extends CloudEventAttributesWriter, CloudEv
3032
*
3133
* @return an eventual return value
3234
*/
33-
R end(byte[] value) throws CloudEventRWException;
35+
R end(CloudEventData data) throws CloudEventRWException;
3436

3537
/**
3638
* End the visit

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package io.cloudevents.core.builder;
1919

2020
import io.cloudevents.CloudEvent;
21+
import io.cloudevents.CloudEventData;
2122
import io.cloudevents.Extension;
2223
import io.cloudevents.SpecVersion;
2324
import io.cloudevents.rw.CloudEventWriter;
@@ -116,6 +117,33 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
116117
*/
117118
CloudEventBuilder withData(String dataContentType, URI dataSchema, byte[] data);
118119

120+
/**
121+
* Set the {@code data} of the event
122+
*
123+
* @param data data of the event
124+
* @return self
125+
*/
126+
CloudEventBuilder withData(CloudEventData data);
127+
128+
/**
129+
* Set the {@code datacontenttype} and {@code data} of the event
130+
*
131+
* @param dataContentType datacontenttype of the event
132+
* @param data data of the event
133+
* @return self
134+
*/
135+
CloudEventBuilder withData(String dataContentType, CloudEventData data);
136+
137+
/**
138+
* Set the {@code datacontenttype}, {@code dataschema} and {@code data} of the event
139+
*
140+
* @param dataContentType datacontenttype of the event
141+
* @param dataSchema dataschema of the event
142+
* @param data data of the event
143+
* @return self
144+
*/
145+
CloudEventBuilder withData(String dataContentType, URI dataSchema, CloudEventData data);
146+
119147
/**
120148
* Set an extension with provided key and string value
121149
*
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.cloudevents.core.data;
2+
3+
import io.cloudevents.CloudEventData;
4+
5+
import java.util.Arrays;
6+
import java.util.Objects;
7+
8+
public class BytesCloudEventData implements CloudEventData {
9+
10+
private final byte[] value;
11+
12+
public BytesCloudEventData(byte[] value) {
13+
Objects.requireNonNull(value);
14+
this.value = value;
15+
}
16+
17+
@Override
18+
public byte[] toBytes() {
19+
return this.value;
20+
}
21+
22+
@Override
23+
public boolean equals(Object o) {
24+
if (this == o) return true;
25+
if (o == null || getClass() != o.getClass()) return false;
26+
BytesCloudEventData that = (BytesCloudEventData) o;
27+
return Arrays.equals(value, that.value);
28+
}
29+
30+
@Override
31+
public int hashCode() {
32+
return Arrays.hashCode(value);
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "BytesCloudEventData{" +
38+
"value=" + Arrays.toString(value) +
39+
'}';
40+
}
41+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package io.cloudevents.core.impl;
1919

2020
import io.cloudevents.CloudEvent;
21+
import io.cloudevents.CloudEventData;
2122
import io.cloudevents.rw.*;
2223

2324
import java.util.HashMap;
@@ -26,16 +27,16 @@
2627

2728
public abstract class BaseCloudEvent implements CloudEvent, CloudEventReader {
2829

29-
private final byte[] data;
30+
private final CloudEventData data;
3031
protected final Map<String, Object> extensions;
3132

32-
protected BaseCloudEvent(byte[] data, Map<String, Object> extensions) {
33+
protected BaseCloudEvent(CloudEventData data, Map<String, Object> extensions) {
3334
this.data = data;
3435
this.extensions = extensions != null ? extensions : new HashMap<>();
3536
}
3637

3738
@Override
38-
public byte[] getData() {
39+
public CloudEventData getData() {
3940
return this.data;
4041
}
4142

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
package io.cloudevents.core.impl;
1919

2020
import io.cloudevents.CloudEvent;
21+
import io.cloudevents.CloudEventData;
2122
import io.cloudevents.Extension;
2223
import io.cloudevents.core.builder.CloudEventBuilder;
24+
import io.cloudevents.core.data.BytesCloudEventData;
2325
import io.cloudevents.rw.CloudEventRWException;
2426

2527
import javax.annotation.Nonnull;
@@ -32,7 +34,7 @@ public abstract class BaseCloudEventBuilder<SELF extends BaseCloudEventBuilder<S
3234
// This is a little trick for enabling fluency
3335
private final SELF self;
3436

35-
protected byte[] data;
37+
protected CloudEventData data;
3638
protected Map<String, Object> extensions;
3739

3840
@SuppressWarnings("unchecked")
@@ -59,7 +61,7 @@ public BaseCloudEventBuilder(CloudEvent event) {
5961
// to encode data
6062

6163
public SELF withData(byte[] data) {
62-
this.data = data;
64+
this.data = new BytesCloudEventData(data);
6365
return this.self;
6466
}
6567

@@ -76,6 +78,24 @@ public SELF withData(String dataContentType, URI dataSchema, byte[] data) {
7678
return this.self;
7779
}
7880

81+
public SELF withData(CloudEventData data) {
82+
this.data = data;
83+
return this.self;
84+
}
85+
86+
public SELF withData(String dataContentType, CloudEventData data) {
87+
withDataContentType(dataContentType);
88+
withData(data);
89+
return this.self;
90+
}
91+
92+
public SELF withData(String dataContentType, URI dataSchema, CloudEventData data) {
93+
withDataContentType(dataContentType);
94+
withDataSchema(dataSchema);
95+
withData(data);
96+
return this.self;
97+
}
98+
7999
public SELF withExtension(@Nonnull String key, String value) {
80100
this.extensions.put(key, value);
81101
return self;
@@ -114,7 +134,7 @@ public SELF withExtension(@Nonnull Extension extension) {
114134
}
115135

116136
@Override
117-
public CloudEvent end(byte[] value) throws CloudEventRWException {
137+
public CloudEvent end(CloudEventData value) throws CloudEventRWException {
118138
this.data = value;
119139
return build();
120140
}

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

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

1818
package io.cloudevents.core.message.impl;
1919

20+
import io.cloudevents.CloudEventData;
2021
import io.cloudevents.SpecVersion;
2122
import io.cloudevents.rw.*;
2223

@@ -34,9 +35,9 @@
3435
public abstract class BaseGenericBinaryMessageReaderImpl<HK, HV> extends BaseBinaryMessageReader {
3536

3637
private final SpecVersion version;
37-
private final byte[] body;
38+
private final CloudEventData body;
3839

39-
protected BaseGenericBinaryMessageReaderImpl(SpecVersion version, byte[] body) {
40+
protected BaseGenericBinaryMessageReaderImpl(SpecVersion version, CloudEventData body) {
4041
Objects.requireNonNull(version);
4142
this.version = version;
4243
this.body = body;
@@ -66,7 +67,7 @@ public <T extends CloudEventWriter<V>, V> V read(CloudEventWriterFactory<T, V> w
6667
});
6768

6869
// Set the payload
69-
if (this.body != null && this.body.length != 0) {
70+
if (this.body != null) {
7071
return visitor.end(this.body);
7172
}
7273

core/src/main/java/io/cloudevents/core/v03/CloudEventV03.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package io.cloudevents.core.v03;
1818

19+
import io.cloudevents.CloudEventData;
1920
import io.cloudevents.SpecVersion;
2021
import io.cloudevents.core.impl.BaseCloudEvent;
2122
import io.cloudevents.lang.Nullable;
@@ -24,7 +25,6 @@
2425

2526
import java.net.URI;
2627
import java.time.OffsetDateTime;
27-
import java.util.Arrays;
2828
import java.util.Map;
2929
import java.util.Objects;
3030

@@ -47,7 +47,7 @@ public final class CloudEventV03 extends BaseCloudEvent {
4747
public CloudEventV03(String id, URI source, String type,
4848
OffsetDateTime time, URI schemaurl,
4949
String datacontenttype, String subject,
50-
byte[] data, Map<String, Object> extensions) {
50+
CloudEventData data, Map<String, Object> extensions) {
5151
super(data, extensions);
5252

5353
this.id = id;
@@ -172,13 +172,13 @@ public boolean equals(Object o) {
172172
Objects.equals(schemaurl, that.schemaurl) &&
173173
Objects.equals(getSubject(), that.getSubject()) &&
174174
Objects.equals(getTime(), that.getTime()) &&
175-
Arrays.equals(getData(), that.getData()) &&
175+
Objects.equals(getData(), that.getData()) &&
176176
Objects.equals(this.extensions, that.extensions);
177177
}
178178

179179
@Override
180180
public int hashCode() {
181-
return Objects.hash(getId(), getSource(), getType(), datacontenttype, schemaurl, getSubject(), getTime(), Arrays.hashCode(getData()), this.extensions);
181+
return Objects.hash(getId(), getSource(), getType(), datacontenttype, schemaurl, getSubject(), getTime(), getData(), this.extensions);
182182
}
183183

184184
@Override
@@ -191,7 +191,7 @@ public String toString() {
191191
", schemaurl=" + schemaurl +
192192
", subject='" + subject + '\'' +
193193
", time=" + time +
194-
", data=" + Arrays.toString(getData()) +
194+
", data=" + getData() +
195195
", extensions" + this.extensions +
196196
'}';
197197
}

core/src/main/java/io/cloudevents/core/v1/CloudEventV1.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
*/
1717
package io.cloudevents.core.v1;
1818

19+
import io.cloudevents.CloudEventData;
1920
import io.cloudevents.SpecVersion;
2021
import io.cloudevents.core.impl.BaseCloudEvent;
2122
import io.cloudevents.rw.CloudEventAttributesWriter;
2223
import io.cloudevents.rw.CloudEventRWException;
2324

2425
import java.net.URI;
2526
import java.time.OffsetDateTime;
26-
import java.util.Arrays;
2727
import java.util.Map;
2828
import java.util.Objects;
2929

@@ -46,7 +46,7 @@ public final class CloudEventV1 extends BaseCloudEvent {
4646
public CloudEventV1(String id, URI source,
4747
String type, String datacontenttype,
4848
URI dataschema, String subject, OffsetDateTime time,
49-
byte[] data, Map<String, Object> extensions) {
49+
CloudEventData data, Map<String, Object> extensions) {
5050
super(data, extensions);
5151

5252
this.id = id;
@@ -167,13 +167,13 @@ public boolean equals(Object o) {
167167
Objects.equals(dataschema, that.getDataSchema()) &&
168168
Objects.equals(getSubject(), that.getSubject()) &&
169169
Objects.equals(getTime(), that.getTime()) &&
170-
Arrays.equals(getData(), that.getData()) &&
170+
Objects.equals(getData(), that.getData()) &&
171171
Objects.equals(this.extensions, that.extensions);
172172
}
173173

174174
@Override
175175
public int hashCode() {
176-
return Objects.hash(getId(), getSource(), getType(), datacontenttype, dataschema, getSubject(), getTime(), Arrays.hashCode(getData()), this.extensions);
176+
return Objects.hash(getId(), getSource(), getType(), datacontenttype, dataschema, getSubject(), getTime(), getData(), this.extensions);
177177
}
178178

179179
@Override
@@ -186,7 +186,7 @@ public String toString() {
186186
", dataschema=" + dataschema +
187187
", subject='" + subject + '\'' +
188188
", time=" + time +
189-
", data=" + Arrays.toString(getData()) +
189+
", data=" + getData() +
190190
", extensions=" + this.extensions +
191191
'}';
192192
}

0 commit comments

Comments
 (0)