From 2982f07f55d39e65488a55bf2d4aa282915a23ce Mon Sep 17 00:00:00 2001 From: Francesco Guardiani Date: Mon, 28 Sep 2020 08:58:57 +0200 Subject: [PATCH] Renamed methods of writes to coerce with the builder apis (#237) Signed-off-by: Francesco Guardiani --- .../rw/CloudEventAttributesWriter.java | 11 +++---- .../rw/CloudEventExtensionsWriter.java | 12 ++++---- .../core/builder/CloudEventBuilder.java | 5 +++- .../cloudevents/core/impl/BaseCloudEvent.java | 6 ++-- .../core/impl/BaseCloudEventBuilder.java | 17 +---------- .../core/impl/CloudEventReaderAdapter.java | 20 ++++++------- .../BaseGenericBinaryMessageReaderImpl.java | 12 ++++---- .../core/v03/CloudEventBuilder.java | 28 ++++++++--------- .../cloudevents/core/v03/CloudEventV03.java | 14 ++++----- .../core/v03/V1ToV03AttributesConverter.java | 26 ++++++++-------- .../core/v1/CloudEventBuilder.java | 26 ++++++++-------- .../io/cloudevents/core/v1/CloudEventV1.java | 14 ++++----- .../core/v1/V03ToV1AttributesConverter.java | 26 ++++++++-------- .../core/mock/MockBinaryMessageWriter.java | 30 +++++++++++-------- .../jackson/CloudEventDeserializer.java | 14 ++++----- .../jackson/CloudEventSerializer.java | 12 +++++--- .../http/impl/HttpMessageWriter.java | 6 ++-- .../ws/impl/RestfulWSClientMessageWriter.java | 6 ++-- .../ws/impl/RestfulWSMessageWriter.java | 6 ++-- ...txHttpServerResponseMessageWriterImpl.java | 6 ++-- ...ertxWebClientRequestMessageWriterImpl.java | 7 +++-- .../impl/BaseKafkaMessageWriterImpl.java | 6 ++-- .../impl/KafkaProducerMessageWriterImpl.java | 2 +- .../KafkaSerializerMessageWriterImpl.java | 2 +- 24 files changed, 163 insertions(+), 151 deletions(-) diff --git a/api/src/main/java/io/cloudevents/rw/CloudEventAttributesWriter.java b/api/src/main/java/io/cloudevents/rw/CloudEventAttributesWriter.java index f084d4fa..04b1ae8b 100644 --- a/api/src/main/java/io/cloudevents/rw/CloudEventAttributesWriter.java +++ b/api/src/main/java/io/cloudevents/rw/CloudEventAttributesWriter.java @@ -17,6 +17,7 @@ package io.cloudevents.rw; +import io.cloudevents.lang.Nullable; import io.cloudevents.types.Time; import java.net.URI; @@ -35,7 +36,7 @@ public interface CloudEventAttributesWriter { * @param value * @throws CloudEventRWException */ - void setAttribute(String name, String value) throws CloudEventRWException; + CloudEventAttributesWriter withAttribute(String name, @Nullable String value) throws CloudEventRWException; /** * Set attribute with type {@link URI}. @@ -44,8 +45,8 @@ public interface CloudEventAttributesWriter { * @param value * @throws CloudEventRWException */ - default void setAttribute(String name, URI value) throws CloudEventRWException { - setAttribute(name, value.toString()); + default CloudEventAttributesWriter withAttribute(String name, @Nullable URI value) throws CloudEventRWException { + return withAttribute(name, value == null ? null : value.toString()); } /** @@ -55,8 +56,8 @@ public interface CloudEventAttributesWriter { * @param value * @throws CloudEventRWException */ - default void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException { - setAttribute(name, Time.writeTime(value)); + default CloudEventAttributesWriter withAttribute(String name, @Nullable OffsetDateTime value) throws CloudEventRWException { + return withAttribute(name, value == null ? null : Time.writeTime(value)); } } diff --git a/api/src/main/java/io/cloudevents/rw/CloudEventExtensionsWriter.java b/api/src/main/java/io/cloudevents/rw/CloudEventExtensionsWriter.java index cc553403..6ca7d83b 100644 --- a/api/src/main/java/io/cloudevents/rw/CloudEventExtensionsWriter.java +++ b/api/src/main/java/io/cloudevents/rw/CloudEventExtensionsWriter.java @@ -17,6 +17,8 @@ package io.cloudevents.rw; +import io.cloudevents.lang.Nullable; + import java.net.URI; /** @@ -31,7 +33,7 @@ public interface CloudEventExtensionsWriter { * @param value * @throws CloudEventRWException */ - void setExtension(String name, String value) throws CloudEventRWException; + CloudEventExtensionsWriter withExtension(String name, @Nullable String value) throws CloudEventRWException; /** * Set attribute with type {@link URI}. @@ -40,8 +42,8 @@ public interface CloudEventExtensionsWriter { * @param value * @throws CloudEventRWException */ - default void setExtension(String name, Number value) throws CloudEventRWException { - setExtension(name, value.toString()); + default CloudEventExtensionsWriter withExtension(String name, @Nullable Number value) throws CloudEventRWException { + return withExtension(name, value == null ? null : value.toString()); } /** @@ -51,8 +53,8 @@ public interface CloudEventExtensionsWriter { * @param value * @throws CloudEventRWException */ - default void setExtension(String name, Boolean value) throws CloudEventRWException { - setExtension(name, value.toString()); + default CloudEventExtensionsWriter withExtension(String name, @Nullable Boolean value) throws CloudEventRWException { + return withExtension(name, value == null ? null : value.toString()); } } diff --git a/core/src/main/java/io/cloudevents/core/builder/CloudEventBuilder.java b/core/src/main/java/io/cloudevents/core/builder/CloudEventBuilder.java index a65e661c..390eb3c2 100644 --- a/core/src/main/java/io/cloudevents/core/builder/CloudEventBuilder.java +++ b/core/src/main/java/io/cloudevents/core/builder/CloudEventBuilder.java @@ -123,6 +123,7 @@ public interface CloudEventBuilder extends CloudEventWriter { * @param value value of the extension attribute * @return self */ + @Override CloudEventBuilder withExtension(@Nonnull String key, String value); /** @@ -132,6 +133,7 @@ public interface CloudEventBuilder extends CloudEventWriter { * @param value value of the extension attribute * @return self */ + @Override CloudEventBuilder withExtension(@Nonnull String key, Number value); /** @@ -141,7 +143,8 @@ public interface CloudEventBuilder extends CloudEventWriter { * @param value value of the extension attribute * @return self */ - CloudEventBuilder withExtension(@Nonnull String key, boolean value); + @Override + CloudEventBuilder withExtension(@Nonnull String key, Boolean value); /** * Add to the builder all the extension key/values of the provided extension diff --git a/core/src/main/java/io/cloudevents/core/impl/BaseCloudEvent.java b/core/src/main/java/io/cloudevents/core/impl/BaseCloudEvent.java index 6e48bf77..b99a019f 100644 --- a/core/src/main/java/io/cloudevents/core/impl/BaseCloudEvent.java +++ b/core/src/main/java/io/cloudevents/core/impl/BaseCloudEvent.java @@ -65,11 +65,11 @@ public abstract class BaseCloudEvent implements CloudEvent, CloudEventReader { // TODO to be improved for (Map.Entry entry : this.extensions.entrySet()) { if (entry.getValue() instanceof String) { - visitor.setExtension(entry.getKey(), (String) entry.getValue()); + visitor.withExtension(entry.getKey(), (String) entry.getValue()); } else if (entry.getValue() instanceof Number) { - visitor.setExtension(entry.getKey(), (Number) entry.getValue()); + visitor.withExtension(entry.getKey(), (Number) entry.getValue()); } else if (entry.getValue() instanceof Boolean) { - visitor.setExtension(entry.getKey(), (Boolean) entry.getValue()); + visitor.withExtension(entry.getKey(), (Boolean) entry.getValue()); } else { // This should never happen because we build that map only through our builders throw new IllegalStateException("Illegal value inside extensions map: " + entry); diff --git a/core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java b/core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java index 19bf6eed..b0b2e32e 100644 --- a/core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java +++ b/core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java @@ -86,7 +86,7 @@ public abstract class BaseCloudEventBuilder extends BaseBin // in order to complete the visit in one loop this.forEachHeader((key, value) -> { if (isContentTypeHeader(key)) { - visitor.setAttribute("datacontenttype", toCloudEventsValue(value)); + visitor.withAttribute("datacontenttype", toCloudEventsValue(value)); } else if (isCloudEventsHeader(key)) { String name = toCloudEventsKey(key); if (name.equals("specversion")) { return; } if (this.version.getAllAttributes().contains(name)) { - visitor.setAttribute(name, toCloudEventsValue(value)); + visitor.withAttribute(name, toCloudEventsValue(value)); } else { - visitor.setExtension(name, toCloudEventsValue(value)); + visitor.withExtension(name, toCloudEventsValue(value)); } } }); @@ -77,14 +77,14 @@ public abstract class BaseGenericBinaryMessageReaderImpl extends BaseBin public void readAttributes(CloudEventAttributesWriter writer) throws RuntimeException { this.forEachHeader((key, value) -> { if (isContentTypeHeader(key)) { - writer.setAttribute("datacontenttype", toCloudEventsValue(value)); + writer.withAttribute("datacontenttype", toCloudEventsValue(value)); } else if (isCloudEventsHeader(key)) { String name = toCloudEventsKey(key); if (name.equals("specversion")) { return; } if (this.version.getAllAttributes().contains(name)) { - writer.setAttribute(name, toCloudEventsValue(value)); + writer.withAttribute(name, toCloudEventsValue(value)); } } }); @@ -97,7 +97,7 @@ public abstract class BaseGenericBinaryMessageReaderImpl extends BaseBin if (isCloudEventsHeader(key)) { String name = toCloudEventsKey(key); if (!this.version.getAllAttributes().contains(name)) { - visitor.setExtension(name, toCloudEventsValue(value)); + visitor.withExtension(name, toCloudEventsValue(value)); } } }); diff --git a/core/src/main/java/io/cloudevents/core/v03/CloudEventBuilder.java b/core/src/main/java/io/cloudevents/core/v03/CloudEventBuilder.java index 41b32545..3ba1ec33 100644 --- a/core/src/main/java/io/cloudevents/core/v03/CloudEventBuilder.java +++ b/core/src/main/java/io/cloudevents/core/v03/CloudEventBuilder.java @@ -135,66 +135,66 @@ public final class CloudEventBuilder extends BaseCloudEventBuilder e : this.attributes.entrySet()) { if (e.getValue() instanceof String) { - writer.setAttribute(e.getKey(), (String) e.getValue()); + writer.withAttribute(e.getKey(), (String) e.getValue()); } else if (e.getValue() instanceof OffsetDateTime) { - writer.setAttribute(e.getKey(), (OffsetDateTime) e.getValue()); + writer.withAttribute(e.getKey(), (OffsetDateTime) e.getValue()); } else if (e.getValue() instanceof URI) { - writer.setAttribute(e.getKey(), (URI) e.getValue()); + writer.withAttribute(e.getKey(), (URI) e.getValue()); } else { // This should never happen because we build that map only through our builders throw new IllegalStateException("Illegal value inside attributes map: " + e); @@ -92,11 +92,11 @@ public class MockBinaryMessageWriter extends BaseBinaryMessageReader implements public void readExtensions(CloudEventExtensionsWriter visitor) throws CloudEventRWException, IllegalStateException { for (Map.Entry entry : this.extensions.entrySet()) { if (entry.getValue() instanceof String) { - visitor.setExtension(entry.getKey(), (String) entry.getValue()); + visitor.withExtension(entry.getKey(), (String) entry.getValue()); } else if (entry.getValue() instanceof Number) { - visitor.setExtension(entry.getKey(), (Number) entry.getValue()); + visitor.withExtension(entry.getKey(), (Number) entry.getValue()); } else if (entry.getValue() instanceof Boolean) { - visitor.setExtension(entry.getKey(), (Boolean) entry.getValue()); + visitor.withExtension(entry.getKey(), (Boolean) entry.getValue()); } else { // This should never happen because we build that map only through our builders throw new IllegalStateException("Illegal value inside extensions map: " + entry); @@ -116,33 +116,39 @@ public class MockBinaryMessageWriter extends BaseBinaryMessageReader implements } @Override - public void setAttribute(String name, String value) throws CloudEventRWException { + public MockBinaryMessageWriter withAttribute(String name, String value) throws CloudEventRWException { this.attributes.put(name, value); + return this; } @Override - public void setAttribute(String name, URI value) throws CloudEventRWException { + public MockBinaryMessageWriter withAttribute(String name, URI value) throws CloudEventRWException { this.attributes.put(name, value); + return this; } @Override - public void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException { + public MockBinaryMessageWriter withAttribute(String name, OffsetDateTime value) throws CloudEventRWException { this.attributes.put(name, value); + return this; } @Override - public void setExtension(String name, String value) throws CloudEventRWException { + public MockBinaryMessageWriter withExtension(String name, String value) throws CloudEventRWException { this.extensions.put(name, value); + return this; } @Override - public void setExtension(String name, Number value) throws CloudEventRWException { + public MockBinaryMessageWriter withExtension(String name, Number value) throws CloudEventRWException { this.extensions.put(name, value); + return this; } @Override - public void setExtension(String name, Boolean value) throws CloudEventRWException { + public MockBinaryMessageWriter withExtension(String name, Boolean value) throws CloudEventRWException { this.extensions.put(name, value); + return this; } @Override diff --git a/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java b/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java index 0cbf0704..625a0a00 100644 --- a/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java +++ b/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java @@ -60,14 +60,14 @@ public class CloudEventDeserializer extends StdDeserializer { // Read mandatory attributes for (String attr : specVersion.getMandatoryAttributes()) { if (!"specversion".equals(attr)) { - visitor.setAttribute(attr, getStringNode(this.node, this.p, attr)); + visitor.withAttribute(attr, getStringNode(this.node, this.p, attr)); } } // Parse datacontenttype if any String contentType = getOptionalStringNode(this.node, this.p, "datacontenttype"); if (contentType != null) { - visitor.setAttribute("datacontenttype", contentType); + visitor.withAttribute("datacontenttype", contentType); } // Read optional attributes @@ -75,7 +75,7 @@ public class CloudEventDeserializer extends StdDeserializer { if (!"datacontentencoding".equals(attr)) { // Skip datacontentencoding, we need it later String val = getOptionalStringNode(this.node, this.p, attr); if (val != null) { - visitor.setAttribute(attr, val); + visitor.withAttribute(attr, val); } } } @@ -127,16 +127,16 @@ public class CloudEventDeserializer extends StdDeserializer { switch (extensionValue.getNodeType()) { case BOOLEAN: - visitor.setExtension(extensionName, extensionValue.booleanValue()); + visitor.withExtension(extensionName, extensionValue.booleanValue()); break; case NUMBER: - visitor.setExtension(extensionName, extensionValue.numberValue()); + visitor.withExtension(extensionName, extensionValue.numberValue()); break; case STRING: - visitor.setExtension(extensionName, extensionValue.textValue()); + visitor.withExtension(extensionName, extensionValue.textValue()); break; default: - visitor.setExtension(extensionName, extensionValue.toString()); + visitor.withExtension(extensionName, extensionValue.toString()); } }); diff --git a/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventSerializer.java b/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventSerializer.java index ff289110..d86fdd2a 100644 --- a/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventSerializer.java +++ b/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventSerializer.java @@ -55,37 +55,41 @@ public class CloudEventSerializer extends StdSerializer { } @Override - public void setAttribute(String name, String value) throws CloudEventRWException { + public FieldsSerializer withAttribute(String name, String value) throws CloudEventRWException { try { gen.writeStringField(name, value); + return this; } catch (IOException e) { throw new RuntimeException(e); } } @Override - public void setExtension(String name, String value) throws CloudEventRWException { + public FieldsSerializer withExtension(String name, String value) throws CloudEventRWException { try { gen.writeStringField(name, value); + return this; } catch (IOException e) { throw new RuntimeException(e); } } @Override - public void setExtension(String name, Number value) throws CloudEventRWException { + public FieldsSerializer withExtension(String name, Number value) throws CloudEventRWException { try { gen.writeFieldName(name); provider.findValueSerializer(value.getClass()).serialize(value, gen, provider); + return this; } catch (IOException e) { throw new RuntimeException(e); } } @Override - public void setExtension(String name, Boolean value) throws CloudEventRWException { + public FieldsSerializer withExtension(String name, Boolean value) throws CloudEventRWException { try { gen.writeBooleanField(name, value); + return this; } catch (IOException e) { throw new RuntimeException(e); } diff --git a/http/basic/src/main/java/io/cloudevents/http/impl/HttpMessageWriter.java b/http/basic/src/main/java/io/cloudevents/http/impl/HttpMessageWriter.java index 84d7eeaf..3e16b3c4 100644 --- a/http/basic/src/main/java/io/cloudevents/http/impl/HttpMessageWriter.java +++ b/http/basic/src/main/java/io/cloudevents/http/impl/HttpMessageWriter.java @@ -57,13 +57,15 @@ public class HttpMessageWriter implements CloudEventWriter, MessageWriter< } @Override - public void setAttribute(String name, String value) throws CloudEventRWException { + public HttpMessageWriter withAttribute(String name, String value) throws CloudEventRWException { putHeader.accept(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value); + return this; } @Override - public void setExtension(String name, String value) throws CloudEventRWException { + public HttpMessageWriter withExtension(String name, String value) throws CloudEventRWException { putHeader.accept("ce-" + name, value); + return this; } @Override diff --git a/http/restful-ws/src/main/java/io/cloudevents/http/restful/ws/impl/RestfulWSClientMessageWriter.java b/http/restful-ws/src/main/java/io/cloudevents/http/restful/ws/impl/RestfulWSClientMessageWriter.java index ce3d2cee..b6d89333 100644 --- a/http/restful-ws/src/main/java/io/cloudevents/http/restful/ws/impl/RestfulWSClientMessageWriter.java +++ b/http/restful-ws/src/main/java/io/cloudevents/http/restful/ws/impl/RestfulWSClientMessageWriter.java @@ -46,13 +46,15 @@ public final class RestfulWSClientMessageWriter implements CloudEventWriter, Mes } @Override - public void setAttribute(String name, String value) throws CloudEventRWException { + public RestfulWSMessageWriter withAttribute(String name, String value) throws CloudEventRWException { this.httpHeaders.add(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value); + return this; } @Override - public void setExtension(String name, String value) throws CloudEventRWException { + public RestfulWSMessageWriter withExtension(String name, String value) throws CloudEventRWException { this.httpHeaders.add(CloudEventsHeaders.CE_PREFIX + name, value); + return this; } @Override diff --git a/http/vertx/src/main/java/io/cloudevents/http/vertx/impl/VertxHttpServerResponseMessageWriterImpl.java b/http/vertx/src/main/java/io/cloudevents/http/vertx/impl/VertxHttpServerResponseMessageWriterImpl.java index b9164c79..9eb2d5a1 100644 --- a/http/vertx/src/main/java/io/cloudevents/http/vertx/impl/VertxHttpServerResponseMessageWriterImpl.java +++ b/http/vertx/src/main/java/io/cloudevents/http/vertx/impl/VertxHttpServerResponseMessageWriterImpl.java @@ -45,13 +45,15 @@ public class VertxHttpServerResponseMessageWriterImpl implements MessageWriter implements MessageWriter withAttribute(String name, String value) throws CloudEventRWException { headers.add(new RecordHeader(KafkaHeaders.ATTRIBUTES_TO_HEADERS.get(name), value.getBytes())); + return this; } @Override - public void setExtension(String name, String value) throws CloudEventRWException { + public BaseKafkaMessageWriterImpl withExtension(String name, String value) throws CloudEventRWException { headers.add(new RecordHeader(KafkaHeaders.CE_PREFIX + name, value.getBytes())); + return this; } @Override diff --git a/kafka/src/main/java/io/cloudevents/kafka/impl/KafkaProducerMessageWriterImpl.java b/kafka/src/main/java/io/cloudevents/kafka/impl/KafkaProducerMessageWriterImpl.java index a8df4b91..15ccaff4 100644 --- a/kafka/src/main/java/io/cloudevents/kafka/impl/KafkaProducerMessageWriterImpl.java +++ b/kafka/src/main/java/io/cloudevents/kafka/impl/KafkaProducerMessageWriterImpl.java @@ -44,7 +44,7 @@ public final class KafkaProducerMessageWriterImpl @Override public KafkaProducerMessageWriterImpl create(SpecVersion version) { - this.setAttribute("specversion", version.toString()); + this.withAttribute("specversion", version.toString()); return this; } } diff --git a/kafka/src/main/java/io/cloudevents/kafka/impl/KafkaSerializerMessageWriterImpl.java b/kafka/src/main/java/io/cloudevents/kafka/impl/KafkaSerializerMessageWriterImpl.java index d5da8a67..892842fd 100644 --- a/kafka/src/main/java/io/cloudevents/kafka/impl/KafkaSerializerMessageWriterImpl.java +++ b/kafka/src/main/java/io/cloudevents/kafka/impl/KafkaSerializerMessageWriterImpl.java @@ -28,7 +28,7 @@ public final class KafkaSerializerMessageWriterImpl extends BaseKafkaMessageWrit @Override public KafkaSerializerMessageWriterImpl create(SpecVersion version) { - this.setAttribute("specversion", version.toString()); + this.withAttribute("specversion", version.toString()); return this; }