001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: PrimitiveArrayFormat.java,v 1.20.2.3 2008/01/07 15:14:20 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.impl;
010:
011: import java.lang.reflect.Array;
012: import java.util.IdentityHashMap;
013: import java.util.Map;
014: import java.util.Set;
015:
016: import com.sleepycat.persist.raw.RawObject;
017:
018: /**
019: * An array of primitives having one dimension. Multidimensional arrays are
020: * handled by {@link ObjectArrayFormat}.
021: *
022: * @author Mark Hayes
023: */
024: public class PrimitiveArrayFormat extends Format {
025:
026: private static final long serialVersionUID = 8285299924106073591L;
027:
028: private SimpleFormat componentFormat;
029:
030: PrimitiveArrayFormat(Class type) {
031: super (type);
032: }
033:
034: @Override
035: public boolean isArray() {
036: return true;
037: }
038:
039: @Override
040: public int getDimensions() {
041: return 1;
042: }
043:
044: @Override
045: public Format getComponentType() {
046: return componentFormat;
047: }
048:
049: @Override
050: void collectRelatedFormats(Catalog catalog,
051: Map<String, Format> newFormats) {
052: /* Component type is simple and simple type formats are predefined. */
053: }
054:
055: @Override
056: void initialize(Catalog catalog, int initVersion) {
057: componentFormat = (SimpleFormat) catalog.getFormat(getType()
058: .getComponentType());
059: }
060:
061: @Override
062: Object newArray(int len) {
063: return Array.newInstance(getType(), len);
064: }
065:
066: @Override
067: public Object newInstance(EntityInput input, boolean rawAccess) {
068: int len = input.readArrayLength();
069: if (rawAccess) {
070: return new RawObject(this , new Object[len]);
071: } else {
072: return componentFormat.newPrimitiveArray(len, input);
073: }
074: }
075:
076: @Override
077: public Object readObject(Object o, EntityInput input,
078: boolean rawAccess) {
079: if (rawAccess) {
080: Object[] a = ((RawObject) o).getElements();
081: for (int i = 0; i < a.length; i += 1) {
082: a[i] = componentFormat.newInstance(input, true);
083: componentFormat.readObject(a[i], input, true);
084: }
085: }
086: /* Else, do nothing -- newInstance reads the value. */
087: return o;
088: }
089:
090: @Override
091: void writeObject(Object o, EntityOutput output, boolean rawAccess) {
092: if (rawAccess) {
093: Object[] a = ((RawObject) o).getElements();
094: output.writeArrayLength(a.length);
095: for (int i = 0; i < a.length; i += 1) {
096: componentFormat.writeObject(a[i], output, true);
097: }
098: } else {
099: componentFormat.writePrimitiveArray(o, output);
100: }
101: }
102:
103: @Override
104: Object convertRawObject(Catalog catalog, boolean rawAccess,
105: RawObject rawObject, IdentityHashMap converted) {
106: RawArrayInput input = new RawArrayInput(catalog, rawAccess,
107: converted, rawObject, componentFormat);
108: Object a = newInstance(input, rawAccess);
109: converted.put(rawObject, a);
110: return readObject(a, input, rawAccess);
111: }
112:
113: @Override
114: void skipContents(RecordInput input) {
115: int len = input.readPackedInt();
116: componentFormat.skipPrimitiveArray(len, input);
117: }
118:
119: @Override
120: void copySecMultiKey(RecordInput input, Format keyFormat,
121: Set results) {
122: int len = input.readPackedInt();
123: componentFormat.copySecMultiKeyPrimitiveArray(len, input,
124: results);
125: }
126:
127: @Override
128: boolean evolve(Format newFormat, Evolver evolver) {
129: evolver.useOldFormat(this , newFormat);
130: return true;
131: }
132: }
|