Skip to content

Commit

Permalink
Address review comments. Compute time range nanos on demand and suppo…
Browse files Browse the repository at this point in the history
…rt returning durations.
  • Loading branch information
chipkent committed Dec 6, 2023
1 parent 6e83ad0 commit 0f08c74
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
23 changes: 13 additions & 10 deletions engine/time/src/main/java/io/deephaven/time/calendar/TimeRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

import io.deephaven.time.DateTimeUtils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.Objects;
Expand All @@ -21,7 +18,6 @@
public class TimeRange<T extends Comparable<T> & Temporal> {
private final T start;
private final T end;
private final long nanos;

/**
* Create a new time range.
Expand All @@ -47,8 +43,6 @@ public class TimeRange<T extends Comparable<T> & Temporal> {
throw new IllegalArgumentException(
"Start is the same as end: startTime=" + startTime + " endTime=" + endTime);
}

this.nanos = start.until(end, ChronoUnit.NANOS);
}

/**
Expand All @@ -75,7 +69,16 @@ public T end() {
* @return length of the range in nanoseconds
*/
public long nanos() {
return nanos;
return start.until(end, ChronoUnit.NANOS);
}

/**
* Duration of the range.
*
* @return duration of the range
*/
public Duration duration() {
return Duration.ofNanos(nanos());
}

/**
Expand All @@ -97,12 +100,12 @@ public boolean equals(Object o) {
if (!(o instanceof TimeRange))
return false;
TimeRange<?> that = (TimeRange<?>) o;
return nanos == that.nanos && start.equals(that.start) && end.equals(that.end);
return start.equals(that.start) && end.equals(that.end);
}

@Override
public int hashCode() {
return Objects.hash(start, end, nanos);
return Objects.hash(start, end);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import io.deephaven.time.DateTimeUtils;
import junit.framework.TestCase;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.*;
import java.util.Objects;

import static org.junit.Assert.assertNotEquals;
Expand Down Expand Up @@ -53,6 +50,7 @@ public void testTimeRange() {
assertEquals(open1, period.start());
assertEquals(close1, period.end());
assertEquals(DateTimeUtils.HOUR, period.nanos());
assertEquals(Duration.ofNanos(DateTimeUtils.HOUR), period.duration());

assertTrue(period.contains(open1));
assertTrue(period
Expand Down

0 comments on commit 0f08c74

Please sign in to comment.