KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > osgi > util > measurement > State


1 /*
2  * $Header: /cvshome/build/org.osgi.util.measurement/src/org/osgi/util/measurement/State.java,v 1.8 2006/06/16 16:31:34 hargrave Exp $
3  *
4  * Copyright (c) OSGi Alliance (2002, 2006). All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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 package org.osgi.util.measurement;
19
20 /**
21  * Groups a state name, value and timestamp.
22  *
23  * <p>
24  * The state itself is represented as an integer and the time is measured in
25  * milliseconds since midnight, January 1, 1970 UTC.
26  *
27  * <p>
28  * A <code>State</code> object is immutable so that it may be easily shared.
29  *
30  * @version $Revision: 1.8 $
31  */

32 public class State {
33     final int value;
34     final long time;
35     final String JavaDoc name;
36
37     /**
38      * Create a new <code>State</code> object.
39      *
40      * @param value The value of the state.
41      * @param name The name of the state.
42      * @param time The time measured in milliseconds since midnight, January 1,
43      * 1970 UTC.
44      */

45     public State(int value, String JavaDoc name, long time) {
46         this.value = value;
47         this.name = name;
48         this.time = time;
49     }
50
51     /**
52      * Create a new <code>State</code> object with a time of 0.
53      *
54      * @param value The value of the state.
55      * @param name The name of the state.
56      */

57     public State(int value, String JavaDoc name) {
58         this(value, name, 0);
59     }
60
61     /**
62      * Returns the value of this <code>State</code>.
63      *
64      * @return The value of this <code>State</code> object.
65      */

66     public final int getValue() {
67         return value;
68     }
69
70     /**
71      * Returns the time with which this <code>State</code> was created.
72      *
73      * @return The time with which this <code>State</code> was created. The time
74      * is measured in milliseconds since midnight, January 1, 1970 UTC.
75      */

76     public final long getTime() {
77         return time;
78     }
79
80     /**
81      * Returns the name of this <code>State</code>.
82      *
83      * @return The name of this <code>State</code> object.
84      */

85     public final String JavaDoc getName() {
86         return name;
87     }
88
89     /**
90      * Returns a <code>String</code> object representing this object.
91      *
92      * @return a <code>String</code> object representing this object.
93      */

94     public String JavaDoc toString() {
95         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
96         sb.append(value);
97         if (name != null) {
98             sb.append(" \"");
99             sb.append(name);
100             sb.append("\"");
101         }
102         return (sb.toString());
103     }
104
105     /**
106      * Returns a hash code value for this object.
107      *
108      * @return A hash code value for this object.
109      */

110     public int hashCode() {
111         int hash = value;
112         if (name != null) {
113             hash ^= name.hashCode();
114         }
115         return hash;
116     }
117
118     /**
119      * Return whether the specified object is equal to this object. Two
120      * <code>State</code> objects are equal if they have same value and name.
121      *
122      * @param obj The object to compare with this object.
123      * @return <code>true</code> if this object is equal to the specified object;
124      * <code>false</code> otherwise.
125      */

126     public boolean equals(Object JavaDoc obj) {
127         if (this == obj) {
128             return true;
129         }
130         if (!(obj instanceof State)) {
131             return false;
132         }
133         State that = (State) obj;
134         if (value != that.value) {
135             return false;
136         }
137         if (name == that.name) {
138             return true;
139         }
140         if (name == null) {
141             return false;
142         }
143         return name.equals(that.name);
144     }
145 }
146
Popular Tags