-
Notifications
You must be signed in to change notification settings - Fork 166
Move PojoCloudEventData in core #289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
slinkydeveloper
merged 4 commits into
cloudevents:master
from
slinkydeveloper:public_pojo_cloudevent_data
Nov 23, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
core/src/main/java/io/cloudevents/core/data/PojoCloudEventData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package io.cloudevents.core.data; | ||
|
|
||
| import io.cloudevents.CloudEventData; | ||
| import io.cloudevents.rw.CloudEventRWException; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class PojoCloudEventData<T> implements CloudEventData { | ||
|
|
||
| /** | ||
| * Interface defining a conversion from T to byte array. This is similar to {@link java.util.function.Function} | ||
| * but it allows checked exceptions. | ||
| * | ||
| * @param <T> the source type of the conversion | ||
| */ | ||
| @FunctionalInterface | ||
| public interface ToBytes<T> { | ||
| byte[] convert(T data) throws Exception; | ||
| } | ||
|
|
||
| private final T value; | ||
| private byte[] memoizedValue; | ||
| private final ToBytes<T> mapper; | ||
|
|
||
| private PojoCloudEventData(T value, ToBytes<T> mapper) { | ||
| this(value, null, mapper); | ||
| } | ||
|
|
||
| private PojoCloudEventData(T value, byte[] memoizedValue, ToBytes<T> mapper) { | ||
| Objects.requireNonNull(value); | ||
| if (memoizedValue == null && mapper == null) { | ||
| throw new NullPointerException("You must provide the serialized data value or a mapper"); | ||
| } | ||
| this.value = value; | ||
| this.memoizedValue = memoizedValue; | ||
| this.mapper = mapper; | ||
| } | ||
|
|
||
| public T getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] toBytes() { | ||
| if (this.memoizedValue == null) { | ||
| try { | ||
| this.memoizedValue = mapper.convert(this.value); | ||
| } catch (Exception e) { | ||
| throw CloudEventRWException.newDataConversion(e, value.getClass().toString(), "byte[]"); | ||
| } | ||
| } | ||
| return this.memoizedValue; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| PojoCloudEventData<?> that = (PojoCloudEventData<?>) o; | ||
| return Objects.equals(getValue(), that.getValue()); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(getValue()); | ||
| } | ||
|
|
||
| /** | ||
| * Wrap the provided data in a {@link PojoCloudEventData} serializable by the provided mapper. | ||
| */ | ||
| public static <T> PojoCloudEventData<T> wrap(T data, ToBytes<T> mapper) { | ||
slinkydeveloper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return new PojoCloudEventData<>(data, mapper); | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
core/src/test/java/io/cloudevents/core/data/PojoCloudEventDataTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package io.cloudevents.core.data; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class PojoCloudEventDataTest { | ||
|
|
||
| @Test | ||
| void testWrapAndMemoization() { | ||
| PojoCloudEventData<Integer> data = PojoCloudEventData.wrap(10, i -> i.toString().getBytes()); | ||
|
|
||
| assertThat(data.getValue()) | ||
| .isEqualTo(10); | ||
|
|
||
| byte[] firstConversion = data.toBytes(); | ||
|
|
||
| assertThat(firstConversion) | ||
| .isEqualTo("10".getBytes()); | ||
|
|
||
| assertThat(data.toBytes()) | ||
| .isSameAs(firstConversion); | ||
| } | ||
|
|
||
| @Test | ||
| void testAlreadySerializedValue() { | ||
| byte[] serialized = "10".getBytes(); | ||
| PojoCloudEventData<Integer> data = PojoCloudEventData.wrap(10, v -> serialized); | ||
|
|
||
| assertThat(data.getValue()) | ||
| .isEqualTo(10); | ||
|
|
||
| assertThat(data.toBytes()) | ||
| .isSameAs(serialized); | ||
| } | ||
| } |
54 changes: 0 additions & 54 deletions
54
formats/json-jackson/src/main/java/io/cloudevents/jackson/PojoCloudEventData.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.