KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > Enumerated


1 /*
2  * @(#)file Enumerated.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 1.20
5  * @(#)lastedit 03/12/19
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  */

10
11 package com.sun.jmx.snmp;
12
13
14 import java.io.*;
15 import java.util.Hashtable JavaDoc;
16 import java.util.*;
17
18
19
20 /** This class is used for implementing enumerated values.
21  *
22  * An enumeration is represented by a class derived from Enumerated.
23  * The derived class defines what are the permitted values in the enumeration.
24  *
25  * An enumerated value is represented by an instance of the derived class.
26  * It can be represented :
27  * - as an integer
28  * - as a string
29  *
30  * <p><b>This API is a Sun Microsystems internal API and is subject
31  * to change without notice.</b></p>
32  * @version 3.1 09/29/98
33  * @author Sun Microsystems, Inc
34  */

35
36 abstract public class Enumerated implements Serializable {
37
38   /**
39    * Construct an enumerated with a default value.
40    * The default value is the first available in getIntTable().
41     * @exception IllegalArgumentException One of the arguments passed to the method is illegal or inappropriate.
42    */

43   public Enumerated() throws IllegalArgumentException JavaDoc {
44     Enumeration e =getIntTable().keys() ;
45     if (e.hasMoreElements()) {
46       value = ((Integer JavaDoc)e.nextElement()).intValue() ;
47     }
48     else {
49       throw new IllegalArgumentException JavaDoc() ;
50     }
51   }
52  
53   /**
54    * Construct an enumerated from its integer form.
55    *
56    * @param valueIndex The integer form.
57    * @exception IllegalArgumentException One of the arguments passed to
58    * the method is illegal or inappropriate.
59    */

60   public Enumerated(int valueIndex) throws IllegalArgumentException JavaDoc {
61     if (getIntTable().get(new Integer JavaDoc(valueIndex)) == null) {
62       throw new IllegalArgumentException JavaDoc() ;
63     }
64     value = valueIndex ;
65   }
66  
67   /**
68    * Construct an enumerated from its Integer form.
69    *
70    * @param valueIndex The Integer form.
71    * @exception IllegalArgumentException One of the arguments passed to
72    * the method is illegal or inappropriate.
73    */

74   public Enumerated(Integer JavaDoc valueIndex) throws IllegalArgumentException JavaDoc {
75     if (getIntTable().get(valueIndex) == null) {
76       throw new IllegalArgumentException JavaDoc() ;
77     }
78     value = valueIndex.intValue() ;
79   }
80  
81  
82   /**
83    * Construct an enumerated from its string form.
84    *
85    * @param valueString The string form.
86    * @exception IllegalArgumentException One of the arguments passed
87    * to the method is illegal or inappropriate.
88    */

89   public Enumerated(String JavaDoc valueString) throws IllegalArgumentException JavaDoc {
90     Integer JavaDoc index = (Integer JavaDoc)getStringTable().get(valueString) ;
91     if (index == null) {
92       throw new IllegalArgumentException JavaDoc() ;
93     }
94     else {
95       value = index.intValue() ;
96     }
97   }
98
99
100   /**
101    * Return the integer form of the enumerated.
102    *
103    * @return The integer form
104    */

105
106   public int intValue() {
107     return value ;
108   }
109
110
111   /**
112    * Returns an Java enumeration of the permitted integers.
113    *
114    * @return An enumeration of Integer instances
115    */

116
117   public Enumeration valueIndexes() {
118     return getIntTable().keys() ;
119   }
120   
121   
122   /**
123    * Returns an Java enumeration of the permitted strings.
124    *
125    * @return An enumeration of String instances
126    */

127
128   public Enumeration valueStrings() {
129     return getStringTable().keys() ;
130   }
131
132
133   /**
134    * Compares this enumerated to the specified enumerated.
135    *
136    * The result is true if and only if the argument is not null
137    * and is of the same class.
138    *
139    * @param obj The object to compare with.
140    *
141    * @return True if this and obj are the same; false otherwise
142    */

143   public boolean equals(Object JavaDoc obj) {
144     
145     return ((obj != null) &&
146             (getClass() == obj.getClass()) &&
147             (value == ((Enumerated)obj).value)) ;
148   }
149  
150  
151   /**
152    * Returns the hash code for this enumerated.
153    *
154    * @return A hash code value for this object.
155    */

156   public int hashCode() {
157     String JavaDoc hashString = getClass().getName() + String.valueOf(value) ;
158     return hashString.hashCode() ;
159   }
160
161
162   /**
163    * Returns the string form of this enumerated.
164    *
165    * @return The string for for this object.
166    */

167
168   public String JavaDoc toString() {
169     return (String JavaDoc)getIntTable().get(new Integer JavaDoc(value)) ;
170   }
171
172
173   /**
174    * Returns the hashtable of the integer forms.
175    * getIntTable().get(x) returns the string form associated
176    * to the integer x.
177    *
178    * This method must be implemented by the derived class.
179    *
180    * @return An hashtable for read-only purpose
181    */

182
183   protected abstract Hashtable JavaDoc getIntTable() ;
184   
185   
186   
187   /**
188    * Returns the hashtable of the string forms.
189    * getStringTable().get(s) returns the integer form associated
190    * to the string s.
191    *
192    * This method must be implemented by the derived class.
193    *
194    * @return An hashtable for read-only purpose
195    */

196
197   protected abstract Hashtable JavaDoc getStringTable() ;
198  
199  
200   /**
201    * This variable keeps the integer form of the enumerated.
202    * The string form is retreived using getIntTable().
203    */

204   protected int value ;
205   
206 }
207
Popular Tags