Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/javax/print/attribute/EnumSyntax.java
41159 views
1
/*
2
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.print.attribute;
27
28
import java.io.InvalidObjectException;
29
import java.io.ObjectStreamException;
30
import java.io.Serial;
31
import java.io.Serializable;
32
33
/**
34
* Class {@code EnumSyntax} is an abstract base class providing the common
35
* implementation of all "type safe enumeration" objects. An enumeration class
36
* (which extends class {@code EnumSyntax}) provides a group of enumeration
37
* values (objects) that are singleton instances of the enumeration class; for
38
* example:
39
*
40
* <pre>
41
* public class Bach extends EnumSyntax {
42
* public static final Bach JOHANN_SEBASTIAN = new Bach(0);
43
* public static final Bach WILHELM_FRIEDEMANN = new Bach(1);
44
* public static final Bach CARL_PHILIP_EMMANUEL = new Bach(2);
45
* public static final Bach JOHANN_CHRISTIAN = new Bach(3);
46
* public static final Bach P_D_Q = new Bach(4);
47
*
48
* private static final String[] stringTable = {
49
* "Johann Sebastian Bach",
50
* "Wilhelm Friedemann Bach",
51
* "Carl Philip Emmanuel Bach",
52
* "Johann Christian Bach",
53
* "P.D.Q. Bach"
54
* };
55
*
56
* protected String[] getStringTable() {
57
* return stringTable;
58
* }
59
*
60
* private static final Bach[] enumValueTable = {
61
* JOHANN_SEBASTIAN,
62
* WILHELM_FRIEDEMANN,
63
* CARL_PHILIP_EMMANUEL,
64
* JOHANN_CHRISTIAN,
65
* P_D_Q
66
* };
67
*
68
* protected EnumSyntax[] getEnumValueTable() {
69
* return enumValueTable;
70
* }
71
* }
72
* </pre>
73
* You can then write code that uses the {@code ==} and {@code !=} operators to
74
* test enumeration values; for example:
75
* <pre>
76
* Bach theComposer;
77
* . . .
78
* if (theComposer == Bach.JOHANN_SEBASTIAN) {
79
* System.out.println ("The greatest composer of all time!");
80
* }
81
* </pre>
82
* The {@code equals()} method for an enumeration class just does a test for
83
* identical objects ({@code ==}).
84
* <p>
85
* You can convert an enumeration value to a string by calling
86
* {@link #toString() toString()}. The string is obtained from a table supplied
87
* by the enumeration class.
88
* <p>
89
* Under the hood, an enumeration value is just an integer, a different integer
90
* for each enumeration value within an enumeration class. You can get an
91
* enumeration value's integer value by calling {@link #getValue() getValue()}.
92
* An enumeration value's integer value is established when it is constructed
93
* (see {@link #EnumSyntax(int) EnumSyntax(int)}). Since the constructor is
94
* protected, the only possible enumeration values are the singleton objects
95
* declared in the enumeration class; additional enumeration values cannot be
96
* created at run time.
97
* <p>
98
* You can define a subclass of an enumeration class that extends it with
99
* additional enumeration values. The subclass's enumeration values' integer
100
* values need not be distinct from the superclass's enumeration values' integer
101
* values; the {@code ==}, {@code !=}, {@code equals()}, and {@code toString()}
102
* methods will still work properly even if the subclass uses some of the same
103
* integer values as the superclass. However, the application in which the
104
* enumeration class and subclass are used may need to have distinct integer
105
* values in the superclass and subclass.
106
*
107
* @author David Mendenhall
108
* @author Alan Kaminsky
109
*/
110
public abstract class EnumSyntax implements Serializable, Cloneable {
111
112
/**
113
* Use serialVersionUID from JDK 1.4 for interoperability.
114
*/
115
@Serial
116
private static final long serialVersionUID = -2739521845085831642L;
117
118
/**
119
* This enumeration value's integer value.
120
*
121
* @serial
122
*/
123
private int value;
124
125
/**
126
* Construct a new enumeration value with the given integer value.
127
*
128
* @param value Integer value
129
*/
130
protected EnumSyntax(int value) {
131
this.value = value;
132
}
133
134
/**
135
* Returns this enumeration value's integer value.
136
*
137
* @return the value
138
*/
139
public int getValue() {
140
return value;
141
}
142
143
/**
144
* Returns a clone of this enumeration value, which to preserve the
145
* semantics of enumeration values is the same object as this enumeration
146
* value.
147
*/
148
public Object clone() {
149
return this;
150
}
151
152
/**
153
* Returns a hash code value for this enumeration value. The hash code is
154
* just this enumeration value's integer value.
155
*/
156
public int hashCode() {
157
return value;
158
}
159
160
/**
161
* Returns a string value corresponding to this enumeration value.
162
*/
163
public String toString() {
164
165
String[] theTable = getStringTable();
166
int theIndex = value - getOffset();
167
return
168
theTable != null && theIndex >= 0 && theIndex < theTable.length ?
169
theTable[theIndex] :
170
Integer.toString (value);
171
}
172
173
/**
174
* During object input, convert this deserialized enumeration instance to
175
* the proper enumeration value defined in the enumeration attribute class.
176
*
177
* @return The enumeration singleton value stored at index <i>i</i>-<i>L</i>
178
* in the enumeration value table returned by
179
* {@link #getEnumValueTable() getEnumValueTable()}, where <i>i</i>
180
* is this enumeration value's integer value and <i>L</i> is the
181
* value returned by {@link #getOffset() getOffset()}
182
* @throws ObjectStreamException if the stream can't be deserialised
183
* @throws InvalidObjectException if the enumeration value table is
184
* {@code null}, this enumeration value's integer value does not
185
* correspond to an element in the enumeration value table, or the
186
* corresponding element in the enumeration value table is
187
* {@code null}. (Note:
188
* {@link InvalidObjectException InvalidObjectException} is a
189
* subclass of {@link ObjectStreamException ObjectStreamException},
190
* which {@code readResolve()} is declared to throw.)
191
*/
192
@Serial
193
protected Object readResolve() throws ObjectStreamException {
194
195
EnumSyntax[] theTable = getEnumValueTable();
196
197
if (theTable == null) {
198
throw new InvalidObjectException(
199
"Null enumeration value table for class " +
200
getClass());
201
}
202
203
int theOffset = getOffset();
204
int theIndex = value - theOffset;
205
206
if (0 > theIndex || theIndex >= theTable.length) {
207
throw new InvalidObjectException
208
("Integer value = " + value + " not in valid range " +
209
theOffset + ".." + (theOffset + theTable.length - 1) +
210
"for class " + getClass());
211
}
212
213
EnumSyntax result = theTable[theIndex];
214
if (result == null) {
215
throw new InvalidObjectException
216
("No enumeration value for integer value = " +
217
value + "for class " + getClass());
218
}
219
return result;
220
}
221
222
// Hidden operations to be implemented in a subclass.
223
224
/**
225
* Returns the string table for this enumeration value's enumeration class.
226
* The enumeration class's integer values are assumed to lie in the range
227
* <i>L</i>..<i>L</i>+<i>N</i>-1, where <i>L</i> is the value returned by
228
* {@link #getOffset() getOffset()} and <i>N</i> is the length of the string
229
* table. The element in the string table at index <i>i</i>-<i>L</i> is the
230
* value returned by {@link #toString() toString()} for the enumeration
231
* value whose integer value is <i>i</i>. If an integer within the above
232
* range is not used by any enumeration value, leave the corresponding table
233
* element {@code null}.
234
* <p>
235
* The default implementation returns {@code null}. If the enumeration class
236
* (a subclass of class {@code EnumSyntax}) does not override this method to
237
* return a {@code non-null} string table, and the subclass does not
238
* override the {@link #toString() toString()} method, the base class
239
* {@link #toString() toString()} method will return just a string
240
* representation of this enumeration value's integer value.
241
*
242
* @return the string table
243
*/
244
protected String[] getStringTable() {
245
return null;
246
}
247
248
/**
249
* Returns the enumeration value table for this enumeration value's
250
* enumeration class. The enumeration class's integer values are assumed to
251
* lie in the range <i>L</i>..<i>L</i>+<i>N</i>-1, where <i>L</i> is the
252
* value returned by {@link #getOffset() getOffset()} and <i>N</i> is the
253
* length of the enumeration value table. The element in the enumeration
254
* value table at index <i>i</i>-<i>L</i> is the enumeration value object
255
* whose integer value is <i>i</i>; the {@link #readResolve() readResolve()}
256
* method needs this to preserve singleton semantics during deserialization
257
* of an enumeration instance. If an integer within the above range is not
258
* used by any enumeration value, leave the corresponding table element
259
* {@code null}.
260
* <p>
261
* The default implementation returns {@code null}. If the enumeration class
262
* (a subclass of class EnumSyntax) does not override this method to return
263
* a {@code non-null} enumeration value table, and the subclass does not
264
* override the {@link #readResolve() readResolve()} method, the base class
265
* {@link #readResolve() readResolve()} method will throw an exception
266
* whenever an enumeration instance is deserialized from an object input
267
* stream.
268
*
269
* @return the value table
270
*/
271
protected EnumSyntax[] getEnumValueTable() {
272
return null;
273
}
274
275
/**
276
* Returns the lowest integer value used by this enumeration value's
277
* enumeration class.
278
* <p>
279
* The default implementation returns 0. If the enumeration class (a
280
* subclass of class {@code EnumSyntax}) uses integer values starting at
281
* other than 0, override this method in the subclass.
282
*
283
* @return the offset of the lowest enumeration value
284
*/
285
protected int getOffset() {
286
return 0;
287
}
288
}
289
290