Debian Codegraph @

lang/java/src/com/sleepycat/persist/impl/PrimitiveArrayFormat.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.Map;
12 import java.util.Set;
13
14 import com.sleepycat.persist.model.EntityModel;
15 import com.sleepycat.persist.raw.RawObject;
16 import java.util.IdentityHashMap;
17
18 /**
19 * An array of primitives having one dimension. Multidimensional arrays are
20 * handled by {@link ObjectArrayFormat}.
21 *
22 * @author Mark Hayes
23 */
24 public class PrimitiveArrayFormat extends Format {
25
26 private static final long serialVersionUID = 8285299924106073591L;
27
28 private SimpleFormat componentFormat;
29
30 PrimitiveArrayFormat(Catalog catalog, Class type) {
31 super(catalog, type);
32 }
33
34 @Override
35 public boolean isArray() {
36 return true;
37 }
38
39 @Override
40 public int getDimensions() {
41 return 1;
42 }
43
44 @Override
45 public Format getComponentType() {
46 return componentFormat;
47 }
48
49 @Override
50 void collectRelatedFormats(Catalog catalog,
51 Map<String, Format> newFormats) {
52 /* Component type is simple and simple type formats are predefined. */
53 }
54
55 @Override
56 void initialize(Catalog catalog, EntityModel model, int initVersion) {
57
58 /*
59 * getExistingType is allowed (to support raw mode) because primitive
60 * arrays are always available in Java.
61 */
62 componentFormat = (SimpleFormat)
63 catalog.getFormat(getExistingType().getComponentType().getName());
64 }
65
66 @Override
67 Object newArray(int len) {
68 return Array.newInstance(getType(), len);
69 }
70
71 @Override
72 public Object newInstance(EntityInput input, boolean rawAccess)
73 throws RefreshException {
74
75 int len = input.readArrayLength();
76 if (rawAccess) {
77 return new RawObject(this, new Object[len]);
78 } else {
79 return componentFormat.newPrimitiveArray(len, input);
80 }
81 }
82
83 @Override
84 public Object readObject(Object o, EntityInput input, boolean rawAccess)
85 throws RefreshException {
86
87 if (rawAccess) {
88 Object[] a = ((RawObject) o).getElements();
89 for (int i = 0; i < a.length; i += 1) {
90 a[i] = componentFormat.newInstance(input, true);
91 componentFormat.readObject(a[i], input, true);
92 }
93 }
94 /* Else, do nothing -- newInstance reads the value. */
95 return o;
96 }
97
98 @Override
99 void writeObject(Object o, EntityOutput output, boolean rawAccess)
100 throws RefreshException {
101
102 if (rawAccess) {
103 Object[] a = ((RawObject) o).getElements();
104 output.writeArrayLength(a.length);
105 for (int i = 0; i < a.length; i += 1) {
106 componentFormat.writeObject(a[i], output, true);
107 }
108 } else {
109 componentFormat.writePrimitiveArray(o, output);
110 }
111 }
112
113 @Override
114 Object convertRawObject(Catalog catalog,
115 boolean rawAccess,
116 RawObject rawObject,
117 IdentityHashMap converted)
118 throws RefreshException {
119
120 RawArrayInput input = new RawArrayInput
121 (catalog, rawAccess, converted, rawObject, componentFormat);
122 Object a = newInstance(input, rawAccess);
123 converted.put(rawObject, a);
124 return readObject(a, input, rawAccess);
125 }
126
127 @Override
128 void skipContents(RecordInput input) {
129 int len = input.readPackedInt();
130 componentFormat.skipPrimitiveArray(len, input);
131 }
132
133 @Override
134 void copySecMultiKey(RecordInput input, Format keyFormat, Set results) {
135 int len = input.readPackedInt();
136 componentFormat.copySecMultiKeyPrimitiveArray(len, input, results);
137 }
138
139 @Override
140 boolean evolve(Format newFormat, Evolver evolver) {
141 evolver.useOldFormat(this, newFormat);
142 return true;
143 }
144 }