001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: RawAccessor.java,v 1.5.2.5 2008/01/07 15:14:20 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.impl;
010:
011: import java.util.Collections;
012: import java.util.HashMap;
013: import java.util.List;
014:
015: import com.sleepycat.persist.raw.RawObject;
016:
017: /**
018: * Implements Accessor for RawObject access.
019: *
020: * @author Mark Hayes
021: */
022: class RawAccessor implements Accessor {
023:
024: private Format parentFormat;
025: private Accessor super Accessor;
026: private FieldInfo priKeyField;
027: private List<FieldInfo> secKeyFields;
028: private List<FieldInfo> nonKeyFields;
029: private boolean isCompositeKey;
030:
031: RawAccessor(Format parentFormat, Accessor super Accessor,
032: FieldInfo priKeyField, List<FieldInfo> secKeyFields,
033: List<FieldInfo> nonKeyFields) {
034: this .parentFormat = parentFormat;
035: this .super Accessor = super Accessor;
036: this .priKeyField = priKeyField;
037: this .secKeyFields = secKeyFields;
038: this .nonKeyFields = nonKeyFields;
039: }
040:
041: RawAccessor(Format parentFormat, List<FieldInfo> nonKeyFields) {
042: this .parentFormat = parentFormat;
043: this .nonKeyFields = nonKeyFields;
044: secKeyFields = Collections.emptyList();
045: isCompositeKey = true;
046: }
047:
048: public Object newInstance() {
049: RawObject super Object;
050: if (super Accessor != null) {
051: super Object = ((RawObject) super Accessor.newInstance());
052: } else {
053: super Object = null;
054: }
055: return new RawObject(parentFormat,
056: new HashMap<String, Object>(), super Object);
057: }
058:
059: public Object newArray(int len) {
060: throw new UnsupportedOperationException();
061: }
062:
063: public boolean isPriKeyFieldNullOrZero(Object o) {
064: if (priKeyField != null) {
065: Object val = getValue(o, priKeyField);
066: Format format = priKeyField.getType();
067: if (format.isPrimitive()) {
068: return ((Number) val).longValue() == 0L;
069: } else {
070: return val == null;
071: }
072: } else if (super Accessor != null) {
073: return super Accessor.isPriKeyFieldNullOrZero(getSuper(o));
074: } else {
075: throw new IllegalStateException("No primary key field");
076: }
077: }
078:
079: public void writePriKeyField(Object o, EntityOutput output) {
080: if (priKeyField != null) {
081: Object val = getValue(o, priKeyField);
082: Format format = priKeyField.getType();
083: output.writeKeyObject(val, format);
084: } else if (super Accessor != null) {
085: super Accessor.writePriKeyField(getSuper(o), output);
086: } else {
087: throw new IllegalStateException("No primary key field");
088: }
089: }
090:
091: public void readPriKeyField(Object o, EntityInput input) {
092: if (priKeyField != null) {
093: Format format = priKeyField.getType();
094: Object val = input.readKeyObject(format);
095: setValue(o, priKeyField, val);
096: } else if (super Accessor != null) {
097: super Accessor.readPriKeyField(getSuper(o), input);
098: } else {
099: throw new IllegalStateException("No primary key field");
100: }
101: }
102:
103: public void writeSecKeyFields(Object o, EntityOutput output) {
104: if (priKeyField != null && !priKeyField.getType().isPrimitive()) {
105: output.registerPriKeyObject(getValue(o, priKeyField));
106: }
107: if (super Accessor != null) {
108: super Accessor.writeSecKeyFields(getSuper(o), output);
109: }
110: for (int i = 0; i < secKeyFields.size(); i += 1) {
111: writeField(o, secKeyFields.get(i), output);
112: }
113: }
114:
115: public void readSecKeyFields(Object o, EntityInput input,
116: int startField, int endField, int super Level) {
117: if (priKeyField != null && !priKeyField.getType().isPrimitive()) {
118: input.registerPriKeyObject(getValue(o, priKeyField));
119: }
120: if (super Level != 0 && super Accessor != null) {
121: super Accessor.readSecKeyFields(getSuper(o), input,
122: startField, endField, super Level - 1);
123: } else {
124: if (super Level > 0) {
125: throw new IllegalStateException(
126: "Super class does not exist");
127: }
128: }
129: if (super Level <= 0) {
130: for (int i = startField; i <= endField
131: && i < secKeyFields.size(); i += 1) {
132: readField(o, secKeyFields.get(i), input);
133: }
134: }
135: }
136:
137: public void writeNonKeyFields(Object o, EntityOutput output) {
138: if (super Accessor != null) {
139: super Accessor.writeNonKeyFields(getSuper(o), output);
140: }
141: for (int i = 0; i < nonKeyFields.size(); i += 1) {
142: writeField(o, nonKeyFields.get(i), output);
143: }
144: }
145:
146: public void readNonKeyFields(Object o, EntityInput input,
147: int startField, int endField, int super Level) {
148: if (super Level != 0 && super Accessor != null) {
149: super Accessor.readNonKeyFields(getSuper(o), input,
150: startField, endField, super Level - 1);
151: } else {
152: if (super Level > 0) {
153: throw new IllegalStateException(
154: "Super class does not exist");
155: }
156: }
157: if (super Level <= 0) {
158: for (int i = startField; i <= endField
159: && i < nonKeyFields.size(); i += 1) {
160: readField(o, nonKeyFields.get(i), input);
161: }
162: }
163: }
164:
165: public Object getField(Object o, int field, int super Level,
166: boolean isSecField) {
167: if (super Level > 0) {
168: return super Accessor.getField(getSuper(o), field,
169: super Level - 1, isSecField);
170: }
171: FieldInfo fld = isSecField ? secKeyFields.get(field)
172: : nonKeyFields.get(field);
173: return getValue(o, fld);
174: }
175:
176: public void setField(Object o, int field, int super Level,
177: boolean isSecField, Object value) {
178: if (super Level > 0) {
179: super Accessor.setField(getSuper(o), field, super Level - 1,
180: isSecField, value);
181: return;
182: }
183: FieldInfo fld = isSecField ? secKeyFields.get(field)
184: : nonKeyFields.get(field);
185: setValue(o, fld, value);
186: }
187:
188: private RawObject getSuper(Object o) {
189: return ((RawObject) o).getSuper();
190: }
191:
192: private Object getValue(Object o, FieldInfo field) {
193: return ((RawObject) o).getValues().get(field.getName());
194: }
195:
196: private void setValue(Object o, FieldInfo field, Object val) {
197: ((RawObject) o).getValues().put(field.getName(), val);
198: }
199:
200: private void writeField(Object o, FieldInfo field,
201: EntityOutput output) {
202: Object val = getValue(o, field);
203: Format format = field.getType();
204: if (isCompositeKey || format.isPrimitive()) {
205: output.writeKeyObject(val, format);
206: } else {
207: output.writeObject(val, format);
208: }
209: }
210:
211: private void readField(Object o, FieldInfo field, EntityInput input) {
212: Format format = field.getType();
213: Object val;
214: if (isCompositeKey || format.isPrimitive()) {
215: val = input.readKeyObject(format);
216: } else {
217: val = input.readObject();
218: }
219: setValue(o, field, val);
220: }
221: }
|