Source for javax.print.attribute.EnumSyntax

   1: /* EnumSyntax.java -- 
   2:    Copyright (C) 2003, 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: package javax.print.attribute;
  39: 
  40: import java.io.InvalidObjectException;
  41: import java.io.ObjectStreamException;
  42: import java.io.Serializable;
  43: 
  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);
  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
 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: }