Use common LocalDateTimeTypeAdaptor for SO 78/115978/1
authorJim Hahn <jrh3@att.com>
Mon, 30 Nov 2020 19:32:47 +0000 (14:32 -0500)
committerJim Hahn <jrh3@att.com>
Mon, 30 Nov 2020 19:34:03 +0000 (14:34 -0500)
SO has its own specialized GSON type adapter.  Modified it to subclass
from the common type adapter.

Issue-ID: POLICY-2903
Change-Id: I578eecbb1a37b5c95c5c27e6deb5c9f72ca7ae28
Signed-off-by: Jim Hahn <jrh3@att.com>
models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/SoLocalDateTimeTypeAdapter.java

index a0e835f..918a3ff 100644 (file)
 
 package org.onap.policy.so.util;
 
-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.LocalDateTime;
 import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeParseException;
-
-/*
- * TODO: combine the functionality of this adapter with existing LocalDateTimeTypeAdapter and eliminate this class.
- */
+import org.onap.policy.common.gson.LocalDateTimeTypeAdapter;
 
 /**
  * GSON Type Adapter for "LocalDateTime" fields, that uses the standard RFC_1123_DATE_TIME
  * formatter.
  */
-public class SoLocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
-    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.RFC_1123_DATE_TIME;
-
-    @Override
-    public LocalDateTime read(JsonReader in) throws IOException {
-        try {
-            if (in.peek() == JsonToken.NULL) {
-                in.nextNull();
-                return null;
-            } else {
-                return LocalDateTime.parse(in.nextString(), FORMATTER);
-            }
-
-        } catch (DateTimeParseException e) {
-            throw new JsonParseException("invalid date", e);
-        }
-    }
+public class SoLocalDateTimeTypeAdapter extends LocalDateTimeTypeAdapter {
 
-    @Override
-    public void write(JsonWriter out, LocalDateTime value) throws IOException {
-        if (value == null) {
-            out.nullValue();
-        } else {
-            String text = value.format(FORMATTER);
-            out.value(text);
-        }
+    public SoLocalDateTimeTypeAdapter() {
+        super(DateTimeFormatter.RFC_1123_DATE_TIME);
     }
 }