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. package com.sun.jmx.snmp;
  11. import java.io.*;
  12. import java.util.Hashtable;
  13. import java.util.*;
  14. /** This class is used for implementing enumerated values.
  15. *
  16. * An enumeration is represented by a class derived from Enumerated.
  17. * The derived class defines what are the permitted values in the enumeration.
  18. *
  19. * An enumerated value is represented by an instance of the derived class.
  20. * It can be represented :
  21. * - as an integer
  22. * - as a string
  23. *
  24. * <p><b>This API is a Sun Microsystems internal API and is subject
  25. * to change without notice.</b></p>
  26. * @version 3.1 09/29/98
  27. * @author Sun Microsystems, Inc
  28. */
  29. abstract public class Enumerated implements Serializable {
  30. /**
  31. * Construct an enumerated with a default value.
  32. * The default value is the first available in getIntTable().
  33. * @exception IllegalArgumentException One of the arguments passed to the method is illegal or inappropriate.
  34. */
  35. public Enumerated() throws IllegalArgumentException {
  36. Enumeration e =getIntTable().keys() ;
  37. if (e.hasMoreElements()) {
  38. value = ((Integer)e.nextElement()).intValue() ;
  39. }
  40. else {
  41. throw new IllegalArgumentException() ;
  42. }
  43. }
  44. /**
  45. * Construct an enumerated from its integer form.
  46. *
  47. * @param valueIndex The integer form.
  48. * @exception IllegalArgumentException One of the arguments passed to
  49. * the method is illegal or inappropriate.
  50. */
  51. public Enumerated(int valueIndex) throws IllegalArgumentException {
  52. if (getIntTable().get(new Integer(valueIndex)) == null) {
  53. throw new IllegalArgumentException() ;
  54. }
  55. value = valueIndex ;
  56. }
  57. /**
  58. * Construct an enumerated from its Integer form.
  59. *
  60. * @param valueIndex The Integer form.
  61. * @exception IllegalArgumentException One of the arguments passed to
  62. * the method is illegal or inappropriate.
  63. */
  64. public Enumerated(Integer valueIndex) throws IllegalArgumentException {
  65. if (getIntTable().get(valueIndex) == null) {
  66. throw new IllegalArgumentException() ;
  67. }
  68. value = valueIndex.intValue() ;
  69. }
  70. /**
  71. * Construct an enumerated from its string form.
  72. *
  73. * @param valueString The string form.
  74. * @exception IllegalArgumentException One of the arguments passed
  75. * to the method is illegal or inappropriate.
  76. */
  77. public Enumerated(String valueString) throws IllegalArgumentException {
  78. Integer index = (Integer)getStringTable().get(valueString) ;
  79. if (index == null) {
  80. throw new IllegalArgumentException() ;
  81. }
  82. else {
  83. value = index.intValue() ;
  84. }
  85. }
  86. /**
  87. * Return the integer form of the enumerated.
  88. *
  89. * @return The integer form
  90. */
  91. public int intValue() {
  92. return value ;
  93. }
  94. /**
  95. * Returns an Java enumeration of the permitted integers.
  96. *
  97. * @return An enumeration of Integer instances
  98. */
  99. public Enumeration valueIndexes() {
  100. return getIntTable().keys() ;
  101. }
  102. /**
  103. * Returns an Java enumeration of the permitted strings.
  104. *
  105. * @return An enumeration of String instances
  106. */
  107. public Enumeration valueStrings() {
  108. return getStringTable().keys() ;
  109. }
  110. /**
  111. * Compares this enumerated to the specified enumerated.
  112. *
  113. * The result is true if and only if the argument is not null
  114. * and is of the same class.
  115. *
  116. * @param obj The object to compare with.
  117. *
  118. * @return True if this and obj are the same; false otherwise
  119. */
  120. public boolean equals(Object obj) {
  121. return ((obj != null) &&
  122. (getClass() == obj.getClass()) &&
  123. (value == ((Enumerated)obj).value)) ;
  124. }
  125. /**
  126. * Returns the hash code for this enumerated.
  127. *
  128. * @return A hash code value for this object.
  129. */
  130. public int hashCode() {
  131. String hashString = getClass().getName() + String.valueOf(value) ;
  132. return hashString.hashCode() ;
  133. }
  134. /**
  135. * Returns the string form of this enumerated.
  136. *
  137. * @return The string for for this object.
  138. */
  139. public String toString() {
  140. return (String)getIntTable().get(new Integer(value)) ;
  141. }
  142. /**
  143. * Returns the hashtable of the integer forms.
  144. * getIntTable().get(x) returns the string form associated
  145. * to the integer x.
  146. *
  147. * This method must be implemented by the derived class.
  148. *
  149. * @return An hashtable for read-only purpose
  150. */
  151. protected abstract Hashtable getIntTable() ;
  152. /**
  153. * Returns the hashtable of the string forms.
  154. * getStringTable().get(s) returns the integer form associated
  155. * to the string s.
  156. *
  157. * This method must be implemented by the derived class.
  158. *
  159. * @return An hashtable for read-only purpose
  160. */
  161. protected abstract Hashtable getStringTable() ;
  162. /**
  163. * This variable keeps the integer form of the enumerated.
  164. * The string form is retreived using getIntTable().
  165. */
  166. protected int value ;
  167. }