J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2007 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 | |
| 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 e =getIntTable().keys() ; |
| 58 | if (e.hasMoreElements()) { |
| 59 | value = ((Integer)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(new Integer(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 = (Integer)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 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 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 | public boolean equals(Object obj) { |
| 157 | |
| 158 | return ((obj != null) && |
| 159 | (getClass() == obj.getClass()) && |
| 160 | (value == ((Enumerated)obj).value)) ; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * Returns the hash code for this enumerated. |
| 166 | * |
| 167 | * @return A hash code value for this object. |
| 168 | */ |
| 169 | public int hashCode() { |
| 170 | String hashString = getClass().getName() + String.valueOf(value) ; |
| 171 | return hashString.hashCode() ; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /** |
| 176 | * Returns the string form of this enumerated. |
| 177 | * |
| 178 | * @return The string for for this object. |
| 179 | */ |
| 180 | |
| 181 | public String toString() { |
| 182 | return (String)getIntTable().get(new Integer(value)) ; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /** |
| 187 | * Returns the hashtable of the integer forms. |
| 188 | * getIntTable().get(x) returns the string form associated |
| 189 | * to the integer x. |
| 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 getIntTable() ; |
| 197 | |
| 198 | |
| 199 | |
| 200 | /** |
| 201 | * Returns the hashtable of the string forms. |
| 202 | * getStringTable().get(s) returns the integer form associated |
| 203 | * to the string s. |
| 204 | * |
| 205 | * This method must be implemented by the derived class. |
| 206 | * |
| 207 | * @return An hashtable for read-only purpose |
| 208 | */ |
| 209 | |
| 210 | protected abstract Hashtable getStringTable() ; |
| 211 | |
| 212 | |
| 213 | /** |
| 214 | * This variable keeps the integer form of the enumerated. |
| 215 | * The string form is retreived using getIntTable(). |
| 216 | */ |
| 217 | protected int value ; |
| 218 | |
| 219 | } |