1 /* |
|
2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. |
|
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 |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
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 * |
|
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. |
|
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 */ |
|
48 @SuppressWarnings("serial") // JDK implementation class |
|
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 { |
|
57 Enumeration<Integer> e =getIntTable().keys(); |
|
58 if (e.hasMoreElements()) { |
|
59 value = e.nextElement().intValue() ; |
|
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 { |
|
74 if (getIntTable().get(valueIndex) == null) { |
|
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 { |
|
103 Integer index = getStringTable().get(valueString) ; |
|
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 |
|
130 public Enumeration<Integer> valueIndexes() { |
|
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 |
|
141 public Enumeration<String> valueStrings() { |
|
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 */ |
|
156 @Override |
|
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 */ |
|
170 @Override |
|
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 */ |
|
182 @Override |
|
183 public String toString() { |
|
184 return getIntTable().get(value); |
|
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 |
|
198 protected abstract Hashtable<Integer,String> getIntTable() ; |
|
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 |
|
212 protected abstract Hashtable<String,Integer> getStringTable() ; |
|
213 |
|
214 |
|
215 /** |
|
216 * This variable keeps the integer form of the enumerated. |
|
217 * The string form is retrieved using getIntTable(). |
|
218 */ |
|
219 protected int value ; |
|
220 |
|
221 } |
|