001: /*
002: * Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.jmx.snmp;
027:
028: import java.io.*;
029: import java.util.Hashtable;
030: import java.util.*;
031:
032: /** This class is used for implementing enumerated values.
033: *
034: * An enumeration is represented by a class derived from Enumerated.
035: * The derived class defines what are the permitted values in the enumeration.
036: *
037: * An enumerated value is represented by an instance of the derived class.
038: * It can be represented :
039: * - as an integer
040: * - as a string
041: *
042: * <p><b>This API is a Sun Microsystems internal API and is subject
043: * to change without notice.</b></p>
044: */
045:
046: abstract public class Enumerated implements Serializable {
047:
048: /**
049: * Construct an enumerated with a default value.
050: * The default value is the first available in getIntTable().
051: * @exception IllegalArgumentException One of the arguments passed to the method is illegal or inappropriate.
052: */
053: public Enumerated() throws IllegalArgumentException {
054: Enumeration e = getIntTable().keys();
055: if (e.hasMoreElements()) {
056: value = ((Integer) e.nextElement()).intValue();
057: } else {
058: throw new IllegalArgumentException();
059: }
060: }
061:
062: /**
063: * Construct an enumerated from its integer form.
064: *
065: * @param valueIndex The integer form.
066: * @exception IllegalArgumentException One of the arguments passed to
067: * the method is illegal or inappropriate.
068: */
069: public Enumerated(int valueIndex) throws IllegalArgumentException {
070: if (getIntTable().get(new Integer(valueIndex)) == null) {
071: throw new IllegalArgumentException();
072: }
073: value = valueIndex;
074: }
075:
076: /**
077: * Construct an enumerated from its Integer form.
078: *
079: * @param valueIndex The Integer form.
080: * @exception IllegalArgumentException One of the arguments passed to
081: * the method is illegal or inappropriate.
082: */
083: public Enumerated(Integer valueIndex)
084: throws IllegalArgumentException {
085: if (getIntTable().get(valueIndex) == null) {
086: throw new IllegalArgumentException();
087: }
088: value = valueIndex.intValue();
089: }
090:
091: /**
092: * Construct an enumerated from its string form.
093: *
094: * @param valueString The string form.
095: * @exception IllegalArgumentException One of the arguments passed
096: * to the method is illegal or inappropriate.
097: */
098: public Enumerated(String valueString)
099: throws IllegalArgumentException {
100: Integer index = (Integer) getStringTable().get(valueString);
101: if (index == null) {
102: throw new IllegalArgumentException();
103: } else {
104: value = index.intValue();
105: }
106: }
107:
108: /**
109: * Return the integer form of the enumerated.
110: *
111: * @return The integer form
112: */
113:
114: public int intValue() {
115: return value;
116: }
117:
118: /**
119: * Returns an Java enumeration of the permitted integers.
120: *
121: * @return An enumeration of Integer instances
122: */
123:
124: public Enumeration valueIndexes() {
125: return getIntTable().keys();
126: }
127:
128: /**
129: * Returns an Java enumeration of the permitted strings.
130: *
131: * @return An enumeration of String instances
132: */
133:
134: public Enumeration valueStrings() {
135: return getStringTable().keys();
136: }
137:
138: /**
139: * Compares this enumerated to the specified enumerated.
140: *
141: * The result is true if and only if the argument is not null
142: * and is of the same class.
143: *
144: * @param obj The object to compare with.
145: *
146: * @return True if this and obj are the same; false otherwise
147: */
148: public boolean equals(Object obj) {
149:
150: return ((obj != null) && (getClass() == obj.getClass()) && (value == ((Enumerated) obj).value));
151: }
152:
153: /**
154: * Returns the hash code for this enumerated.
155: *
156: * @return A hash code value for this object.
157: */
158: public int hashCode() {
159: String hashString = getClass().getName()
160: + String.valueOf(value);
161: return hashString.hashCode();
162: }
163:
164: /**
165: * Returns the string form of this enumerated.
166: *
167: * @return The string for for this object.
168: */
169:
170: public String toString() {
171: return (String) getIntTable().get(new Integer(value));
172: }
173:
174: /**
175: * Returns the hashtable of the integer forms.
176: * getIntTable().get(x) returns the string form associated
177: * to the integer x.
178: *
179: * This method must be implemented by the derived class.
180: *
181: * @return An hashtable for read-only purpose
182: */
183:
184: protected abstract Hashtable getIntTable();
185:
186: /**
187: * Returns the hashtable of the string forms.
188: * getStringTable().get(s) returns the integer form associated
189: * to the string s.
190: *
191: * This method must be implemented by the derived class.
192: *
193: * @return An hashtable for read-only purpose
194: */
195:
196: protected abstract Hashtable getStringTable();
197:
198: /**
199: * This variable keeps the integer form of the enumerated.
200: * The string form is retreived using getIntTable().
201: */
202: protected int value;
203:
204: }
|