Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/print/attribute/EnumSyntax.java
38918 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package javax.print.attribute;2728import java.io.InvalidObjectException;29import java.io.ObjectStreamException;30import java.io.Serializable;3132/**33* Class EnumSyntax is an abstract base class providing the common34* implementation of all "type safe enumeration" objects. An enumeration class35* (which extends class EnumSyntax) provides a group of enumeration values36* (objects) that are singleton instances of the enumeration class; for example:37* <PRE>38* public class Bach extends EnumSyntax {39* public static final Bach JOHANN_SEBASTIAN = new Bach(0);40* public static final Bach WILHELM_FRIEDEMANN = new Bach(1);41* public static final Bach CARL_PHILIP_EMMANUEL = new Bach(2);42* public static final Bach JOHANN_CHRISTIAN = new Bach(3);43* public static final Bach P_D_Q = new Bach(4);44*45* private static final String[] stringTable = {46* "Johann Sebastian Bach",47* "Wilhelm Friedemann Bach",48* "Carl Philip Emmanuel Bach",49* "Johann Christian Bach",50* "P.D.Q. Bach"51* };52*53* protected String[] getStringTable() {54* return stringTable;55* }56*57* private static final Bach[] enumValueTable = {58* JOHANN_SEBASTIAN,59* WILHELM_FRIEDEMANN,60* CARL_PHILIP_EMMANUEL,61* JOHANN_CHRISTIAN,62* P_D_Q63* };64*65* protected EnumSyntax[] getEnumValueTable() {66* return enumValueTable;67* }68* }69* </PRE>70* You can then write code that uses the <CODE>==</CODE> and <CODE>!=</CODE>71* operators to test enumeration values; for example:72* <PRE>73* Bach theComposer;74* . . .75* if (theComposer == Bach.JOHANN_SEBASTIAN) {76* System.out.println ("The greatest composer of all time!");77* }78* </PRE>79* The <CODE>equals()</CODE> method for an enumeration class just does a test80* for identical objects (<CODE>==</CODE>).81* <P>82* You can convert an enumeration value to a string by calling {@link83* #toString() toString()}. The string is obtained from a table84* supplied by the enumeration class.85* <P>86* Under the hood, an enumeration value is just an integer, a different integer87* for each enumeration value within an enumeration class. You can get an88* enumeration value's integer value by calling {@link #getValue()89* getValue()}. An enumeration value's integer value is established90* when it is constructed (see {@link #EnumSyntax(int)91* EnumSyntax(int)}). Since the constructor is protected, the only92* possible enumeration values are the singleton objects declared in the93* enumeration class; additional enumeration values cannot be created at run94* time.95* <P>96* You can define a subclass of an enumeration class that extends it with97* additional enumeration values. The subclass's enumeration values' integer98* values need not be distinct from the superclass's enumeration values' integer99* values; the <CODE>==</CODE>, <CODE>!=</CODE>, <CODE>equals()</CODE>, and100* <CODE>toString()</CODE> methods will still work properly even if the subclass101* uses some of the same integer values as the superclass. However, the102* application in which the enumeration class and subclass are used may need to103* have distinct integer values in the superclass and subclass.104* <P>105*106* @author David Mendenhall107* @author Alan Kaminsky108*/109public abstract class EnumSyntax implements Serializable, Cloneable {110111private static final long serialVersionUID = -2739521845085831642L;112113/**114* This enumeration value's integer value.115* @serial116*/117private int value;118119/**120* Construct a new enumeration value with the given integer value.121*122* @param value Integer value.123*/124protected EnumSyntax(int value) {125this.value = value;126}127128/**129* Returns this enumeration value's integer value.130* @return the value131*/132public int getValue() {133return value;134}135136/**137* Returns a clone of this enumeration value, which to preserve the138* semantics of enumeration values is the same object as this enumeration139* value.140*/141public Object clone() {142return this;143}144145/**146* Returns a hash code value for this enumeration value. The hash code is147* just this enumeration value's integer value.148*/149public int hashCode() {150return value;151}152153/**154* Returns a string value corresponding to this enumeration value.155*/156public String toString() {157158String[] theTable = getStringTable();159int theIndex = value - getOffset();160return161theTable != null && theIndex >= 0 && theIndex < theTable.length ?162theTable[theIndex] :163Integer.toString (value);164}165166/**167* During object input, convert this deserialized enumeration instance to168* the proper enumeration value defined in the enumeration attribute class.169*170* @return The enumeration singleton value stored at index171* <I>i</I>-<I>L</I> in the enumeration value table returned by172* {@link #getEnumValueTable() getEnumValueTable()},173* where <I>i</I> is this enumeration value's integer value and174* <I>L</I> is the value returned by {@link #getOffset()175* getOffset()}.176*177* @throws ObjectStreamException if the stream can't be deserialised178* @throws InvalidObjectException179* Thrown if the enumeration value table is null, this enumeration180* value's integer value does not correspond to an element in the181* enumeration value table, or the corresponding element in the182* enumeration value table is null. (Note: {@link183* java.io.InvalidObjectException InvalidObjectException} is a subclass184* of {@link java.io.ObjectStreamException ObjectStreamException}, which185* <CODE>readResolve()</CODE> is declared to throw.)186*/187protected Object readResolve() throws ObjectStreamException {188189EnumSyntax[] theTable = getEnumValueTable();190191if (theTable == null) {192throw new InvalidObjectException(193"Null enumeration value table for class " +194getClass());195}196197int theOffset = getOffset();198int theIndex = value - theOffset;199200if (0 > theIndex || theIndex >= theTable.length) {201throw new InvalidObjectException202("Integer value = " + value + " not in valid range " +203theOffset + ".." + (theOffset + theTable.length - 1) +204"for class " + getClass());205}206207EnumSyntax result = theTable[theIndex];208if (result == null) {209throw new InvalidObjectException210("No enumeration value for integer value = " +211value + "for class " + getClass());212}213return result;214}215216// Hidden operations to be implemented in a subclass.217218/**219* Returns the string table for this enumeration value's enumeration class.220* The enumeration class's integer values are assumed to lie in the range221* <I>L</I>..<I>L</I>+<I>N</I>-1, where <I>L</I> is the value returned by222* {@link #getOffset() getOffset()} and <I>N</I> is the length223* of the string table. The element in the string table at index224* <I>i</I>-<I>L</I> is the value returned by {@link #toString()225* toString()} for the enumeration value whose integer value226* is <I>i</I>. If an integer within the above range is not used by any227* enumeration value, leave the corresponding table element null.228* <P>229* The default implementation returns null. If the enumeration class (a230* subclass of class EnumSyntax) does not override this method to return a231* non-null string table, and the subclass does not override the {@link232* #toString() toString()} method, the base class {@link233* #toString() toString()} method will return just a string234* representation of this enumeration value's integer value.235* @return the string table236*/237protected String[] getStringTable() {238return null;239}240241/**242* Returns the enumeration value table for this enumeration value's243* enumeration class. The enumeration class's integer values are assumed to244* lie in the range <I>L</I>..<I>L</I>+<I>N</I>-1, where <I>L</I> is the245* value returned by {@link #getOffset() getOffset()} and246* <I>N</I> is the length of the enumeration value table. The element in the247* enumeration value table at index <I>i</I>-<I>L</I> is the enumeration248* value object whose integer value is <I>i</I>; the {@link #readResolve()249* readResolve()} method needs this to preserve singleton250* semantics during deserialization of an enumeration instance. If an251* integer within the above range is not used by any enumeration value,252* leave the corresponding table element null.253* <P>254* The default implementation returns null. If the enumeration class (a255* subclass of class EnumSyntax) does not override this method to return256* a non-null enumeration value table, and the subclass does not override257* the {@link #readResolve() readResolve()} method, the base258* class {@link #readResolve() readResolve()} method will throw259* an exception whenever an enumeration instance is deserialized from an260* object input stream.261* @return the value table262*/263protected EnumSyntax[] getEnumValueTable() {264return null;265}266267/**268* Returns the lowest integer value used by this enumeration value's269* enumeration class.270* <P>271* The default implementation returns 0. If the enumeration class (a272* subclass of class EnumSyntax) uses integer values starting at other than273* 0, override this method in the subclass.274* @return the offset of the lowest enumeration value.275*/276protected int getOffset() {277return 0;278}279280}281282283