Skip to content

Commit

Permalink
Implement equals/hashcode for named DocValueFormat inner classes (ope…
Browse files Browse the repository at this point in the history
…nsearch-project#6357)

Signed-off-by: Bryan Burkholder <[email protected]>
Signed-off-by: Mingshi Liu <[email protected]>
  • Loading branch information
bryanlb authored and mingshl committed Mar 24, 2023
1 parent 0814e36 commit d214863
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,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

Expand Down
21 changes: 21 additions & 0 deletions server/src/main/java/org/opensearch/search/DocValueFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand Down

0 comments on commit d214863

Please sign in to comment.