diff options
author | Jim Hahn <jrh3@att.com> | 2021-02-18 17:15:07 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-02-18 17:24:26 -0500 |
commit | 758cd2e691950843415515ba6df2295679bd938b (patch) | |
tree | 1c3acdcf18a04f655b6e642fc80a7d8630934f24 /gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java | |
parent | 00ee2d5b4f1e847d8e272c79f66230aa30cb11d6 (diff) |
Remove duplicate code from type adapters
Extracted a common superclass, StringTypeAdapter, out of the gson type
adapter classes.
Issue-ID: POLICY-2914
Change-Id: I4bbd2a6e8372e7f42c4952ba9ff03eed97473fb2
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java')
-rw-r--r-- | gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java | 43 |
1 files changed, 4 insertions, 39 deletions
diff --git a/gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java index 7777e702..928fae95 100644 --- a/gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java +++ b/gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java @@ -20,63 +20,28 @@ package org.onap.policy.common.gson; -import com.google.gson.JsonParseException; -import com.google.gson.TypeAdapter; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonToken; -import com.google.gson.stream.JsonWriter; -import java.io.IOException; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeParseException; /** * GSON Type Adapter for "ZonedDateTime" fields, that uses the standard * ISO_ZONED_DATE_TIME formatter. */ -public class ZonedDateTimeTypeAdapter extends TypeAdapter<ZonedDateTime> { - private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ISO_ZONED_DATE_TIME; - - private final DateTimeFormatter formatter; - +public class ZonedDateTimeTypeAdapter extends StringTypeAdapter<ZonedDateTime> { /** * Constructs an adapter that uses the ISO_ZONED_DATE_TIME formatter. */ public ZonedDateTimeTypeAdapter() { - this(DEFAULT_FORMATTER); + this(DateTimeFormatter.ISO_ZONED_DATE_TIME); } /** * Constructs an adapter that uses the specified formatter for reading and writing. + * * @param formatter date-time formatter */ public ZonedDateTimeTypeAdapter(DateTimeFormatter formatter) { - this.formatter = formatter; - } - - @Override - public ZonedDateTime read(JsonReader in) throws IOException { - try { - if (in.peek() == JsonToken.NULL) { - in.nextNull(); - return null; - } else { - return ZonedDateTime.parse(in.nextString(), formatter); - } - - } catch (DateTimeParseException e) { - throw new JsonParseException("invalid date", e); - } - } - - @Override - public void write(JsonWriter out, ZonedDateTime value) throws IOException { - if (value == null) { - out.nullValue(); - } else { - String text = value.format(formatter); - out.value(text); - } + super("date", string -> ZonedDateTime.parse(string, formatter), value -> value.format(formatter)); } } |