01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package javax.print.attribute;
19:
20: import java.io.InvalidObjectException;
21: import java.io.ObjectStreamException;
22: import java.io.Serializable;
23:
24: public abstract class EnumSyntax implements Cloneable, Serializable {
25: private static final long serialVersionUID = -2739521845085831642L;
26:
27: private final int value;
28:
29: protected EnumSyntax(int intValue) {
30: super ();
31: value = intValue;
32: }
33:
34: protected EnumSyntax[] getEnumValueTable() {
35: return null;
36: }
37:
38: protected int getOffset() {
39: return 0;
40: }
41:
42: protected String[] getStringTable() {
43: return null;
44: }
45:
46: public int getValue() {
47: return value;
48: }
49:
50: @Override
51: public int hashCode() {
52: return value;
53: }
54:
55: @Override
56: public Object clone() {
57: return this ;
58: }
59:
60: @Override
61: public String toString() {
62: int i = value - getOffset();
63: String[] stringTable = getStringTable();
64: if ((stringTable == null) || (i < 0)
65: || (i > stringTable.length - 1)) {
66: //No string value corresponding to enumeration value
67: return Integer.toString(value);
68: }
69: return stringTable[i];
70: }
71:
72: protected Object readResolve() throws ObjectStreamException {
73: int offset = getOffset();
74: int i = value - offset;
75: EnumSyntax[] enumTable = getEnumValueTable();
76: if (enumTable == null) {
77: throw new InvalidObjectException(
78: "Null enumeration value table");
79: }
80: if ((i < 0) || (i > enumTable.length - 1)) {
81: throw new InvalidObjectException("Value = " + value
82: + " is not in valid range (" + offset + ","
83: + (offset + enumTable.length - 1) + ")");
84: }
85: EnumSyntax outcome = enumTable[i];
86: if (outcome == null) {
87: throw new InvalidObjectException("Null enumeration value");
88: }
89: return outcome;
90: }
91: }
|