Debian Codegraph @

lang/java/src/com/sleepycat/persist/impl/EnumFormat.java

raw
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 */
7
8 package com.sleepycat.persist.impl;
9
10 import java.lang.reflect.Array;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17
18 import com.sleepycat.compat.DbCompat;
19 import com.sleepycat.persist.model.EntityModel;
20 import com.sleepycat.persist.raw.RawObject;
21 import java.util.IdentityHashMap;
22
23 /**
24 * Format for all enum types.
25 *
26 * In this class we resort to using reflection to allocate arrays of enums.
27 * If there is a need for it, reflection could be avoided in the future by
28 * generating code as new array formats are encountered.
29 *
30 * @author Mark Hayes
31 */
32 public class EnumFormat extends Format {
33
34 private static final long serialVersionUID = 1069833955604373538L;
35
36 private String[] names;
37 private transient Object[] values;
38
39 EnumFormat(Catalog catalog, Class type) {
40 super(catalog, type);
41 values = type.getEnumConstants();
42 names = new String[values.length];
43 for (int i = 0; i < names.length; i += 1) {
44 names[i] = ((Enum) values[i]).name();
45 }
46 }
47
48 /**
49 * For use in a deserialized CompositeKeyFormat.
50 */
51 EnumFormat(Catalog catalog, Class type, String[] enumData) {
52 super(catalog, type);
53 names = enumData;
54 }
55
56 /**
57 * Returns data needed for serialization of a CompositeKeyFormat.
58 */
59 String[] getFormatData() {
60 return names;
61 }
62
63 @Override
64 public boolean isEnum() {
65 return true;
66 }
67
68 @Override
69 public List<String> getEnumConstants() {
70 return Arrays.asList(names);
71 }
72
73 @Override
74 void collectRelatedFormats(Catalog catalog,
75 Map<String, Format> newFormats) {
76 }
77
78 @Override
79 void initialize(Catalog catalog, EntityModel model, int initVersion) {
80 if (values == null) {
81 initValues();
82 }
83 }
84
85 private void initValues() {
86 Class cls = getType();
87 if (cls != null) {
88 values = new Object[names.length];
89 for (int i = 0; i < names.length; i += 1) {
90 try {
91 values[i] = Enum.valueOf(cls, names[i]);
92 } catch (IllegalArgumentException e) {
93 throw new IllegalArgumentException
94 ("Deletion and renaming of enum values is not " +
95 "supported: " + names[i], e);
96 }
97 }
98 }
99 }
100
101 @Override
102 Object newArray(int len) {
103 return Array.newInstance(getType(), len);
104 }
105
106 @Override
107 public Object newInstance(EntityInput input, boolean rawAccess) {
108 int index = input.readEnumConstant(names);
109 if (rawAccess) {
110 return new RawObject(this, names[index]);
111 } else {
112 return values[index];
113 }
114 }
115
116 @Override
117 public Object readObject(Object o, EntityInput input, boolean rawAccess) {
118 /* newInstance reads the value -- do nothing here. */
119 return o;
120 }
121
122 @Override
123 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
124 if (rawAccess) {
125 String name = ((RawObject) o).getEnum();
126 for (int i = 0; i < names.length; i += 1) {
127 if (names[i].equals(name)) {
128 output.writeEnumConstant(names, i);
129 return;
130 }
131 }
132 } else {
133 for (int i = 0; i < values.length; i += 1) {
134 if (o == values[i]) {
135 output.writeEnumConstant(names, i);
136 return;
137 }
138 }
139 }
140 throw DbCompat.unexpectedState("Bad enum: " + o);
141 }
142
143 @Override
144 Object convertRawObject(Catalog catalog,
145 boolean rawAccess,
146 RawObject rawObject,
147 IdentityHashMap converted) {
148 String name = rawObject.getEnum();
149 for (int i = 0; i < names.length; i += 1) {
150 if (names[i].equals(name)) {
151 Object o = values[i];
152 converted.put(rawObject, o);
153 return o;
154 }
155 }
156 throw new IllegalArgumentException
157 ("Enum constant is not defined: " + name);
158 }
159
160 @Override
161 void skipContents(RecordInput input) {
162 input.skipFast(input.getPackedIntByteLength());
163 }
164
165 @Override
166 void copySecKey(RecordInput input, RecordOutput output) {
167 int len = input.getPackedIntByteLength();
168 output.writeFast
169 (input.getBufferBytes(), input.getBufferOffset(), len);
170 input.skipFast(len);
171 }
172
173 @Override
174 boolean evolve(Format newFormatParam, Evolver evolver) {
175 if (!(newFormatParam instanceof EnumFormat)) {
176 evolver.addEvolveError
177 (this, newFormatParam,
178 "Incompatible enum type changed detected",
179 "An enum class may not be changed to a non-enum type");
180 /* For future:
181 evolver.addMissingMutation
182 (this, newFormatParam,
183 "Converter is required when an enum class is changed to " +
184 "a non-enum type");
185 */
186 return false;
187 }
188
189 final EnumFormat newFormat = (EnumFormat) newFormatParam;
190
191 /* Return quickly if the enum was not changed at all. */
192 if (Arrays.equals(names, newFormat.names)) {
193 evolver.useOldFormat(this, newFormat);
194 return true;
195 }
196
197 final List<String> newNamesList = Arrays.asList(newFormat.names);
198 final Set<String> newNamesSet = new HashSet<String>(newNamesList);
199 final List<String> oldNamesList = Arrays.asList(names);
200
201 /* Deletion (or renaming, which appears as deletion) is not allowed. */
202 if (!newNamesSet.containsAll(oldNamesList)) {
203 final Set<String> oldNamesSet = new HashSet<String>(oldNamesList);
204 oldNamesSet.removeAll(newNamesSet);
205 evolver.addEvolveError
206 (this, newFormat,
207 "Incompatible enum type changed detected",
208 "Enum values may not be removed: " + oldNamesSet);
209 }
210
211 /* Use a List for additional names to preserve ordinal order. */
212 final List<String> additionalNamesList =
213 new ArrayList<String>(newNamesList);
214 additionalNamesList.removeAll(oldNamesList);
215 final int nAdditionalNames = additionalNamesList.size();
216
217 /*
218 * If there are no aditional names, the new and old formats are
219 * equivalent. This is the case where only the declaration order was
220 * changed.
221 */
222 if (nAdditionalNames == 0) {
223 evolver.useOldFormat(this, newFormat);
224 return true;
225 }
226
227 /*
228 * Evolve the new format. It should use the old names array, but with
229 * any additional names appended. [#17140]
230 */
231 final int nOldNames = names.length;
232 newFormat.names = new String[nOldNames + nAdditionalNames];
233 System.arraycopy(names, 0, newFormat.names, 0, nOldNames);
234 for (int i = 0; i < nAdditionalNames; i += 1) {
235 newFormat.names[nOldNames + i] = additionalNamesList.get(i);
236 }
237 newFormat.initValues();
238
239 /*
240 * Because we never change the array index (stored integer value) for
241 * an enum value, the new format can read the values written by the old
242 * format (newFormat is used as the Reader in the 2nd param below).
243 */
244 evolver.useEvolvedFormat(this, newFormat, newFormat);
245 return true;
246 }
247 }