This project has retired. For details please refer to its Attic page.
SystemTime xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * 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 and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.time;
20  
21  import java.util.Date;
22  
23  /**
24   * Implementation of Time that is thread-safe and should be used in
25   * production.
26   */
27  public class SystemTime implements Time {
28    /**
29     * Single instance of this object
30     */
31    private static final SystemTime SINGLE_TIME = new SystemTime();
32  
33    @Override
34    public long getMilliseconds() {
35      return System.currentTimeMillis();
36    }
37  
38    @Override
39    public long getMicroseconds() {
40      return getNanoseconds() / NS_PER_US;
41    }
42  
43    @Override
44    public long getNanoseconds() {
45      return System.nanoTime();
46    }
47  
48    @Override
49    public int getSeconds() {
50      return (int) (getMilliseconds() / MS_PER_SECOND);
51    }
52  
53    @Override
54    public Date getCurrentDate() {
55      return new Date();
56    }
57  
58    @Override
59    public void sleep(long milliseconds) throws InterruptedException {
60      Thread.sleep(milliseconds);
61    }
62  
63    /**
64     * Get an instance (shared) of this object
65     *
66     * @return Instance of this object
67     */
68    public static Time get() {
69      return SINGLE_TIME;
70    }
71  }