1 |
/* EnumSyntax.java -- |
/* EnumSyntax.java -- |
2 |
Copyright (C) 2003 Free Software Foundation, Inc. |
Copyright (C) 2003, 2005 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
37 |
|
|
38 |
package javax.print.attribute; |
package javax.print.attribute; |
39 |
|
|
40 |
|
import java.io.InvalidObjectException; |
41 |
|
import java.io.ObjectStreamException; |
42 |
import java.io.Serializable; |
import java.io.Serializable; |
43 |
|
|
44 |
/** |
/** |
45 |
* @author Michael Koch |
* <code>EnumSyntax</code> is the abstract base class of all enumeration |
46 |
|
* classes in the Java Print Service API. |
47 |
|
* <p> |
48 |
|
* Every enumeration class which extends from EnumSyntax provides several |
49 |
|
* enumeration objects as singletons of its class. |
50 |
|
* </p> |
51 |
|
* <p> |
52 |
|
* Notes for implementing subclasses: |
53 |
|
* <ul> |
54 |
|
* <li> |
55 |
|
* The values of all enumeration singelton instances have to be in a |
56 |
|
* sequence which may start at any value. See: {@link #getOffset()} |
57 |
|
* </li> |
58 |
|
* <li> |
59 |
|
* Subclasses have to override {@link #getEnumValueTable()} and should |
60 |
|
* override {@link #getStringTable()} for correct serialization. |
61 |
|
* </li> |
62 |
|
* </ul> |
63 |
|
* </p> |
64 |
|
* Example: |
65 |
|
* <pre> |
66 |
|
* public class PrinterState extends EnumSyntax |
67 |
|
* { |
68 |
|
* public static final PrinterState IDLE = new PrinterState(1); |
69 |
|
* public static final PrinterState PROCESSING = new PrinterState(2); |
70 |
|
* public static final PrinterState STOPPED = new PrinterState(3); |
71 |
|
* |
72 |
|
* protected PrinterState(int value) |
73 |
|
* { |
74 |
|
* super(value); |
75 |
|
* } |
76 |
|
* |
77 |
|
* // Overridden because values start not at zero ! |
78 |
|
* protected int getOffset() |
79 |
|
* { |
80 |
|
* return 1; |
81 |
|
* } |
82 |
|
* |
83 |
|
* private static final String[] stringTable = { "idle", "processing", |
84 |
|
* "stopped" }; |
85 |
|
* |
86 |
|
* protected String[] getStringTable() |
87 |
|
* { |
88 |
|
* return stringTable; |
89 |
|
* } |
90 |
|
* |
91 |
|
* private static final PrinterState[] enumValueTable = { IDLE, |
92 |
|
* PROCESSING, STOPPED}; |
93 |
|
* |
94 |
|
* protected EnumSyntax[] getEnumValueTable() |
95 |
|
* { |
96 |
|
* return enumValueTable; |
97 |
|
* } |
98 |
|
* } |
99 |
|
* </pre> |
100 |
|
* |
101 |
|
* @author Michael Koch (konqueror@gmx.de) |
102 |
|
* @author Wolfgang Baer (WBaer@gmx.de) |
103 |
*/ |
*/ |
104 |
public abstract class EnumSyntax implements Cloneable, Serializable |
public abstract class EnumSyntax implements Cloneable, Serializable |
105 |
{ |
{ |
110 |
/** |
/** |
111 |
* Creates a <code>EnumSyntax</code> object. |
* Creates a <code>EnumSyntax</code> object. |
112 |
* |
* |
113 |
* @param value the value to set |
* @param value the value to set. |
114 |
*/ |
*/ |
115 |
protected EnumSyntax(int value) |
protected EnumSyntax(int value) |
116 |
{ |
{ |
118 |
} |
} |
119 |
|
|
120 |
/** |
/** |
121 |
* Returns the value of this object. |
* Returns the value of this enumeration object. |
122 |
* |
* |
123 |
* @return the value |
* @return The value. |
124 |
*/ |
*/ |
125 |
public int getValue() |
public int getValue() |
126 |
{ |
{ |
130 |
/** |
/** |
131 |
* Clones this object. |
* Clones this object. |
132 |
* |
* |
133 |
* @return a clone of this object |
* @return A clone of this object. |
134 |
*/ |
*/ |
135 |
public Object clone() |
public Object clone() |
136 |
{ |
{ |
146 |
} |
} |
147 |
|
|
148 |
/** |
/** |
149 |
* Returns the hashcode for this object. |
* Returns the hashcode for this object. |
150 |
|
* The hashcode is the value of this enumeration object. |
151 |
* |
* |
152 |
* @return the hashcode |
* @return The hashcode. |
153 |
*/ |
*/ |
154 |
public int hashCode() |
public int hashCode() |
155 |
{ |
{ |
158 |
|
|
159 |
/** |
/** |
160 |
* Returns the string representation for this object. |
* Returns the string representation for this object. |
161 |
|
* The string value from <code>getStringTable()</code> method is returned |
162 |
|
* if subclasses override this method. Otherwise the value of this object |
163 |
|
* as a string is returned. |
164 |
* |
* |
165 |
* @return the string representation |
* @return The string representation. |
166 |
*/ |
*/ |
167 |
public String toString() |
public String toString() |
168 |
{ |
{ |
181 |
* Returns a table with the enumeration values represented as strings |
* Returns a table with the enumeration values represented as strings |
182 |
* for this object. |
* for this object. |
183 |
* |
* |
184 |
* The default implementation just returns null. |
* The default implementation just returns null. Subclasses should |
185 |
|
* override this method. |
186 |
* |
* |
187 |
* @return the enumeration values as strings |
* @return The enumeration values as strings. |
188 |
*/ |
*/ |
189 |
protected String[] getStringTable() |
protected String[] getStringTable() |
190 |
{ |
{ |
192 |
} |
} |
193 |
|
|
194 |
/** |
/** |
195 |
|
* Needed for singelton semantics during deserialisation. |
196 |
|
* |
197 |
|
* Subclasses must not override this class. Subclasses have to override |
198 |
|
* <code>getEnumValueTable()</code> and should override |
199 |
|
* <code>getStringTable()</code> for correct serialization. |
200 |
|
* |
201 |
|
* @return The Object at index <code>value - getOffset()</code> |
202 |
|
* in getEnumValueTable. |
203 |
|
* @throws ObjectStreamException if getEnumValueTable() returns null. |
204 |
|
*/ |
205 |
|
protected Object readResolve() throws ObjectStreamException |
206 |
|
{ |
207 |
|
EnumSyntax[] table = getEnumValueTable(); |
208 |
|
if (table == null) |
209 |
|
throw new InvalidObjectException("Null enumeration value table " |
210 |
|
+ "for class " |
211 |
|
+ this.getClass().toString()); |
212 |
|
|
213 |
|
return table[value - getOffset()]; |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
* Returns a table with the enumeration values for this object. |
* Returns a table with the enumeration values for this object. |
218 |
* |
* |
219 |
* The default implementation just returns null. |
* The default implementation just returns null. Subclasses have to |
220 |
|
* to override this method for serialization. |
221 |
* |
* |
222 |
* @return the enumeration values |
* @return The enumeration values. |
223 |
*/ |
*/ |
224 |
protected EnumSyntax[] getEnumValueTable() |
protected EnumSyntax[] getEnumValueTable() |
225 |
{ |
{ |
226 |
return null; |
return null; |
227 |
} |
} |
228 |
|
|
229 |
|
/** |
230 |
|
* Returns the lowest used value by the enumerations of this class. |
231 |
|
* |
232 |
|
* The default implementation returns 0. This is enough if enumerations |
233 |
|
* start with a zero value. Otherwise subclasses need to override this |
234 |
|
* method for serialization and return the lowest value they use. |
235 |
|
* . |
236 |
|
* @return The lowest used value used. |
237 |
|
*/ |
238 |
protected int getOffset() |
protected int getOffset() |
239 |
{ |
{ |
240 |
return 0; |
return 0; |