Add our own Deadline implementation
Project: http://git-wip-us.apache.org/repos/asf/flink/repo Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/c4861250 Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/c4861250 Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/c4861250 Branch: refs/heads/release-1.5 Commit: c48612501b14174a04c7d1ad31c1e34de5e59854 Parents: 9e253b5 Author: Aljoscha Krettek <aljoscha.kret...@gmail.com> Authored: Wed Feb 28 15:06:59 2018 +0100 Committer: Aljoscha Krettek <aljoscha.kret...@gmail.com> Committed: Sun Mar 11 08:31:56 2018 -0700 ---------------------------------------------------------------------- .../apache/flink/api/common/time/Deadline.java | 68 ++++++++++++++++++++ 1 file changed, 68 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flink/blob/c4861250/flink-core/src/main/java/org/apache/flink/api/common/time/Deadline.java ---------------------------------------------------------------------- diff --git a/flink-core/src/main/java/org/apache/flink/api/common/time/Deadline.java b/flink-core/src/main/java/org/apache/flink/api/common/time/Deadline.java new file mode 100644 index 0000000..2db6579 --- /dev/null +++ b/flink-core/src/main/java/org/apache/flink/api/common/time/Deadline.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.flink.api.common.time; + +import org.apache.flink.annotation.Internal; + +import java.time.Duration; + +/** + * This class stores a deadline, as obtained via {@link #now()} or from {@link #plus(Duration)}. + */ +@Internal +public class Deadline { + private final long timeNanos; + + private Deadline(Duration time) { + this.timeNanos = time.toNanos(); + } + + public Deadline plus(Duration other) { + return new Deadline(Duration.ofNanos(timeNanos).plus(other)); + } + + /** + * Returns the time left between the deadline and now. The result is negative if the deadline + * has passed. + */ + public Duration timeLeft() { + return Duration.ofNanos(timeNanos).minus(Duration.ofNanos(System.nanoTime())); + } + + /** + * Returns whether there is any time left between the deadline and now. + */ + public boolean hasTimeLeft() { + return !isOverdue(); + } + + /** + * Determines whether the deadline is in the past, i.e. whether the time left is negative. + */ + public boolean isOverdue() { + return timeNanos - System.nanoTime() < 0; + } + + /** + * Constructs a {@link Deadline} that has now as the deadline. Use this and then extend via + * {@link #plus(Duration)} to specify a deadline in the future. + */ + public static Deadline now() { + return new Deadline(Duration.ofNanos(System.nanoTime())); + } +}