Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / print / attribute / EnumSyntax.java
blob9a5e62d458f894ef12d5226d9dcd2b906b9dc5b2
1 /* EnumSyntax.java --
2 Copyright (C) 2003, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.print.attribute;
40 import java.io.InvalidObjectException;
41 import java.io.ObjectStreamException;
42 import java.io.Serializable;
44 /**
45 * <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);
72 * protected PrinterState(int value)
73 * {
74 * super(value);
75 * }
77 * // Overridden because values start not at zero !
78 * protected int getOffset()
79 * {
80 * return 1;
81 * }
83 * private static final String[] stringTable = { "idle", "processing",
84 * "stopped" };
86 * protected String[] getStringTable()
87 * {
88 * return stringTable;
89 * }
91 * private static final PrinterState[] enumValueTable = { IDLE,
92 * PROCESSING, STOPPED};
94 * protected EnumSyntax[] getEnumValueTable()
95 * {
96 * return enumValueTable;
97 * }
98 * }
99 * </pre>
101 * @author Michael Koch (konqueror@gmx.de)
102 * @author Wolfgang Baer (WBaer@gmx.de)
104 public abstract class EnumSyntax implements Cloneable, Serializable
106 private static final long serialVersionUID = -2739521845085831642L;
108 private int value;
111 * Creates a <code>EnumSyntax</code> object.
113 * @param value the value to set.
115 protected EnumSyntax(int value)
117 this.value = value;
121 * Returns the value of this enumeration object.
123 * @return The value.
125 public int getValue()
127 return value;
131 * Clones this object.
133 * @return A clone of this object.
135 public Object clone()
139 return super.clone();
141 catch (CloneNotSupportedException e)
143 // Cannot happen as we implement java.lang.Cloneable.
144 return null;
149 * Returns the hashcode for this object.
150 * The hashcode is the value of this enumeration object.
152 * @return The hashcode.
154 public int hashCode()
156 return value;
160 * 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.
165 * @return The string representation.
167 public String toString()
169 int index = value - getOffset();
170 String[] table = getStringTable();
172 if (table != null
173 && index >= 0
174 && index < table.length)
175 return table[index];
177 return "" + value;
181 * Returns a table with the enumeration values represented as strings
182 * for this object.
184 * The default implementation just returns null. Subclasses should
185 * override this method.
187 * @return The enumeration values as strings.
189 protected String[] getStringTable()
191 return null;
195 * Needed for singelton semantics during deserialisation.
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.
201 * @return The Object at index <code>value - getOffset()</code>
202 * in getEnumValueTable.
203 * @throws ObjectStreamException if getEnumValueTable() returns null.
205 protected Object readResolve() throws ObjectStreamException
207 EnumSyntax[] table = getEnumValueTable();
208 if (table == null)
209 throw new InvalidObjectException("Null enumeration value table "
210 + "for class "
211 + this.getClass().toString());
213 return table[value - getOffset()];
217 * Returns a table with the enumeration values for this object.
219 * The default implementation just returns null. Subclasses have to
220 * to override this method for serialization.
222 * @return The enumeration values.
224 protected EnumSyntax[] getEnumValueTable()
226 return null;
230 * Returns the lowest used value by the enumerations of this class.
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.
236 * @return The lowest used value used.
238 protected int getOffset()
240 return 0;