001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.handlers;
022:
023: import com.db4o.*;
024: import com.db4o.foundation.*;
025: import com.db4o.internal.*;
026: import com.db4o.internal.marshall.*;
027: import com.db4o.marshall.*;
028: import com.db4o.reflect.*;
029:
030: /**
031: * @exclude
032: */
033: public abstract class PrimitiveHandler implements IndexableTypeHandler,
034: BuiltinTypeHandler {
035:
036: protected final ObjectContainerBase _stream;
037:
038: protected ReflectClass _classReflector;
039:
040: private ReflectClass _primitiveClassReflector;
041:
042: public PrimitiveHandler(ObjectContainerBase stream) {
043: _stream = stream;
044: }
045:
046: private boolean i_compareToIsNull;
047:
048: public Object coerce(ReflectClass claxx, Object obj) {
049: return Handlers4.handlerCanHold(this , claxx) ? obj
050: : No4.INSTANCE;
051: }
052:
053: public abstract Object defaultValue();
054:
055: public void deleteEmbedded(MarshallerFamily mf,
056: StatefulBuffer a_bytes) {
057: a_bytes.incrementOffset(linkLength());
058: }
059:
060: public Object indexEntryToObject(Transaction trans,
061: Object indexEntry) {
062: return indexEntry;
063: }
064:
065: protected abstract Class primitiveJavaClass();
066:
067: public abstract Object primitiveNull();
068:
069: /**
070: *
071: * @param mf
072: * @param buffer
073: * @param redirect
074: */
075: public Object read(
076:
077: /* FIXME: Work in progress here, this signature should not be used */
078: MarshallerFamily mf,
079:
080: StatefulBuffer buffer, boolean redirect) throws CorruptionException {
081: return read1(buffer);
082: }
083:
084: abstract Object read1(Buffer reader) throws CorruptionException;
085:
086: public Object readIndexEntry(Buffer buffer) {
087: try {
088: return read1(buffer);
089: } catch (CorruptionException e) {
090: }
091: return null;
092: }
093:
094: public Object readIndexEntry(MarshallerFamily mf,
095: StatefulBuffer a_writer) throws CorruptionException {
096: return read(mf, a_writer, true);
097: }
098:
099: public ReflectClass classReflector() {
100: ensureClassReflectorLoaded();
101: return _classReflector;
102: }
103:
104: public ReflectClass primitiveClassReflector() {
105: ensureClassReflectorLoaded();
106: return _primitiveClassReflector;
107: }
108:
109: private void ensureClassReflectorLoaded() {
110: if (_classReflector != null) {
111: return;
112: }
113: _classReflector = _stream.reflector().forClass(
114: defaultValue().getClass());
115: Class clazz = primitiveJavaClass();
116: if (clazz != null) {
117: _primitiveClassReflector = _stream.reflector().forClass(
118: clazz);
119: }
120: }
121:
122: public abstract void write(Object a_object, Buffer a_bytes);
123:
124: public void writeIndexEntry(Buffer a_writer, Object a_object) {
125: if (a_object == null) {
126: a_object = primitiveNull();
127: }
128: write(a_object, a_writer);
129: }
130:
131: public Comparable4 prepareComparison(Object obj) {
132: if (obj == null) {
133: i_compareToIsNull = true;
134: return Null.INSTANCE;
135: }
136: i_compareToIsNull = false;
137: prepareComparison1(obj);
138: return this ;
139: }
140:
141: abstract void prepareComparison1(Object obj);
142:
143: public int compareTo(Object obj) {
144: if (i_compareToIsNull) {
145: if (obj == null) {
146: return 0;
147: }
148: return 1;
149: }
150: if (obj == null) {
151: return -1;
152: }
153: if (isEqual1(obj)) {
154: return 0;
155: }
156: if (isGreater1(obj)) {
157: return 1;
158: }
159: return -1;
160: }
161:
162: abstract boolean isEqual1(Object obj);
163:
164: abstract boolean isGreater1(Object obj);
165:
166: abstract boolean isSmaller1(Object obj);
167:
168: // redundant, only added to make Sun JDK 1.2's java happy :(
169: public abstract int linkLength();
170:
171: public final void defrag(MarshallerFamily mf, BufferPair readers,
172: boolean redirect) {
173: int linkLength = linkLength();
174: readers.incrementOffset(linkLength);
175: }
176:
177: public void defragIndexEntry(BufferPair readers) {
178: try {
179: read1(readers.source());
180: read1(readers.target());
181: } catch (CorruptionException exc) {
182: Exceptions4.virtualException();
183: }
184: }
185:
186: protected PrimitiveMarshaller primitiveMarshaller() {
187: return MarshallerFamily.current()._primitive;
188: }
189:
190: public void write(WriteContext context, Object obj) {
191: throw new NotImplementedException();
192: }
193:
194: public Object read(ReadContext context) {
195: throw new NotImplementedException();
196: }
197:
198: public Object nullRepresentationInUntypedArrays() {
199: return primitiveNull();
200: }
201:
202: }
|