Skip to content

Commit

Permalink
Simplify with better compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
David Turanski committed Dec 4, 2019
1 parent 5cbdc24 commit a4fb601
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 180 deletions.
7 changes: 7 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,20 @@
<version>3.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<properties>
<jackson.version>2.10.1</jackson.version>
<hibernate-validator.version>6.0.17.Final</hibernate-validator.version>
<jakarta.el.version>3.0.3</jakarta.el.version>
<gson.version>2.8.6</gson.version>
</properties>

</project>
42 changes: 0 additions & 42 deletions api/src/main/java/io/cloudevents/format/DateTimeFormat.java

This file was deleted.

50 changes: 0 additions & 50 deletions api/src/main/java/io/cloudevents/format/DateTimeValidator.java

This file was deleted.

15 changes: 7 additions & 8 deletions api/src/main/java/io/cloudevents/v02/AttributesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME;

import io.cloudevents.format.DateTimeFormat;
import java.net.URI;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -59,13 +58,13 @@ public class AttributesImpl implements Attributes {
@NotBlank
private final String id;

@DateTimeFormat
private final String time;
private final ZonedDateTime time;
private final URI schemaurl;
private final String contenttype;


AttributesImpl(String type, String specversion, URI source,
String id, String time, URI schemaurl, String contenttype) {
String id, ZonedDateTime time, URI schemaurl, String contenttype) {
this.type = type;
this.specversion = specversion;
this.source = source;
Expand Down Expand Up @@ -115,7 +114,7 @@ public URI getSource() {
* Timestamp of when the event happened.
*/
public Optional<ZonedDateTime> getTime() {
return parseZonedDateTime(time);
return Optional.ofNullable(time);
}

/**
Expand Down Expand Up @@ -149,7 +148,7 @@ public static AttributesImpl build(
@JsonProperty("schemaurl") URI schemaurl,
@JsonProperty("contenttype") String contenttype) {

return new AttributesImpl(type, specversion, source, id, time,
return new AttributesImpl(type, specversion, source, id, parseZonedDateTime(time).orElse(null),
schemaurl, contenttype);
}

Expand Down Expand Up @@ -206,11 +205,11 @@ public static Map<String, String> marshal(AttributesImpl attributes) {
return result;
}

protected static Optional<String> formatZonedDateTime(ZonedDateTime zonedDateTime) {
static Optional<String> formatZonedDateTime(ZonedDateTime zonedDateTime) {
return zonedDateTime == null? Optional.empty() : Optional.of(zonedDateTime.format(ISO_ZONED_DATE_TIME));
}

protected static Optional<ZonedDateTime> parseZonedDateTime(String zonedDateTime) {
static Optional<ZonedDateTime> parseZonedDateTime(String zonedDateTime) {
return zonedDateTime == null ? Optional.empty(): Optional.of(ZonedDateTime.parse(zonedDateTime));
}
}
7 changes: 1 addition & 6 deletions api/src/main/java/io/cloudevents/v02/CloudEventBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private CloudEventBuilder() {
private String id;
private URI source;

private String time;
private ZonedDateTime time;
private URI schemaurl;
private String contenttype;
private T data;
Expand Down Expand Up @@ -196,11 +196,6 @@ public CloudEventBuilder<T> withSource(URI source) {
}

public CloudEventBuilder<T> withTime(ZonedDateTime time) {
this.time = AttributesImpl.formatZonedDateTime(time).get();
return this;
}

public CloudEventBuilder<T> withTime(String time) {
this.time = time;
return this;
}
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/io/cloudevents/v02/CloudEventImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package io.cloudevents.v02;

import java.net.URI;
import java.time.ZonedDateTime;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -128,7 +128,7 @@ public static <T> CloudEventImpl<T> build(
.withId(id)
.withSource(source)
.withType(type)
.withTime(time)
.withTime(AttributesImpl.parseZonedDateTime(time).orElse(null))
.withSchemaurl(schemaurl)
.withContenttype(contenttype)
.withData(data)
Expand Down
37 changes: 13 additions & 24 deletions api/src/main/java/io/cloudevents/v03/AttributesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,31 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.cloudevents.Attributes;
import io.cloudevents.format.DateTimeFormat;

import java.net.URI;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import io.cloudevents.Attributes;

import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME;

/**
* The event attributes implementation for v0.3
*
* @author fabiojose
* @author dturanski
*
*/
@JsonInclude(value = Include.NON_ABSENT)
Expand All @@ -57,8 +62,7 @@ public class AttributesImpl implements Attributes {
@NotBlank
private final String type;

@DateTimeFormat
private final String time;
private final ZonedDateTime time;
private final URI schemaurl;

@Pattern(regexp = "base64")
Expand All @@ -67,24 +71,9 @@ public class AttributesImpl implements Attributes {

@Size(min = 1)
private final String subject;

AttributesImpl(String id, URI source, String specversion, String type,
ZonedDateTime time, URI schemaurl, String datacontentencoding,
String datacontenttype, String subject) {
this.id = id;
this.source = source;
this.specversion = specversion;
this.type = type;

this.time = formatZonedDateTime(time).get();
this.schemaurl = schemaurl;
this.datacontentencoding = datacontentencoding;
this.datacontenttype = datacontenttype;
this.subject = subject;
}

AttributesImpl(String id, URI source, String specversion, String type,
String time, URI schemaurl, String datacontentencoding,
ZonedDateTime time, URI schemaurl, String datacontentencoding,
String datacontenttype, String subject) {
this.id = id;
this.source = source;
Expand All @@ -111,7 +100,7 @@ public String getType() {
return type;
}
public Optional<ZonedDateTime> getTime() {
return parseZonedDateTime(time);
return Optional.ofNullable(time);
}
public Optional<URI> getSchemaurl() {
return Optional.ofNullable(schemaurl);
Expand Down Expand Up @@ -157,7 +146,7 @@ public static AttributesImpl build(
@JsonProperty("datacontenttype") String datacontenttype,
@JsonProperty("subject") String subject) {

return new AttributesImpl(id, source, specversion, type, time,
return new AttributesImpl(id, source, specversion, type, parseZonedDateTime(time).orElse(null),
schemaurl, datacontentencoding, datacontenttype, subject);
}

Expand Down Expand Up @@ -221,11 +210,11 @@ public static AttributesImpl unmarshal(Map<String, String> attributes) {
datacontenttype, subject);
}

protected static Optional<String> formatZonedDateTime(ZonedDateTime zonedDateTime) {
static Optional<String> formatZonedDateTime(ZonedDateTime zonedDateTime) {
return zonedDateTime == null? Optional.empty() : Optional.of(zonedDateTime.format(ISO_ZONED_DATE_TIME));
}

protected static Optional<ZonedDateTime> parseZonedDateTime(String zonedDateTime) {
static Optional<ZonedDateTime> parseZonedDateTime(String zonedDateTime) {
return zonedDateTime == null ? Optional.empty(): Optional.of(ZonedDateTime.parse(zonedDateTime));
}
}
7 changes: 1 addition & 6 deletions api/src/main/java/io/cloudevents/v03/CloudEventBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private CloudEventBuilder() {}

private String type;

private String time;
private ZonedDateTime time;
private URI schemaurl;
private String datacontentencoding;
private String datacontenttype;
Expand Down Expand Up @@ -213,11 +213,6 @@ public CloudEventBuilder<T> withType(String type) {
}

public CloudEventBuilder<T> withTime(ZonedDateTime time) {
this.time = AttributesImpl.formatZonedDateTime(time).get();
return this;
}

public CloudEventBuilder<T> withTime(String time) {
this.time = time;
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/io/cloudevents/v03/CloudEventImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static <T> CloudEventImpl<T> build(
.withId(id)
.withSource(source)
.withType(type)
.withTime(time)
.withTime(AttributesImpl.parseZonedDateTime(time).orElse(null))
.withSchemaurl(schemaurl)
.withDatacontentencoding(datacontentencoding)
.withDatacontenttype(datacontenttype)
Expand Down
22 changes: 3 additions & 19 deletions api/src/main/java/io/cloudevents/v1/AttributesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.cloudevents.Attributes;
import io.cloudevents.format.DateTimeFormat;
import java.net.URI;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -64,26 +63,11 @@ public class AttributesImpl implements Attributes {
@Size(min = 1)
private final String subject;

@DateTimeFormat
private final String time;
private final ZonedDateTime time;

public AttributesImpl(String id, URI source, String specversion,
String type, String datacontenttype,
URI dataschema, String subject, ZonedDateTime time) {

this.id = id;
this.source = source;
this.specversion = specversion;
this.type = type;
this.datacontenttype = datacontenttype;
this.dataschema = dataschema;
this.subject = subject;
this.time = formatZonedDateTime(time).get();
}

public AttributesImpl(String id, URI source, String specversion,
String type, String datacontenttype,
URI dataschema, String subject, String time) {
this.id = id;
this.source = source;
this.specversion = specversion;
Expand Down Expand Up @@ -128,7 +112,7 @@ public Optional<String> getSubject() {
}

public Optional<ZonedDateTime> getTime() {
return parseZonedDateTime(time);
return Optional.ofNullable(time);
}

@Override
Expand All @@ -155,7 +139,7 @@ public static AttributesImpl build(
@JsonProperty("time") String time) {

return new AttributesImpl(id, source, specversion, type,
datacontenttype, dataschema, subject, time);
datacontenttype, dataschema, subject, parseZonedDateTime(time).get());
}

/**
Expand Down
Loading

0 comments on commit a4fb601

Please sign in to comment.