Skip to content

Commit 96be516

Browse files
Moved CloudEventUtils from impl to io.cloudevents.core
Renamed CloudEventUtils#toVisitable to CloudEventUtils#toReader Added CloudEventUtils#toEvent Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
1 parent 42a7326 commit 96be516

File tree

11 files changed

+45
-17
lines changed

11 files changed

+45
-17
lines changed

core/src/main/java/io/cloudevents/core/impl/CloudEventUtils.java renamed to core/src/main/java/io/cloudevents/core/CloudEventUtils.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,36 @@
1515
*
1616
*/
1717

18-
package io.cloudevents.core.impl;
18+
package io.cloudevents.core;
1919

2020
import io.cloudevents.CloudEvent;
2121
import io.cloudevents.CloudEventData;
22+
import io.cloudevents.core.builder.CloudEventBuilder;
23+
import io.cloudevents.core.impl.CloudEventReaderAdapter;
2224
import io.cloudevents.lang.Nullable;
2325
import io.cloudevents.rw.CloudEventContextReader;
2426
import io.cloudevents.rw.CloudEventDataMapper;
27+
import io.cloudevents.rw.CloudEventRWException;
2528
import io.cloudevents.rw.CloudEventReader;
2629

30+
/**
31+
* This class contains a set of utility methods to deal with conversions of io.cloudevents related interfaces
32+
*/
2733
public final class CloudEventUtils {
2834

2935
private CloudEventUtils() {}
3036

3137
/**
3238
* Convert a {@link CloudEvent} to a {@link CloudEventReader}. This method provides a default implementation
39+
* for CloudEvent that doesn't implement {@link CloudEventReader}
3340
* for CloudEvent that doesn't implement CloudEventVisitable.
3441
* <p>
3542
* It's safe to use the returned {@link CloudEventReader} multiple times.
3643
*
3744
* @param event the event to convert
38-
* @return the visitable implementation
45+
* @return the reader implementation
3946
*/
40-
public static CloudEventReader toVisitable(CloudEvent event) {
47+
public static CloudEventReader toReader(CloudEvent event) {
4148
if (event instanceof CloudEventReader) {
4249
return (CloudEventReader) event;
4350
} else {
@@ -62,6 +69,27 @@ public static CloudEventContextReader toContextReader(CloudEvent event) {
6269
}
6370
}
6471

72+
/**
73+
* Convert a {@link CloudEventReader} to a {@link CloudEvent}.
74+
*
75+
* @param reader the reader where to read the message from
76+
* @return the reader implementation
77+
*/
78+
public static CloudEvent toEvent(CloudEventReader reader) throws CloudEventRWException {
79+
return toEvent(reader, null);
80+
}
81+
82+
/**
83+
* Convert a {@link CloudEventReader} to a {@link CloudEvent} mapping the data with the provided {@code mapper}.
84+
*
85+
* @param reader the reader where to read the message from
86+
* @param mapper the mapper to use when reading the data
87+
* @return the reader implementation
88+
*/
89+
public static CloudEvent toEvent(CloudEventReader reader, @Nullable CloudEventDataMapper<?> mapper) throws CloudEventRWException {
90+
return reader.read(CloudEventBuilder::fromSpecVersion, mapper);
91+
}
92+
6593
/**
6694
* Get the data contained in {@code event} and map it using the provided mapper.
6795
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class CloudEventReaderAdapter implements CloudEventReader, CloudEventCont
2424

2525
private final CloudEvent event;
2626

27-
CloudEventReaderAdapter(CloudEvent event) {
27+
public CloudEventReaderAdapter(CloudEvent event) {
2828
this.event = event;
2929
}
3030

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import io.cloudevents.CloudEvent;
2121
import io.cloudevents.CloudEventData;
22-
import io.cloudevents.core.builder.CloudEventBuilder;
22+
import io.cloudevents.core.CloudEventUtils;
2323
import io.cloudevents.lang.Nullable;
2424
import io.cloudevents.rw.*;
2525

@@ -102,7 +102,7 @@ default CloudEvent toEvent() throws CloudEventRWException, IllegalStateException
102102
default CloudEvent toEvent(@Nullable CloudEventDataMapper<? extends CloudEventData> mapper) throws CloudEventRWException, IllegalStateException {
103103
switch (getEncoding()) {
104104
case BINARY:
105-
return this.read(CloudEventBuilder::fromSpecVersion, mapper);
105+
return CloudEventUtils.toEvent(this, mapper);
106106
case STRUCTURED:
107107
return this.read((format, value) -> format.deserialize(value, mapper));
108108
default:

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

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

2020
import io.cloudevents.CloudEvent;
21+
import io.cloudevents.core.CloudEventUtils;
2122
import io.cloudevents.core.format.EventFormat;
22-
import io.cloudevents.core.impl.CloudEventUtils;
2323
import io.cloudevents.core.message.impl.GenericStructuredMessageReader;
2424
import io.cloudevents.rw.CloudEventWriter;
2525
import io.cloudevents.rw.CloudEventWriterFactory;
@@ -68,7 +68,7 @@ default R writeStructured(CloudEvent event, EventFormat format) {
6868
* @return return value at the end of the write process.
6969
*/
7070
default R writeBinary(CloudEvent event) {
71-
return CloudEventUtils.toVisitable(event).read(this);
71+
return CloudEventUtils.toReader(event).read(this);
7272
}
7373

7474
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package io.cloudevents.core.v03;
1818

1919
import io.cloudevents.SpecVersion;
20+
import io.cloudevents.core.CloudEventUtils;
2021
import io.cloudevents.core.impl.BaseCloudEventBuilder;
21-
import io.cloudevents.core.impl.CloudEventUtils;
2222
import io.cloudevents.rw.CloudEventRWException;
2323
import io.cloudevents.types.Time;
2424

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
import io.cloudevents.CloudEvent;
2121
import io.cloudevents.SpecVersion;
22+
import io.cloudevents.core.CloudEventUtils;
2223
import io.cloudevents.core.impl.BaseCloudEventBuilder;
23-
import io.cloudevents.core.impl.CloudEventUtils;
2424
import io.cloudevents.rw.CloudEventRWException;
2525
import io.cloudevents.types.Time;
2626

core/src/test/java/io/cloudevents/core/impl/CloudEventUtilsTest.java renamed to core/src/test/java/io/cloudevents/core/CloudEventUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.cloudevents.core.impl;
1+
package io.cloudevents.core;
22

33
import io.cloudevents.CloudEvent;
44
import io.cloudevents.CloudEventData;

core/src/test/java/io/cloudevents/core/mock/MockBinaryMessageWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import io.cloudevents.CloudEvent;
2121
import io.cloudevents.CloudEventData;
2222
import io.cloudevents.SpecVersion;
23+
import io.cloudevents.core.CloudEventUtils;
2324
import io.cloudevents.core.data.BytesCloudEventData;
24-
import io.cloudevents.core.impl.CloudEventUtils;
2525
import io.cloudevents.core.message.MessageReader;
2626
import io.cloudevents.core.message.impl.BaseBinaryMessageReader;
2727
import io.cloudevents.rw.*;
@@ -57,7 +57,7 @@ public MockBinaryMessageWriter() {
5757
public MockBinaryMessageWriter(CloudEvent event) {
5858
this();
5959
CloudEventUtils
60-
.toVisitable(event)
60+
.toReader(event)
6161
.read(this);
6262
}
6363

formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
2323
import io.cloudevents.CloudEvent;
2424
import io.cloudevents.CloudEventData;
25-
import io.cloudevents.core.impl.CloudEventUtils;
25+
import io.cloudevents.core.CloudEventUtils;
2626
import io.cloudevents.rw.CloudEventAttributesWriter;
2727
import io.cloudevents.rw.CloudEventContextReader;
2828
import io.cloudevents.rw.CloudEventExtensionsWriter;

formats/json-jackson/src/test/java/io/cloudevents/jackson/PojoCloudEventDataMapperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import com.fasterxml.jackson.databind.ObjectMapper;
66
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
77
import io.cloudevents.CloudEvent;
8+
import io.cloudevents.core.CloudEventUtils;
89
import io.cloudevents.core.builder.CloudEventBuilder;
9-
import io.cloudevents.core.impl.CloudEventUtils;
1010
import io.cloudevents.core.test.Data;
1111
import org.junit.jupiter.api.Test;
1212

0 commit comments

Comments
 (0)