Skip to content

Commit

Permalink
TimeLeapClock constructor with Instant, and "now" method
Browse files Browse the repository at this point in the history
This commit adds a constructor, with an Instant Option only.

This commit is one of many prepare PRs / commits for a bigger PR:
SumStateAlert OpenEMS#2260
By Seperating the big OpenEMS#2260 into smaller commits we would like to make the review easier.

Co-authored-by:  Kai Jeschek <[email protected]>
  • Loading branch information
DerStoecki and da-Kai committed Oct 30, 2023
1 parent d629145 commit a98b8ee
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions io.openems.common/src/io/openems/common/test/TimeLeapClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalUnit;

public class TimeLeapClock extends Clock {
Expand All @@ -21,6 +22,10 @@ public TimeLeapClock(ZoneId zone) {
this(Instant.now(), zone);
}

public TimeLeapClock(Instant start) {
this(start, ZoneOffset.UTC);
}

public TimeLeapClock() {
this(Instant.now(), ZoneOffset.UTC);
}
Expand All @@ -30,7 +35,7 @@ public Clock withZone(ZoneId zone) {
if (zone.equals(this.zone)) { // intentional NPE
return this;
}
return new TimeLeapClock(zone);
return new TimeLeapClock(this.instant, zone);
}

@Override
Expand Down Expand Up @@ -60,11 +65,10 @@ public Instant instant() {

@Override
public boolean equals(Object obj) {
if (obj instanceof TimeLeapClock) {
var other = (TimeLeapClock) obj;
return this.instant.equals(other.instant) && this.zone.equals(other.zone);
}
return false;
return obj == this //
|| obj instanceof Clock other //
&& this.instant.equals(other.instant()) //
&& this.zone.equals(other.getZone());
}

@Override
Expand All @@ -74,6 +78,15 @@ public int hashCode() {

@Override
public String toString() {
return "TimeLeapClock[" + this.instant + "," + this.zone + "]";
return "TimeLeapClock[" + this.instant + ", " + this.zone + "]";
}

/**
* Get current DateTime as {@link ZonedDateTime}.
*
* @return current date and time.
*/
public ZonedDateTime now() {
return ZonedDateTime.ofInstant(this.instant, this.zone);
}
}

0 comments on commit a98b8ee

Please sign in to comment.