KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > YapJavaClass


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o;
22
23 import com.db4o.foundation.*;
24 import com.db4o.inside.*;
25 import com.db4o.inside.marshall.*;
26 import com.db4o.reflect.*;
27
28 /**
29  * @exclude
30  */

31 public abstract class YapJavaClass implements TypeHandler4 {
32     
33     protected final YapStream _stream;
34     
35     protected ReflectClass _classReflector;
36     
37     private ReflectClass _primitiveClassReflector;
38     
39     public YapJavaClass(YapStream stream) {
40         _stream = stream;
41     }
42
43     private boolean i_compareToIsNull;
44
45     public boolean canHold(ReflectClass claxx) {
46         return claxx.equals(classReflector());
47     }
48
49     public void cascadeActivation(Transaction a_trans, Object JavaDoc a_object,
50         int a_depth, boolean a_activate) {
51         // do nothing
52
}
53     
54     public Object JavaDoc coerce(ReflectClass claxx, Object JavaDoc obj) {
55         return canHold(claxx) ? obj : No4.INSTANCE;
56     }
57     
58     public Object JavaDoc comparableObject(Transaction a_trans, Object JavaDoc a_object) {
59         return a_object;
60     }
61
62     public void copyValue(Object JavaDoc a_from, Object JavaDoc a_to) {
63         // do nothing
64
}
65
66     public abstract Object JavaDoc defaultValue();
67
68     public void deleteEmbedded(MarshallerFamily mf, YapWriter a_bytes) {
69         a_bytes.incrementOffset(linkLength());
70     }
71
72     public boolean equals(TypeHandler4 a_dataType) {
73         return (this == a_dataType);
74     }
75     
76     public int getTypeID() {
77         return YapConst.TYPE_SIMPLE;
78     }
79
80     public YapClass getYapClass(YapStream a_stream) {
81         return a_stream.i_handlers.i_yapClasses[getID() - 1];
82     }
83
84     public boolean hasFixedLength(){
85         return true;
86     }
87     
88     public Object JavaDoc indexEntryToObject(Transaction trans, Object JavaDoc indexEntry){
89         return indexEntry;
90     }
91     
92     public boolean indexNullHandling() {
93         return false;
94     }
95     
96     public int isSecondClass(){
97         return YapConst.YES;
98     }
99     
100     public void calculateLengths(Transaction trans, ObjectHeaderAttributes header, boolean topLevel, Object JavaDoc obj, boolean withIndirection) {
101         if(topLevel){
102             header.addBaseLength(linkLength());
103         }else{
104             header.addPayLoadLength(linkLength());
105         }
106     }
107
108     public void prepareComparison(Transaction a_trans, Object JavaDoc obj) {
109         prepareComparison(obj);
110     }
111
112     protected abstract Class JavaDoc primitiveJavaClass();
113     
114     abstract Object JavaDoc primitiveNull();
115     
116     public boolean readArray(Object JavaDoc array, YapReader reader) {
117         return false;
118     }
119
120     public TypeHandler4 readArrayHandler(Transaction a_trans, MarshallerFamily mf, YapReader[] a_bytes) {
121         // virtual and do nothing
122
return null;
123     }
124
125     public Object JavaDoc readQuery(Transaction trans, MarshallerFamily mf, boolean withRedirection, YapReader reader, boolean toArray)
126         throws CorruptionException {
127         return read1(reader);
128     }
129
130     public Object JavaDoc read(MarshallerFamily mf, YapWriter writer, boolean redirect) throws CorruptionException {
131         return read1(writer);
132     }
133
134     abstract Object JavaDoc read1(YapReader reader) throws CorruptionException;
135
136     public void readCandidates(MarshallerFamily mf, YapReader a_bytes, QCandidates a_candidates) {
137         // do nothing
138
}
139     
140     public QCandidate readSubCandidate(MarshallerFamily mf, YapReader reader, QCandidates candidates, boolean withIndirection) {
141         try {
142             Object JavaDoc obj = readQuery(candidates.i_trans, mf, withIndirection, reader, true);
143             if(obj != null){
144                 return new QCandidate(candidates, obj, 0, true);
145             }
146         } catch (CorruptionException e) {
147         }
148         return null;
149     }
150
151     public Object JavaDoc readIndexEntry(YapReader a_reader) {
152         try {
153             return read1(a_reader);
154         } catch (CorruptionException e) {
155         }
156         return null;
157     }
158     
159     public Object JavaDoc readIndexEntry(MarshallerFamily mf, YapWriter a_writer) throws CorruptionException{
160         return read(mf, a_writer, true);
161     }
162     
163     public ReflectClass classReflector(){
164         
165         if(_classReflector != null){
166             return _classReflector;
167         }
168         _classReflector = _stream.reflector().forClass(defaultValue().getClass());
169         Class JavaDoc clazz = primitiveJavaClass();
170         if(clazz != null){
171             _primitiveClassReflector = _stream.reflector().forClass(clazz);
172         }
173         return _classReflector;
174     }
175     
176     /**
177      *
178      * classReflector() has to be called first, before this returns a value
179      */

180     public ReflectClass primitiveClassReflector(){
181         return _primitiveClassReflector;
182     }
183
184     public boolean supportsIndex() {
185         return true;
186     }
187
188     public abstract void write(Object JavaDoc a_object, YapReader a_bytes);
189     
190     public boolean writeArray(Object JavaDoc array, YapReader reader) {
191         return false;
192     }
193
194     public void writeIndexEntry(YapReader a_writer, Object JavaDoc a_object) {
195         if (a_object == null) {
196             a_object = primitiveNull();
197         }
198         write(a_object, a_writer);
199     }
200     
201     public Object JavaDoc writeNew(MarshallerFamily mf, Object JavaDoc a_object, boolean topLevel, YapWriter a_bytes, boolean withIndirection, boolean restoreLinkeOffset) {
202         if (a_object == null) {
203             a_object = primitiveNull();
204         }
205         write(a_object, a_bytes);
206         return a_object;
207     }
208
209     public YapComparable prepareComparison(Object JavaDoc obj) {
210         if (obj == null) {
211             i_compareToIsNull = true;
212             return Null.INSTANCE;
213         }
214         i_compareToIsNull = false;
215         prepareComparison1(obj);
216         return this;
217     }
218     
219     public Object JavaDoc current(){
220         if (i_compareToIsNull){
221             return null;
222         }
223         return current1();
224     }
225
226     abstract void prepareComparison1(Object JavaDoc obj);
227     
228     public abstract Object JavaDoc current1();
229
230     public int compareTo(Object JavaDoc obj) {
231         if (i_compareToIsNull) {
232             if (obj == null) {
233                 return 0;
234             }
235             return 1;
236         }
237         if (obj == null) {
238             return -1;
239         }
240         if (isEqual1(obj)) {
241             return 0;
242         }
243         if (isGreater1(obj)) {
244             return 1;
245         }
246         return -1;
247     }
248
249     public boolean isEqual(Object JavaDoc obj) {
250         if (i_compareToIsNull) {
251             return obj == null;
252         }
253         return isEqual1(obj);
254     }
255
256     abstract boolean isEqual1(Object JavaDoc obj);
257
258     public boolean isGreater(Object JavaDoc obj) {
259         if (i_compareToIsNull) {
260             return obj != null;
261         }
262         return isGreater1(obj);
263     }
264
265     abstract boolean isGreater1(Object JavaDoc obj);
266
267     public boolean isSmaller(Object JavaDoc obj) {
268         if (i_compareToIsNull) {
269             return false;
270         }
271         return isSmaller1(obj);
272     }
273
274     abstract boolean isSmaller1(Object JavaDoc obj);
275
276     // redundant, only added to make Sun JDK 1.2's java happy :(
277
public abstract int linkLength();
278     
279     public final void defrag(MarshallerFamily mf, ReaderPair readers, boolean redirect) {
280         int linkLength = linkLength();
281         readers.incrementOffset(linkLength);
282     }
283     
284     public void defragIndexEntry(ReaderPair readers) {
285         try {
286             read1(readers.source());
287             read1(readers.target());
288         } catch (CorruptionException exc) {
289             Exceptions4.virtualException();
290         }
291     }
292 }
Popular Tags