001 /* EnumSyntax.java -- 002 Copyright (C) 2003, 2005 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 package javax.print.attribute; 039 040 import java.io.InvalidObjectException; 041 import java.io.ObjectStreamException; 042 import java.io.Serializable; 043 044 /** 045 * <code>EnumSyntax</code> is the abstract base class of all enumeration 046 * classes in the Java Print Service API. 047 * <p> 048 * Every enumeration class which extends from EnumSyntax provides several 049 * enumeration objects as singletons of its class. 050 * </p> 051 * <p> 052 * Notes for implementing subclasses: 053 * <ul> 054 * <li> 055 * The values of all enumeration singelton instances have to be in a 056 * sequence which may start at any value. See: {@link #getOffset()} 057 * </li> 058 * <li> 059 * Subclasses have to override {@link #getEnumValueTable()} and should 060 * override {@link #getStringTable()} for correct serialization. 061 * </li> 062 * </ul> 063 * </p> 064 * Example: 065 * <pre> 066 * public class PrinterState extends EnumSyntax 067 * { 068 * public static final PrinterState IDLE = new PrinterState(1); 069 * public static final PrinterState PROCESSING = new PrinterState(2); 070 * public static final PrinterState STOPPED = new PrinterState(3); 071 * 072 * protected PrinterState(int value) 073 * { 074 * super(value); 075 * } 076 * 077 * // Overridden because values start not at zero ! 078 * protected int getOffset() 079 * { 080 * return 1; 081 * } 082 * 083 * private static final String[] stringTable = { "idle", "processing", 084 * "stopped" }; 085 * 086 * protected String[] getStringTable() 087 * { 088 * return stringTable; 089 * } 090 * 091 * private static final PrinterState[] enumValueTable = { IDLE, 092 * PROCESSING, STOPPED}; 093 * 094 * protected EnumSyntax[] getEnumValueTable() 095 * { 096 * return enumValueTable; 097 * } 098 * } 099 * </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 105 { 106 private static final long serialVersionUID = -2739521845085831642L; 107 108 private int value; 109 110 /** 111 * Creates a <code>EnumSyntax</code> object. 112 * 113 * @param value the value to set. 114 */ 115 protected EnumSyntax(int value) 116 { 117 this.value = value; 118 } 119 120 /** 121 * Returns the value of this enumeration object. 122 * 123 * @return The value. 124 */ 125 public int getValue() 126 { 127 return value; 128 } 129 130 /** 131 * Clones this object. 132 * 133 * @return A clone of this object. 134 */ 135 public Object clone() 136 { 137 try 138 { 139 return super.clone(); 140 } 141 catch (CloneNotSupportedException e) 142 { 143 // Cannot happen as we implement java.lang.Cloneable. 144 return null; 145 } 146 } 147 148 /** 149 * Returns the hashcode for this object. 150 * The hashcode is the value of this enumeration object. 151 * 152 * @return The hashcode. 153 */ 154 public int hashCode() 155 { 156 return value; 157 } 158 159 /** 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. 164 * 165 * @return The string representation. 166 */ 167 public String toString() 168 { 169 int index = value - getOffset(); 170 String[] table = getStringTable(); 171 172 if (table != null 173 && index >= 0 174 && index < table.length) 175 return table[index]; 176 177 return "" + value; 178 } 179 180 /** 181 * Returns a table with the enumeration values represented as strings 182 * for this object. 183 * 184 * The default implementation just returns null. Subclasses should 185 * override this method. 186 * 187 * @return The enumeration values as strings. 188 */ 189 protected String[] getStringTable() 190 { 191 return null; 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. 218 * 219 * The default implementation just returns null. Subclasses have to 220 * to override this method for serialization. 221 * 222 * @return The enumeration values. 223 */ 224 protected EnumSyntax[] getEnumValueTable() 225 { 226 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() 239 { 240 return 0; 241 } 242 }