author | prr |
Tue, 15 Jul 2014 11:22:14 -0700 | |
changeset 25522 | 10d789df41bb |
parent 23711 | 95fb268275e3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23711
95fb268275e3
8039022: Fix serial lint warnings in sun.tools.java
darcy
parents:
23010
diff
changeset
|
2 |
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.jmx.snmp; |
|
27 |
||
28 |
||
29 |
import java.io.*; |
|
30 |
import java.util.Hashtable; |
|
31 |
import java.util.*; |
|
32 |
||
33 |
||
34 |
||
35 |
/** This class is used for implementing enumerated values. |
|
36 |
* |
|
37 |
* An enumeration is represented by a class derived from Enumerated. |
|
38 |
* The derived class defines what are the permitted values in the enumeration. |
|
39 |
* |
|
40 |
* An enumerated value is represented by an instance of the derived class. |
|
41 |
* It can be represented : |
|
42 |
* - as an integer |
|
43 |
* - as a string |
|
44 |
* |
|
45 |
* <p><b>This API is a Sun Microsystems internal API and is subject |
|
46 |
* to change without notice.</b></p> |
|
47 |
*/ |
|
23711
95fb268275e3
8039022: Fix serial lint warnings in sun.tools.java
darcy
parents:
23010
diff
changeset
|
48 |
@SuppressWarnings("serial") // JDK implementation class |
2 | 49 |
abstract public class Enumerated implements Serializable { |
50 |
||
51 |
/** |
|
52 |
* Construct an enumerated with a default value. |
|
53 |
* The default value is the first available in getIntTable(). |
|
54 |
* @exception IllegalArgumentException One of the arguments passed to the method is illegal or inappropriate. |
|
55 |
*/ |
|
56 |
public Enumerated() throws IllegalArgumentException { |
|
14677 | 57 |
Enumeration<Integer> e =getIntTable().keys(); |
2 | 58 |
if (e.hasMoreElements()) { |
14677 | 59 |
value = e.nextElement().intValue() ; |
2 | 60 |
} |
61 |
else { |
|
62 |
throw new IllegalArgumentException() ; |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Construct an enumerated from its integer form. |
|
68 |
* |
|
69 |
* @param valueIndex The integer form. |
|
70 |
* @exception IllegalArgumentException One of the arguments passed to |
|
71 |
* the method is illegal or inappropriate. |
|
72 |
*/ |
|
73 |
public Enumerated(int valueIndex) throws IllegalArgumentException { |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
23711
diff
changeset
|
74 |
if (getIntTable().get(valueIndex) == null) { |
2 | 75 |
throw new IllegalArgumentException() ; |
76 |
} |
|
77 |
value = valueIndex ; |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Construct an enumerated from its Integer form. |
|
82 |
* |
|
83 |
* @param valueIndex The Integer form. |
|
84 |
* @exception IllegalArgumentException One of the arguments passed to |
|
85 |
* the method is illegal or inappropriate. |
|
86 |
*/ |
|
87 |
public Enumerated(Integer valueIndex) throws IllegalArgumentException { |
|
88 |
if (getIntTable().get(valueIndex) == null) { |
|
89 |
throw new IllegalArgumentException() ; |
|
90 |
} |
|
91 |
value = valueIndex.intValue() ; |
|
92 |
} |
|
93 |
||
94 |
||
95 |
/** |
|
96 |
* Construct an enumerated from its string form. |
|
97 |
* |
|
98 |
* @param valueString The string form. |
|
99 |
* @exception IllegalArgumentException One of the arguments passed |
|
100 |
* to the method is illegal or inappropriate. |
|
101 |
*/ |
|
102 |
public Enumerated(String valueString) throws IllegalArgumentException { |
|
14677 | 103 |
Integer index = getStringTable().get(valueString) ; |
2 | 104 |
if (index == null) { |
105 |
throw new IllegalArgumentException() ; |
|
106 |
} |
|
107 |
else { |
|
108 |
value = index.intValue() ; |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
||
113 |
/** |
|
114 |
* Return the integer form of the enumerated. |
|
115 |
* |
|
116 |
* @return The integer form |
|
117 |
*/ |
|
118 |
||
119 |
public int intValue() { |
|
120 |
return value ; |
|
121 |
} |
|
122 |
||
123 |
||
124 |
/** |
|
125 |
* Returns an Java enumeration of the permitted integers. |
|
126 |
* |
|
127 |
* @return An enumeration of Integer instances |
|
128 |
*/ |
|
129 |
||
14677 | 130 |
public Enumeration<Integer> valueIndexes() { |
2 | 131 |
return getIntTable().keys() ; |
132 |
} |
|
133 |
||
134 |
||
135 |
/** |
|
136 |
* Returns an Java enumeration of the permitted strings. |
|
137 |
* |
|
138 |
* @return An enumeration of String instances |
|
139 |
*/ |
|
140 |
||
14677 | 141 |
public Enumeration<String> valueStrings() { |
2 | 142 |
return getStringTable().keys() ; |
143 |
} |
|
144 |
||
145 |
||
146 |
/** |
|
147 |
* Compares this enumerated to the specified enumerated. |
|
148 |
* |
|
149 |
* The result is true if and only if the argument is not null |
|
150 |
* and is of the same class. |
|
151 |
* |
|
152 |
* @param obj The object to compare with. |
|
153 |
* |
|
154 |
* @return True if this and obj are the same; false otherwise |
|
155 |
*/ |
|
14677 | 156 |
@Override |
2 | 157 |
public boolean equals(Object obj) { |
158 |
||
159 |
return ((obj != null) && |
|
160 |
(getClass() == obj.getClass()) && |
|
161 |
(value == ((Enumerated)obj).value)) ; |
|
162 |
} |
|
163 |
||
164 |
||
165 |
/** |
|
166 |
* Returns the hash code for this enumerated. |
|
167 |
* |
|
168 |
* @return A hash code value for this object. |
|
169 |
*/ |
|
14677 | 170 |
@Override |
2 | 171 |
public int hashCode() { |
172 |
String hashString = getClass().getName() + String.valueOf(value) ; |
|
173 |
return hashString.hashCode() ; |
|
174 |
} |
|
175 |
||
176 |
||
177 |
/** |
|
178 |
* Returns the string form of this enumerated. |
|
179 |
* |
|
180 |
* @return The string for for this object. |
|
181 |
*/ |
|
14677 | 182 |
@Override |
2 | 183 |
public String toString() { |
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
23711
diff
changeset
|
184 |
return getIntTable().get(value); |
2 | 185 |
} |
186 |
||
187 |
||
188 |
/** |
|
189 |
* Returns the hashtable of the integer forms. |
|
190 |
* getIntTable().get(x) returns the string form associated |
|
191 |
* to the integer x. |
|
192 |
* |
|
193 |
* This method must be implemented by the derived class. |
|
194 |
* |
|
195 |
* @return An hashtable for read-only purpose |
|
196 |
*/ |
|
197 |
||
14677 | 198 |
protected abstract Hashtable<Integer,String> getIntTable() ; |
2 | 199 |
|
200 |
||
201 |
||
202 |
/** |
|
203 |
* Returns the hashtable of the string forms. |
|
204 |
* getStringTable().get(s) returns the integer form associated |
|
205 |
* to the string s. |
|
206 |
* |
|
207 |
* This method must be implemented by the derived class. |
|
208 |
* |
|
209 |
* @return An hashtable for read-only purpose |
|
210 |
*/ |
|
211 |
||
14677 | 212 |
protected abstract Hashtable<String,Integer> getStringTable() ; |
2 | 213 |
|
214 |
||
215 |
/** |
|
216 |
* This variable keeps the integer form of the enumerated. |
|
14677 | 217 |
* The string form is retrieved using getIntTable(). |
2 | 218 |
*/ |
219 |
protected int value ; |
|
220 |
||
221 |
} |