-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add TimePoint
and use it in Timeout
#1164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
807d185
Add `Timer` and use it in `Timeout`
stIncMale 2619552
Remove `Timer.oneOff`
stIncMale d4c5a3e
Rename `Timer` to `TimePoint` and modify its API accordingly
stIncMale 60915ed
Work around a bug in OpenJDK JDK 8
stIncMale 4708466
Merge branch 'master' into JAVA-5076_add_Timer
stIncMale e89fd4c
Improve the wording
stIncMale 15c2257
Simplify code, improve tests
stIncMale f61b569
Reinstate `Timeout.startNow`
stIncMale 31a0f73
Change the wording
stIncMale 2430710
Get rid of tautology
stIncMale 5bb651b
Get rid of tautology
stIncMale a639884
Add `TimePoint.elapsed`
stIncMale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
driver-core/src/main/com/mongodb/internal/time/TimePoint.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mongodb.internal.time; | ||
|
||
import com.mongodb.annotations.Immutable; | ||
import com.mongodb.internal.VisibleForTesting; | ||
|
||
import java.time.Clock; | ||
import java.time.Duration; | ||
|
||
import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE; | ||
|
||
/** | ||
* A <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html">value-based</a> class | ||
* representing a point on a timeline. The origin of this timeline has no known relation to the | ||
* {@linkplain Clock#systemUTC() system clock}. The same timeline is used by all {@link TimePoint}s within the same process. | ||
* <p> | ||
* Methods operating on a pair of {@link TimePoint}s, | ||
* for example, {@link #durationSince(TimePoint)}, {@link #compareTo(TimePoint)}, | ||
* or producing a point from another one, for example, {@link #add(Duration)}, | ||
* work correctly only if the duration between the points is not greater than {@link Long#MAX_VALUE} nanoseconds, | ||
* which is more than 292 years.</p> | ||
* <p> | ||
* This class is not part of the public API and may be removed or changed at any time.</p> | ||
*/ | ||
@Immutable | ||
public final class TimePoint implements Comparable<TimePoint> { | ||
private final long nanos; | ||
|
||
private TimePoint(final long nanos) { | ||
this.nanos = nanos; | ||
} | ||
|
||
/** | ||
* Returns the current {@link TimePoint}. | ||
*/ | ||
public static TimePoint now() { | ||
return at(System.nanoTime()); | ||
} | ||
|
||
@VisibleForTesting(otherwise = PRIVATE) | ||
static TimePoint at(final long nanos) { | ||
return new TimePoint(nanos); | ||
} | ||
|
||
/** | ||
* The {@link Duration} between this {@link TimePoint} and {@code t}. | ||
* A {@linkplain Duration#isNegative() negative} {@link Duration} means that | ||
* this {@link TimePoint} is {@linkplain #compareTo(TimePoint) before} {@code t}. | ||
* | ||
* @see #elapsed() | ||
*/ | ||
public Duration durationSince(final TimePoint t) { | ||
return Duration.ofNanos(nanos - t.nanos); | ||
} | ||
|
||
/** | ||
* The {@link Duration} between {@link TimePoint#now()} and this {@link TimePoint}. | ||
* This method is functionally equivalent to {@code TimePoint.now().durationSince(this)}. | ||
* | ||
* @see #durationSince(TimePoint) | ||
*/ | ||
public Duration elapsed() { | ||
return Duration.ofNanos(System.nanoTime() - nanos); | ||
} | ||
|
||
/** | ||
* Returns a {@link TimePoint} that is {@code duration} away from this one. | ||
* | ||
* @param duration A duration that may also be {@linkplain Duration#isNegative() negative}. | ||
*/ | ||
public TimePoint add(final Duration duration) { | ||
long durationNanos = duration.toNanos(); | ||
return TimePoint.at(nanos + durationNanos); | ||
} | ||
|
||
/** | ||
* If this {@link TimePoint} is less/greater than {@code t}, then it is before/after {@code t}. | ||
* <p> | ||
* {@inheritDoc}</p> | ||
*/ | ||
@Override | ||
public int compareTo(final TimePoint t) { | ||
return Long.signum(nanos - t.nanos); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
final TimePoint timePoint = (TimePoint) o; | ||
return nanos == timePoint.nanos; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Long.hashCode(nanos); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TimePoint{" | ||
+ "nanos=" + nanos | ||
+ '}'; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.