From 564ef19e77b5f8b47fe54822939e806162b7b5fb Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 12 Feb 2021 10:06:27 -0500 Subject: 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 --- .../common/gson/InstantAsMillisTypeAdapter.java | 8 ++----- .../common/gson/ZonedDateTimeTypeAdapter.java | 26 ++++++++++++++++++---- 2 files changed, 24 insertions(+), 10 deletions(-) (limited to 'gson/src') 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 { 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 { - 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 { 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 { if (value == null) { out.nullValue(); } else { - String text = value.format(FORMATTER); + String text = value.format(formatter); out.value(text); } } -- cgit 1.2.3-korg