xref: /illumos-gate/usr/src/cmd/pools/poold/com/sun/solaris/service/pools/HRTime.java (revision 4c87aefe8930bd07275b8dd2e96ea5f24d93a52e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * ident	"%Z%%M%	%I%	%E% SMI"
27  */
28 
29 package com.sun.solaris.service.pools;
30 
31 import java.math.BigInteger;
32 import java.lang.Comparable;
33 
34 /**
35  * hrtime_t-like (see gethrtime(3C)) uptime-based time value (i.e., resilient
36  * to changes to the host's clock) for comparison of timestamps of sampled
37  * data.
38  */
39 public class HRTime implements Comparable {
40 	/**
41 	 * The native <code>hrtime_t</code> value.
42 	 */
43 	private UnsignedInt64 value;
44 
45 	/**
46 	 * Constructor.
47 	 */
48 	public HRTime()
49 	{
50 		this.value = timestamp();
51 	}
52 
53 	/**
54 	 * Constructs a new HRTime with the value of the given
55 	 * UnsignedInt64.
56 	 *
57 	 * @param value The timestamp to be used.
58 	 */
59 	public HRTime(UnsignedInt64 value)
60 	{
61 		this.value = value;
62 	}
63 
64 	/**
65 	 * Computes the difference between this time and another, older,
66 	 * time.
67 	 *
68 	 * @param older the time from which to compute the delta.
69 	 * @throws IllegalArgumentException if the given time is not
70 	 * earlier than this one.
71 	 */
72 	public HRTime deltaFrom(HRTime older)
73 	{
74 		if (older.compareTo(value) > 0)
75 			throw(new IllegalArgumentException());
76 		else
77 			return (new HRTime(new UnsignedInt64(value
78 			    .subtract(older.getValue()))));
79 	}
80 
81 	/**
82 	 * Returns this HRTime's value.
83 	 */
84 	public UnsignedInt64 getValue()
85 	{
86 		return (value);
87 	}
88 
89 	/**
90 	 * Return a string representation of this instance.
91 	 */
92 	public String toString()
93 	{
94 		return (value.toString());
95 	}
96 
97 	public int compareTo(Object o) {
98 		HRTime other = (HRTime) o;
99 
100 		return (value.compareTo(other.getValue()));
101 	}
102 
103 	/**
104 	 * Return the current timestamp.
105 	 */
106 	private native UnsignedInt64 timestamp();
107 }
108