|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.mongodb.internal.time; |
| 17 | + |
| 18 | +import com.mongodb.annotations.Immutable; |
| 19 | +import com.mongodb.internal.VisibleForTesting; |
| 20 | + |
| 21 | +import java.time.Clock; |
| 22 | +import java.time.Duration; |
| 23 | + |
| 24 | +import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE; |
| 25 | + |
| 26 | +/** |
| 27 | + * A <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html">value-based</a> class |
| 28 | + * representing a point on a timeline. The origin of this timeline has no known relation to the |
| 29 | + * {@linkplain Clock#systemUTC() system clock}. The same timeline is used by all {@link TimePoint}s within the same process. |
| 30 | + * <p> |
| 31 | + * Methods operating on a pair of {@link TimePoint}s, |
| 32 | + * for example, {@link #durationSince(TimePoint)}, {@link #compareTo(TimePoint)}, |
| 33 | + * or producing a point from another one, for example, {@link #add(Duration)}, |
| 34 | + * work correctly only if the duration between the points is not greater than {@link Long#MAX_VALUE} nanoseconds, |
| 35 | + * which is more than 292 years.</p> |
| 36 | + * <p> |
| 37 | + * This class is not part of the public API and may be removed or changed at any time.</p> |
| 38 | + */ |
| 39 | +@Immutable |
| 40 | +public final class TimePoint implements Comparable<TimePoint> { |
| 41 | + private final long nanos; |
| 42 | + |
| 43 | + private TimePoint(final long nanos) { |
| 44 | + this.nanos = nanos; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns the current {@link TimePoint}. |
| 49 | + */ |
| 50 | + public static TimePoint now() { |
| 51 | + return at(System.nanoTime()); |
| 52 | + } |
| 53 | + |
| 54 | + @VisibleForTesting(otherwise = PRIVATE) |
| 55 | + static TimePoint at(final long nanos) { |
| 56 | + return new TimePoint(nanos); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * The {@link Duration} between this {@link TimePoint} and {@code t}. |
| 61 | + * A {@linkplain Duration#isNegative() negative} {@link Duration} means that |
| 62 | + * this {@link TimePoint} is {@linkplain #compareTo(TimePoint) before} {@code t}. |
| 63 | + * |
| 64 | + * @see #elapsed() |
| 65 | + */ |
| 66 | + public Duration durationSince(final TimePoint t) { |
| 67 | + return Duration.ofNanos(nanos - t.nanos); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * The {@link Duration} between {@link TimePoint#now()} and this {@link TimePoint}. |
| 72 | + * This method is functionally equivalent to {@code TimePoint.now().durationSince(this)}. |
| 73 | + * |
| 74 | + * @see #durationSince(TimePoint) |
| 75 | + */ |
| 76 | + public Duration elapsed() { |
| 77 | + return Duration.ofNanos(System.nanoTime() - nanos); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns a {@link TimePoint} that is {@code duration} away from this one. |
| 82 | + * |
| 83 | + * @param duration A duration that may also be {@linkplain Duration#isNegative() negative}. |
| 84 | + */ |
| 85 | + public TimePoint add(final Duration duration) { |
| 86 | + long durationNanos = duration.toNanos(); |
| 87 | + return TimePoint.at(nanos + durationNanos); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * If this {@link TimePoint} is less/greater than {@code t}, then it is before/after {@code t}. |
| 92 | + * <p> |
| 93 | + * {@inheritDoc}</p> |
| 94 | + */ |
| 95 | + @Override |
| 96 | + public int compareTo(final TimePoint t) { |
| 97 | + return Long.signum(nanos - t.nanos); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public boolean equals(final Object o) { |
| 102 | + if (this == o) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + if (o == null || getClass() != o.getClass()) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + final TimePoint timePoint = (TimePoint) o; |
| 109 | + return nanos == timePoint.nanos; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public int hashCode() { |
| 114 | + return Long.hashCode(nanos); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public String toString() { |
| 119 | + return "TimePoint{" |
| 120 | + + "nanos=" + nanos |
| 121 | + + '}'; |
| 122 | + } |
| 123 | +} |
0 commit comments