Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@
* (inclusive) to {@link #end} (exclusive).
*/
public class IntervalWindow extends BoundedWindow implements Comparable<IntervalWindow> {

/** Start of the interval, inclusive. */
private final Instant start;

/** End of the interval, exclusive. */
private final Instant end;

// Cached hashCode. ints don't tear and access don't need to be synchronized.
// Stale reads if any will return 0 and will recalculate hashCode.
// ByteString and String hashCodes are cached similarly.
private int hashCode; // Default is 0.

/** Creates a new IntervalWindow that represents the half-open time interval [start, end). */
public IntervalWindow(Instant start, Instant end) {
this.start = start;
Expand Down Expand Up @@ -103,10 +109,13 @@ public boolean equals(@Nullable Object o) {

@Override
public int hashCode() {
// The end values are themselves likely to be arithmetic sequence, which
// is a poor distribution to use for a hashtable, so we
// add a highly non-linear transformation.
return (int) (start.getMillis() + modInverse((int) (end.getMillis() << 1) + 1));
if (hashCode == 0) {
// The end values are themselves likely to be arithmetic sequence, which
// is a poor distribution to use for a hashtable, so we
// add a highly non-linear transformation.
hashCode = (int) (start.getMillis() + modInverse((int) (end.getMillis() << 1) + 1));
}
return hashCode;
}

/** Compute the inverse of (odd) x mod 2^32. */
Expand Down
Loading