51 * "P.D.Q. Bach"
52 * };
53 *
54 * protected String[] getStringTable() {
55 * return stringTable;
56 * }
57 *
58 * private static final Bach[] enumValueTable = {
59 * JOHANN_SEBASTIAN,
60 * WILHELM_FRIEDEMANN,
61 * CARL_PHILIP_EMMANUEL,
62 * JOHANN_CHRISTIAN,
63 * P_D_Q
64 * };
65 *
66 * protected EnumSyntax[] getEnumValueTable() {
67 * return enumValueTable;
68 * }
69 * }
70 * </PRE>
71 * You can then write code that uses the <CODE>==</CODE> and <CODE>!=</CODE>
72 * operators to test enumeration values; for example:
73 * <PRE>
74 * Bach theComposer;
75 * . . .
76 * if (theComposer == Bach.JOHANN_SEBASTIAN) {
77 * System.out.println ("The greatest composer of all time!");
78 * }
79 * </PRE>
80 * The <CODE>equals()</CODE> method for an enumeration class just does a test
81 * for identical objects (<CODE>==</CODE>).
82 * <P>
83 * You can convert an enumeration value to a string by calling {@link
84 * #toString() toString()}. The string is obtained from a table
85 * supplied by the enumeration class.
86 * <P>
87 * Under the hood, an enumeration value is just an integer, a different integer
88 * for each enumeration value within an enumeration class. You can get an
89 * enumeration value's integer value by calling {@link #getValue()
90 * getValue()}. An enumeration value's integer value is established
91 * when it is constructed (see {@link #EnumSyntax(int)
92 * EnumSyntax(int)}). Since the constructor is protected, the only
93 * possible enumeration values are the singleton objects declared in the
94 * enumeration class; additional enumeration values cannot be created at run
95 * time.
96 * <P>
97 * You can define a subclass of an enumeration class that extends it with
98 * additional enumeration values. The subclass's enumeration values' integer
99 * values need not be distinct from the superclass's enumeration values' integer
100 * values; the <CODE>==</CODE>, <CODE>!=</CODE>, <CODE>equals()</CODE>, and
101 * <CODE>toString()</CODE> methods will still work properly even if the subclass
102 * uses some of the same integer values as the superclass. However, the
103 * application in which the enumeration class and subclass are used may need to
104 * have distinct integer values in the superclass and subclass.
105 *
106 * @author David Mendenhall
107 * @author Alan Kaminsky
108 */
109 public abstract class EnumSyntax implements Serializable, Cloneable {
110
111 private static final long serialVersionUID = -2739521845085831642L;
112
113 /**
114 * This enumeration value's integer value.
115 * @serial
116 */
117 private int value;
118
119 /**
120 * Construct a new enumeration value with the given integer value.
121 *
165
166 /**
167 * During object input, convert this deserialized enumeration instance to
168 * the proper enumeration value defined in the enumeration attribute class.
169 *
170 * @return The enumeration singleton value stored at index
171 * <I>i</I>-<I>L</I> in the enumeration value table returned by
172 * {@link #getEnumValueTable() getEnumValueTable()},
173 * where <I>i</I> is this enumeration value's integer value and
174 * <I>L</I> is the value returned by {@link #getOffset()
175 * getOffset()}.
176 *
177 * @throws ObjectStreamException if the stream can't be deserialised
178 * @throws InvalidObjectException
179 * Thrown if the enumeration value table is null, this enumeration
180 * value's integer value does not correspond to an element in the
181 * enumeration value table, or the corresponding element in the
182 * enumeration value table is null. (Note: {@link
183 * java.io.InvalidObjectException InvalidObjectException} is a subclass
184 * of {@link java.io.ObjectStreamException ObjectStreamException}, which
185 * <CODE>readResolve()</CODE> is declared to throw.)
186 */
187 protected Object readResolve() throws ObjectStreamException {
188
189 EnumSyntax[] theTable = getEnumValueTable();
190
191 if (theTable == null) {
192 throw new InvalidObjectException(
193 "Null enumeration value table for class " +
194 getClass());
195 }
196
197 int theOffset = getOffset();
198 int theIndex = value - theOffset;
199
200 if (0 > theIndex || theIndex >= theTable.length) {
201 throw new InvalidObjectException
202 ("Integer value = " + value + " not in valid range " +
203 theOffset + ".." + (theOffset + theTable.length - 1) +
204 "for class " + getClass());
205 }
|
51 * "P.D.Q. Bach"
52 * };
53 *
54 * protected String[] getStringTable() {
55 * return stringTable;
56 * }
57 *
58 * private static final Bach[] enumValueTable = {
59 * JOHANN_SEBASTIAN,
60 * WILHELM_FRIEDEMANN,
61 * CARL_PHILIP_EMMANUEL,
62 * JOHANN_CHRISTIAN,
63 * P_D_Q
64 * };
65 *
66 * protected EnumSyntax[] getEnumValueTable() {
67 * return enumValueTable;
68 * }
69 * }
70 * </PRE>
71 * You can then write code that uses the {@code ==} and {@code !=}
72 * operators to test enumeration values; for example:
73 * <PRE>
74 * Bach theComposer;
75 * . . .
76 * if (theComposer == Bach.JOHANN_SEBASTIAN) {
77 * System.out.println ("The greatest composer of all time!");
78 * }
79 * </PRE>
80 * The {@code equals()} method for an enumeration class just does a test
81 * for identical objects ({@code ==}).
82 * <P>
83 * You can convert an enumeration value to a string by calling {@link
84 * #toString() toString()}. The string is obtained from a table
85 * supplied by the enumeration class.
86 * <P>
87 * Under the hood, an enumeration value is just an integer, a different integer
88 * for each enumeration value within an enumeration class. You can get an
89 * enumeration value's integer value by calling {@link #getValue()
90 * getValue()}. An enumeration value's integer value is established
91 * when it is constructed (see {@link #EnumSyntax(int)
92 * EnumSyntax(int)}). Since the constructor is protected, the only
93 * possible enumeration values are the singleton objects declared in the
94 * enumeration class; additional enumeration values cannot be created at run
95 * time.
96 * <P>
97 * You can define a subclass of an enumeration class that extends it with
98 * additional enumeration values. The subclass's enumeration values' integer
99 * values need not be distinct from the superclass's enumeration values' integer
100 * values; the {@code ==}, {@code !=}, {@code equals()}, and
101 * {@code toString()} methods will still work properly even if the subclass
102 * uses some of the same integer values as the superclass. However, the
103 * application in which the enumeration class and subclass are used may need to
104 * have distinct integer values in the superclass and subclass.
105 *
106 * @author David Mendenhall
107 * @author Alan Kaminsky
108 */
109 public abstract class EnumSyntax implements Serializable, Cloneable {
110
111 private static final long serialVersionUID = -2739521845085831642L;
112
113 /**
114 * This enumeration value's integer value.
115 * @serial
116 */
117 private int value;
118
119 /**
120 * Construct a new enumeration value with the given integer value.
121 *
165
166 /**
167 * During object input, convert this deserialized enumeration instance to
168 * the proper enumeration value defined in the enumeration attribute class.
169 *
170 * @return The enumeration singleton value stored at index
171 * <I>i</I>-<I>L</I> in the enumeration value table returned by
172 * {@link #getEnumValueTable() getEnumValueTable()},
173 * where <I>i</I> is this enumeration value's integer value and
174 * <I>L</I> is the value returned by {@link #getOffset()
175 * getOffset()}.
176 *
177 * @throws ObjectStreamException if the stream can't be deserialised
178 * @throws InvalidObjectException
179 * Thrown if the enumeration value table is null, this enumeration
180 * value's integer value does not correspond to an element in the
181 * enumeration value table, or the corresponding element in the
182 * enumeration value table is null. (Note: {@link
183 * java.io.InvalidObjectException InvalidObjectException} is a subclass
184 * of {@link java.io.ObjectStreamException ObjectStreamException}, which
185 * {@code readResolve()} is declared to throw.)
186 */
187 protected Object readResolve() throws ObjectStreamException {
188
189 EnumSyntax[] theTable = getEnumValueTable();
190
191 if (theTable == null) {
192 throw new InvalidObjectException(
193 "Null enumeration value table for class " +
194 getClass());
195 }
196
197 int theOffset = getOffset();
198 int theIndex = value - theOffset;
199
200 if (0 > theIndex || theIndex >= theTable.length) {
201 throw new InvalidObjectException
202 ("Integer value = " + value + " not in valid range " +
203 theOffset + ".." + (theOffset + theTable.length - 1) +
204 "for class " + getClass());
205 }
|