Skip to content

Commit

Permalink
Override equals & hashCode for JavaDateMathParser
Browse files Browse the repository at this point in the history
Signed-off-by: Bryan Burkholder <[email protected]>
  • Loading branch information
bryanlb committed Feb 17, 2023
1 parent c772596 commit fe01144
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,22 @@ private Instant parseDateTime(String value, ZoneId timeZone, boolean roundUpIfNo
throw new OpenSearchParseException("failed to parse date field [{}] with format [{}]: [{}]", e, value, format, e.getMessage());
}
}

@Override
public boolean equals(Object obj) {
if (obj.getClass().equals(this.getClass()) == false) {
return false;
}

JavaDateMathParser other = (JavaDateMathParser) obj;

return Objects.equals(formatter, other.formatter)
&& Objects.equals(format, other.format)
&& Objects.equals(roundupParser, other.roundupParser);
}

@Override
public int hashCode() {
return Objects.hash(formatter, format, roundupParser);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

public class JavaDateMathParserTests extends OpenSearchTestCase {

Expand Down Expand Up @@ -381,6 +382,22 @@ public void testOnlyCallsNowIfNecessary() {
assertTrue(called.get());
}

public void testEqualsAndHashcode() {
assertThat(DateFormatters.forPattern("YYYY").toDateMathParser(), equalTo(DateFormatters.forPattern("YYYY").toDateMathParser()));
assertThat(
DateFormatters.forPattern("YYYY").toDateMathParser().hashCode(),
is(DateFormatters.forPattern("YYYY").toDateMathParser().hashCode())
);

assertThat(
DateFormatters.forPattern("YYYY").withZone(ZoneId.of("CET")).toDateMathParser(),
not(equalTo(DateFormatters.forPattern("YYYY").toDateMathParser()))
);
DateMathParser f1 = DateFormatters.forPattern("YYYY").withLocale(Locale.CANADA).toDateMathParser();
DateMathParser f2 = DateFormatters.forPattern("YYYY").withLocale(Locale.FRENCH).toDateMathParser();
assertThat(f1, not(equalTo(f2)));
}

private void assertDateMathEquals(String toTest, String expected) {
assertDateMathEquals(toTest, expected, 0, false, null);
}
Expand Down

0 comments on commit fe01144

Please sign in to comment.