You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by th...@apache.org on 2011/06/16 20:12:28 UTC

svn commit: r1136578 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/NonDescendingClock.java

Author: thomasm
Date: Thu Jun 16 18:12:28 2011
New Revision: 1136578

URL: http://svn.apache.org/viewvc?rev=1136578&view=rev
Log:
A non-descending clock implementation.

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/NonDescendingClock.java

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/NonDescendingClock.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/NonDescendingClock.java?rev=1136578&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/NonDescendingClock.java (added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/NonDescendingClock.java Thu Jun 16 18:12:28 2011
@@ -0,0 +1,64 @@
+/*
+ * 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.jackrabbit.mk.util;
+
+/**
+ * A clock that normally returns the current system time, but is guaranteed to
+ * return ascending values. It never returns lower values than the start or last
+ * value, even if the system time is changed. Worst case, it returns the same
+ * value for 1000 times, and then increments by one.
+ */
+public class NonDescendingClock {
+
+    private final static int MAX_REPEAT = 1000;
+
+    private long last;
+    private int count = MAX_REPEAT;
+
+    /**
+     * Create a new clock. The next returned time will be at least one larger
+     * than the last time.
+     *
+     * @param lastTime the last used time
+     */
+    public NonDescendingClock(long lastTime) {
+        this.last = lastTime + 1;
+    }
+
+    /**
+     * Get the current time, ensuring that it is never older than the last
+     * returned time, even if the system time was changed. Worst case, this
+     * method will return the same value for at most 1000 calls. After that, the
+     * value is incremented by one.
+     *
+     * @return the time
+     */
+    public long time() {
+        long now = System.currentTimeMillis();
+        if (now > last) {
+            last = now;
+            count = 0;
+        } else {
+            if (count-- <= 0) {
+                last++;
+                count = MAX_REPEAT;
+            }
+        }
+        return last;
+    }
+
+}