From 5a7fd4f4c0efc1f300b05a250f484517426fb2e4 Mon Sep 17 00:00:00 2001 From: Bryan Burkholder <771133+bryanlb@users.noreply.github.com> Date: Tue, 7 Mar 2023 11:26:21 -0700 Subject: [PATCH] Implement equals/hashcode for named DocValueFormat inner classes (#6357) Signed-off-by: Bryan Burkholder --- CHANGELOG.md | 1 + .../org/opensearch/search/DocValueFormat.java | 21 +++++++++++++++++++ .../search/DocValueFormatTests.java | 3 +++ 3 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b9f5ce0272d0..67e022825e50b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,6 +93,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Removed ### Fixed +- Added equals/hashcode for named DocValueFormat.DateTime inner class ([#6357](https://github.com/opensearch-project/OpenSearch/pull/6357)) ### Security diff --git a/server/src/main/java/org/opensearch/search/DocValueFormat.java b/server/src/main/java/org/opensearch/search/DocValueFormat.java index 4b592303ee253..99be37cb85587 100644 --- a/server/src/main/java/org/opensearch/search/DocValueFormat.java +++ b/server/src/main/java/org/opensearch/search/DocValueFormat.java @@ -272,6 +272,27 @@ public double parseDouble(String value, boolean roundUp, LongSupplier now) { public String toString() { return "DocValueFormat.DateTime(" + formatter + ", " + timeZone + ", " + resolution + ")"; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + DateTime that = (DateTime) o; + + return Objects.equals(formatter, that.formatter) + && Objects.equals(timeZone, that.timeZone) + && Objects.equals(resolution, that.resolution); + } + + @Override + public int hashCode() { + return Objects.hash(formatter, timeZone, resolution); + } } DocValueFormat GEOHASH = new DocValueFormat() { diff --git a/server/src/test/java/org/opensearch/search/DocValueFormatTests.java b/server/src/test/java/org/opensearch/search/DocValueFormatTests.java index bd0fbfe69960c..092838ba764c1 100644 --- a/server/src/test/java/org/opensearch/search/DocValueFormatTests.java +++ b/server/src/test/java/org/opensearch/search/DocValueFormatTests.java @@ -76,6 +76,7 @@ public void testSerialization() throws Exception { DocValueFormat vf = in.readNamedWriteable(DocValueFormat.class); assertEquals(DocValueFormat.Decimal.class, vf.getClass()); assertEquals("###.##", ((DocValueFormat.Decimal) vf).pattern); + assertEquals(decimalFormat, vf); DateFormatter formatter = DateFormatter.forPattern("epoch_second"); DocValueFormat.DateTime dateFormat = new DocValueFormat.DateTime(formatter, ZoneOffset.ofHours(1), Resolution.MILLISECONDS); @@ -87,6 +88,7 @@ public void testSerialization() throws Exception { assertEquals("epoch_second", ((DocValueFormat.DateTime) vf).formatter.pattern()); assertEquals(ZoneOffset.ofHours(1), ((DocValueFormat.DateTime) vf).timeZone); assertEquals(Resolution.MILLISECONDS, ((DocValueFormat.DateTime) vf).resolution); + assertEquals(dateFormat, vf); DocValueFormat.DateTime nanosDateFormat = new DocValueFormat.DateTime(formatter, ZoneOffset.ofHours(1), Resolution.NANOSECONDS); out = new BytesStreamOutput(); @@ -97,6 +99,7 @@ public void testSerialization() throws Exception { assertEquals("epoch_second", ((DocValueFormat.DateTime) vf).formatter.pattern()); assertEquals(ZoneOffset.ofHours(1), ((DocValueFormat.DateTime) vf).timeZone); assertEquals(Resolution.NANOSECONDS, ((DocValueFormat.DateTime) vf).resolution); + assertEquals(nanosDateFormat, vf); out = new BytesStreamOutput(); out.writeNamedWriteable(DocValueFormat.GEOHASH);