001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: RawAbstractInput.java,v 1.6.2.3 2008/01/07 15:14:20 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.impl;
010:
011: import java.math.BigInteger;
012: import java.util.IdentityHashMap;
013:
014: import com.sleepycat.persist.raw.RawObject;
015:
016: /**
017: * Base class for EntityInput implementations that type-check RawObject
018: * instances and convert them to regular persistent objects, via the
019: * Format.convertRawObject method.
020: *
021: * The subclass implements readNext which should call checkAndConvert before
022: * returning the final value.
023: *
024: * @author Mark Hayes
025: */
026: abstract class RawAbstractInput extends AbstractInput {
027:
028: private IdentityHashMap converted;
029:
030: RawAbstractInput(Catalog catalog, boolean rawAccess,
031: IdentityHashMap converted) {
032: super (catalog, rawAccess);
033: this .converted = converted;
034: }
035:
036: public Object readObject() {
037: return readNext();
038: }
039:
040: public Object readKeyObject(Format format) {
041: return readNext();
042: }
043:
044: public void registerPriKeyObject(Object o) {
045: }
046:
047: public int readArrayLength() {
048: throw new UnsupportedOperationException();
049: }
050:
051: public int readEnumConstant(String[] names) {
052: throw new UnsupportedOperationException();
053: }
054:
055: public void skipField(Format declaredFormat) {
056: }
057:
058: abstract Object readNext();
059:
060: Object checkAndConvert(Object o, Format declaredFormat) {
061: if (o == null) {
062: if (declaredFormat.isPrimitive()) {
063: throw new IllegalArgumentException(
064: "A primitive type may not be null or missing: "
065: + declaredFormat.getClassName());
066: }
067: } else if (declaredFormat.isSimple()) {
068: if (declaredFormat.isPrimitive()) {
069: if (o.getClass() != declaredFormat.getWrapperFormat()
070: .getType()) {
071: throw new IllegalArgumentException(
072: "Raw value class: "
073: + o.getClass().getName()
074: + " must be the wrapper class for a primitive type: "
075: + declaredFormat.getClassName());
076: }
077: } else {
078: if (o.getClass() != declaredFormat.getType()) {
079: throw new IllegalArgumentException(
080: "Raw value class: "
081: + o.getClass().getName()
082: + " must be the declared class for a simple type: "
083: + declaredFormat.getClassName());
084: }
085: }
086: } else {
087: if (o instanceof RawObject) {
088: Object o2 = null;
089: if (!rawAccess) {
090: if (converted != null) {
091: o2 = converted.get(o);
092: } else {
093: converted = new IdentityHashMap();
094: }
095: }
096: if (o2 != null) {
097: o = o2;
098: } else {
099: if (!rawAccess) {
100: o = catalog.convertRawObject((RawObject) o,
101: converted);
102: }
103: }
104: } else {
105: if (!SimpleCatalog.isSimpleType(o.getClass())) {
106: throw new IllegalArgumentException(
107: "Raw value class: "
108: + o.getClass().getName()
109: + " must be RawObject a simple type");
110: }
111: }
112: if (rawAccess) {
113: checkRawType(catalog, o, declaredFormat);
114: } else {
115: if (!declaredFormat.getType().isAssignableFrom(
116: o.getClass())) {
117: throw new IllegalArgumentException(
118: "Raw value class: "
119: + o.getClass().getName()
120: + " is not assignable to type: "
121: + declaredFormat.getClassName());
122: }
123: }
124: }
125: return o;
126: }
127:
128: static Format checkRawType(Catalog catalog, Object o,
129: Format declaredFormat) {
130: assert declaredFormat != null;
131: Format format;
132: if (o instanceof RawObject) {
133: format = (Format) ((RawObject) o).getType();
134: } else {
135: format = catalog.getFormat(o.getClass());
136: if (!format.isSimple() || format.isEnum()) {
137: throw new IllegalArgumentException(
138: "Not a RawObject or a non-enum simple type: "
139: + format.getClassName());
140: }
141: }
142: if (!format.isAssignableTo(declaredFormat)) {
143: throw new IllegalArgumentException(
144: "Not a subtype of the field's declared class "
145: + declaredFormat.getClassName() + ": "
146: + format.getClassName());
147: }
148: if (!format.isCurrentVersion()) {
149: throw new IllegalArgumentException(
150: "Raw type version is not current. Class: "
151: + format.getClassName() + " Version: "
152: + format.getVersion());
153: }
154: Format proxiedFormat = format.getProxiedFormat();
155: if (proxiedFormat != null) {
156: format = proxiedFormat;
157: }
158: return format;
159: }
160:
161: /* The following methods are a subset of the methods in TupleInput. */
162:
163: public String readString() {
164: return (String) readNext();
165: }
166:
167: public char readChar() {
168: return ((Character) readNext()).charValue();
169: }
170:
171: public boolean readBoolean() {
172: return ((Boolean) readNext()).booleanValue();
173: }
174:
175: public byte readByte() {
176: return ((Byte) readNext()).byteValue();
177: }
178:
179: public short readShort() {
180: return ((Short) readNext()).shortValue();
181: }
182:
183: public int readInt() {
184: return ((Integer) readNext()).intValue();
185: }
186:
187: public long readLong() {
188: return ((Long) readNext()).longValue();
189: }
190:
191: public float readSortedFloat() {
192: return ((Float) readNext()).floatValue();
193: }
194:
195: public double readSortedDouble() {
196: return ((Double) readNext()).doubleValue();
197: }
198:
199: public BigInteger readBigInteger() {
200: return (BigInteger) readNext();
201: }
202: }
|