1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.giraph.time;
2021import java.util.Date;
22import java.util.concurrent.atomic.AtomicLong;
2324/**25 * Thread-safe implementation of Time for testing that can help get time based26 * ordering of events when desired.27 */28publicclassFakeTimeimplementsTime {
29/** Nanoseconds from the fake epoch */30privatefinal AtomicLong nanosecondsSinceEpoch = new AtomicLong();
3132 @Override
33publiclong getMilliseconds() {
34return nanosecondsSinceEpoch.get() / NS_PER_MS;
35 }
3637 @Override
38publiclong getMicroseconds() {
39return nanosecondsSinceEpoch.get() / NS_PER_US;
40 }
4142 @Override
43publiclong getNanoseconds() {
44return nanosecondsSinceEpoch.get();
45 }
4647 @Override
48publicint getSeconds() {
49return (int) (nanosecondsSinceEpoch.get() / NS_PER_SECOND);
50 }
5152 @Override
53public Date getCurrentDate() {
54returnnew Date(getMilliseconds());
55 }
5657 @Override
58publicvoid sleep(long milliseconds) throws InterruptedException {
59 nanosecondsSinceEpoch.getAndAdd(milliseconds * NS_PER_MS);
60 }
61 }