Make gson type adapters more generic 01/117801/1
authorJim Hahn <jrh3@att.com>
Fri, 12 Feb 2021 15:06:27 +0000 (10:06 -0500)
committerJim Hahn <jrh3@att.com>
Fri, 12 Feb 2021 15:08:38 +0000 (10:08 -0500)
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>
gson/src/main/java/org/onap/policy/common/gson/InstantAsMillisTypeAdapter.java
gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java

index 6bcfaac..c63b03c 100644 (file)
@@ -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());
         }
     }
 
index 147fb03..7777e70 100644 (file)
@@ -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);
         }
     }