summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-02-12 10:06:27 -0500
committerJim Hahn <jrh3@att.com>2021-02-12 10:08:38 -0500
commit564ef19e77b5f8b47fe54822939e806162b7b5fb (patch)
treea8de2616608476b608ee5d1c152f86dd80016d3c
parenta57710faad56015eb3b69a5ce6f8da235ca146c7 (diff)
Make gson type adapters more generic
Enhanced ZonedDateTime adapter so a date format string can be provided. Simplified InstantAsMillis adapter. Issue-ID: POLICY-2905 Change-Id: Ic4ddba19391b165d6a7528ce18c22541d12324c7 Signed-off-by: Jim Hahn <jrh3@att.com>
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/InstantAsMillisTypeAdapter.java8
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java26
2 files changed, 24 insertions, 10 deletions
diff --git a/gson/src/main/java/org/onap/policy/common/gson/InstantAsMillisTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/InstantAsMillisTypeAdapter.java
index 6bcfaac3..c63b03c3 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/InstantAsMillisTypeAdapter.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/InstantAsMillisTypeAdapter.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@ import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.time.Instant;
-import java.util.concurrent.TimeUnit;
/**
* GSON Type Adapter for "Instant" fields, that encodes them as milliseconds.
@@ -38,10 +37,7 @@ public class InstantAsMillisTypeAdapter extends TypeAdapter<Instant> {
if (value == null) {
out.nullValue();
} else {
- long epochMillis = TimeUnit.MILLISECONDS.convert(value.getEpochSecond(), TimeUnit.SECONDS);
- long nanoMillis = TimeUnit.MILLISECONDS.convert(value.getNano(), TimeUnit.NANOSECONDS);
- long millis = epochMillis + nanoMillis;
- out.value(millis);
+ out.value(value.toEpochMilli());
}
}
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 147fb03d..7777e702 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
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,7 +35,25 @@ import java.time.format.DateTimeParseException;
* ISO_ZONED_DATE_TIME formatter.
*/
public class ZonedDateTimeTypeAdapter extends TypeAdapter<ZonedDateTime> {
- private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_ZONED_DATE_TIME;
+ private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ISO_ZONED_DATE_TIME;
+
+ private final DateTimeFormatter formatter;
+
+
+ /**
+ * Constructs an adapter that uses the ISO_ZONED_DATE_TIME formatter.
+ */
+ public ZonedDateTimeTypeAdapter() {
+ this(DEFAULT_FORMATTER);
+ }
+
+ /**
+ * 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 {
@@ -44,7 +62,7 @@ public class ZonedDateTimeTypeAdapter extends TypeAdapter<ZonedDateTime> {
in.nextNull();
return null;
} else {
- return ZonedDateTime.parse(in.nextString(), FORMATTER);
+ return ZonedDateTime.parse(in.nextString(), formatter);
}
} catch (DateTimeParseException e) {
@@ -57,7 +75,7 @@ public class ZonedDateTimeTypeAdapter extends TypeAdapter<ZonedDateTime> {
if (value == null) {
out.nullValue();
} else {
- String text = value.format(FORMATTER);
+ String text = value.format(formatter);
out.value(text);
}
}