Skip to content

Commit

Permalink
Adds equals, hashCode, toString overrides to IntervalSchedule and Cro…
Browse files Browse the repository at this point in the history
  • Loading branch information
dbbaughe committed Feb 6, 2020
1 parent c770158 commit 326b4ac
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;
import com.cronutils.utils.VisibleForTesting;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.xcontent.XContentBuilder;

Expand All @@ -30,6 +31,7 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
import java.util.Optional;

/**
Expand Down Expand Up @@ -138,4 +140,23 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
.endObject();
return builder;
}

@Override
public String toString() {
return Strings.toString(this, false, true);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CronSchedule cronSchedule = (CronSchedule) o;
return timezone.equals(cronSchedule.timezone) &&
expression.equals(cronSchedule.expression);
}

@Override
public int hashCode() {
return Objects.hash(timezone, expression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.amazon.opendistroforelasticsearch.jobscheduler.spi.schedule;

import com.cronutils.utils.VisibleForTesting;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.xcontent.XContentBuilder;

Expand All @@ -27,6 +28,7 @@
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
Expand Down Expand Up @@ -139,4 +141,25 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
void setClock(Clock clock) {
this.clock = clock;
}

@Override
public String toString() {
return Strings.toString(this, false, true);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IntervalSchedule intervalSchedule = (IntervalSchedule) o;
return startTime.equals(intervalSchedule.startTime) &&
interval == intervalSchedule.interval &&
unit == intervalSchedule.unit &&
intervalInMillis == intervalSchedule.intervalInMillis;
}

@Override
public int hashCode() {
return Objects.hash(startTime, interval, unit, intervalInMillis);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,15 @@ public void testToXContent() throws IOException {
Assert.assertEquals(expectedJsonStr,
XContentHelper.toXContent(schedule, XContentType.JSON, false).utf8ToString());
}

@Test
public void testCronScheduleEqualsAndHashCode() {
CronSchedule cronScheduleOne = new CronSchedule("* * * * *", ZoneId.of("PST8PDT"));
CronSchedule cronScheduleTwo = new CronSchedule("* * * * *", ZoneId.of("PST8PDT"));
CronSchedule cronScheduleThree = new CronSchedule("1 * * * *", ZoneId.of("PST8PDT"));

Assert.assertEquals(cronScheduleOne, cronScheduleTwo);
Assert.assertNotEquals(cronScheduleOne, cronScheduleThree);
Assert.assertEquals(cronScheduleOne.hashCode(), cronScheduleTwo.hashCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,16 @@ public void testToXContent() throws IOException {
Assert.assertEquals(xContentJsonStr, XContentHelper.toXContent(this.intervalSchedule, XContentType.JSON, false)
.utf8ToString());
}

@Test
public void testIntervalScheduleEqualsAndHashCode() {
Long epochMilli = Instant.now().toEpochMilli();
IntervalSchedule intervalScheduleOne = new IntervalSchedule(Instant.ofEpochMilli(epochMilli), 5, ChronoUnit.MINUTES);
IntervalSchedule intervalScheduleTwo = new IntervalSchedule(Instant.ofEpochMilli(epochMilli), 5, ChronoUnit.MINUTES);
IntervalSchedule intervalScheduleThree = new IntervalSchedule(Instant.ofEpochMilli(epochMilli), 4, ChronoUnit.MINUTES);

Assert.assertEquals(intervalScheduleOne, intervalScheduleTwo);
Assert.assertNotEquals(intervalScheduleOne, intervalScheduleThree);
Assert.assertEquals(intervalScheduleOne.hashCode(), intervalScheduleTwo.hashCode());
}
}

0 comments on commit 326b4ac

Please sign in to comment.