This project has retired. For details please refer to its Attic page.
Times 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.concurrent.TimeUnit;
22  
23  /**
24   * Utility methods for Time classes.
25   */
26  public class Times {
27    /** Do not instantiate */
28    private Times() { }
29  
30    /**
31     * Convenience method to measure time in a given TimeUnit.
32     *
33     * @param time Time instance to use
34     * @param timeUnit TimeUnit to measure in
35     * @return long measured time in TimeUnit dimension
36     */
37    public static long get(Time time, TimeUnit timeUnit) {
38      return timeUnit.convert(time.getNanoseconds(), TimeUnit.NANOSECONDS);
39    }
40  
41    /**
42     * Convenience method to get time since the beginning of an event in a given
43     * TimeUnit.
44     *
45     * @param time Time object used for measuring.
46     * @param timeUnit TimeUnit to use for dimension.
47     * @param startTime beginning time to diff against
48     * @return time elapsed since startTime in TimeUnit dimension.
49     */
50    public static long getDiff(Time time, TimeUnit timeUnit, long startTime) {
51      return get(time, timeUnit) - startTime;
52    }
53  
54    /**
55     * Convenience method to get milliseconds since a previous milliseconds
56     * point.
57     *
58     * @param time Time instance to use
59     * @param previousMilliseconds Previous milliseconds
60     * @return Milliseconds elapsed since the previous milliseconds
61     */
62    public static long getMillisecondsSince(Time time,
63                                            long previousMilliseconds) {
64      return time.getMilliseconds() - previousMilliseconds;
65    }
66  
67    /**
68     * Convenience method to get milliseconds since a previous milliseconds
69     * point.
70     *
71     * @param time Time instance to use
72     * @param previousMs Previous milliseconds
73     * @return Milliseconds elapsed since the previous milliseconds
74     */
75    public static long getMsSince(Time time, long previousMs) {
76      return getMillisecondsSince(time, previousMs);
77    }
78  
79    /**
80     * Convenience method to get microseconds since a previous microseconds point.
81     *
82     * @param time Time instance to use
83     * @param previousMicros Previous microseconds
84     * @return Microseconds elapsed since the previous microseconds
85     */
86    public static long getMicrosSince(Time time, long previousMicros) {
87      return time.getMicroseconds() - previousMicros;
88    }
89  
90    /**
91     * Convenience method to get nanoseconds since a previous nanoseconds
92     * point.
93     *
94     * @param time Time instance to use
95     * @param previousNanoseconds Previous nanoseconds
96     * @return Nanoseconds elapsed since the previous nanoseconds
97     */
98    public static long getNanosecondsSince(Time time, long previousNanoseconds) {
99      return time.getNanoseconds() - previousNanoseconds;
100   }
101 
102   /**
103    * Convenience method to get nanoseconds since a previous nanoseconds
104    * point.
105    *
106    * @param time Time instance to use
107    * @param previousNanos Previous nanoseconds
108    * @return Nanoseconds elapsed since the previous nanoseconds
109    */
110   public static long getNanosSince(Time time, long previousNanos) {
111     return getNanosecondsSince(time, previousNanos);
112   }
113 
114   /**
115    * Convenience method to get seconds since a previous seconds
116    * point.
117    *
118    * @param time Time instance to use
119    * @param previousSeconds Previous seconds
120    * @return Seconds elapsed since the previous seconds
121    */
122   public static int getSecondsSince(Time time, int previousSeconds) {
123     return time.getSeconds() - previousSeconds;
124   }
125 
126   /**
127    * Convenience method to get seconds since a previous seconds
128    * point.
129    *
130    * @param time Time instance to use
131    * @param previousSec Previous seconds
132    * @return Seconds elapsed since the previous seconds
133    */
134   public static int getSecSince(Time time, int previousSec) {
135     return getSecondsSince(time, previousSec);
136   }
137 }