KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > YapArrayN


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.inside.marshall.*;
24 import com.db4o.reflect.*;
25
26 /**
27  * n-dimensional array
28  * @exclude
29  */

30 public final class YapArrayN extends YapArray {
31     
32     
33     public YapArrayN(YapStream stream, TypeHandler4 a_handler, boolean a_isPrimitive) {
34         super(stream, a_handler, a_isPrimitive);
35     }
36
37     public final Object JavaDoc[] allElements(Object JavaDoc a_array) {
38         int[] dim = _reflectArray.dimensions(a_array);
39         Object JavaDoc[] flat = new Object JavaDoc[elementCount(dim)];
40         _reflectArray.flatten(a_array, dim, 0, flat, 0);
41         return flat;
42     }
43
44     public final int elementCount(Transaction a_trans, YapReader a_bytes) {
45         return elementCount(readDimensions(a_trans, a_bytes, new ReflectClass[1]));
46     }
47
48     private final int elementCount(int[] a_dim) {
49         int elements = a_dim[0];
50         for (int i = 1; i < a_dim.length; i++) {
51             elements = elements * a_dim[i];
52         }
53         return elements;
54     }
55
56     public final byte identifier() {
57         return YapConst.YAPARRAYN;
58     }
59
60     public final int objectLength(Object JavaDoc a_object) {
61         int[] dim = _reflectArray.dimensions(a_object);
62         return YapConst.OBJECT_LENGTH
63             + (YapConst.INT_LENGTH * (2 + dim.length))
64             + (elementCount(dim) * i_handler.linkLength());
65     }
66     
67     public int ownLength(Object JavaDoc obj){
68         int[] dim = _reflectArray.dimensions(obj);
69         return YapConst.OBJECT_LENGTH
70             + (YapConst.INT_LENGTH * (2 + dim.length));
71     }
72
73     public final Object JavaDoc read1(MarshallerFamily mf, YapWriter reader) throws CorruptionException {
74         
75         if (Deploy.debug) {
76             reader.readBegin(identifier());
77         }
78         
79         Object JavaDoc[] ret = new Object JavaDoc[1];
80         int[] dim = read1Create(reader.getTransaction(), reader, ret);
81         if(ret[0] != null){
82             Object JavaDoc[] objects = new Object JavaDoc[elementCount(dim)];
83             for (int i = 0; i < objects.length; i++) {
84                 objects[i] = i_handler.read(mf, reader, true);
85             }
86             _reflectArray.shape(objects, 0, ret[0], dim, 0);
87         }
88         
89         if (Deploy.debug) {
90             reader.readEnd();
91         }
92         
93         return ret[0];
94     }
95     
96     protected int readElementsDefrag(ReaderPair readers) {
97         int numDimensions=super.readElementsDefrag(readers);
98         int [] dimensions=new int[numDimensions];
99         for (int i = 0; i < numDimensions; i++) {
100             dimensions[i]=readers.readInt();
101         }
102         return elementCount(dimensions);
103     }
104     
105     public final void read1Candidates(MarshallerFamily mf, YapReader reader, QCandidates candidates) {
106         if(Deploy.debug){
107             reader.readBegin(identifier());
108         }
109         
110         Object JavaDoc[] ret = new Object JavaDoc[1];
111         int[] dim = read1Create(candidates.i_trans, reader, ret);
112         if(ret[0] != null){
113             int count = elementCount(dim);
114             for (int i = 0; i < count; i++) {
115                 QCandidate qc = i_handler.readSubCandidate(mf, reader, candidates, true);
116                 if(qc != null){
117                     candidates.addByIdentity(qc);
118                 }
119             }
120         }
121         
122         if (Deploy.debug) {
123             reader.readEnd();
124         }
125     }
126     
127     public final Object JavaDoc read1Query(Transaction a_trans, MarshallerFamily mf, YapReader a_bytes) throws CorruptionException {
128         
129         if(Deploy.debug){
130             a_bytes.readBegin(identifier());
131         }
132         
133         Object JavaDoc[] ret = new Object JavaDoc[1];
134         int[] dim = read1Create(a_trans, a_bytes, ret);
135         if(ret[0] != null){
136             Object JavaDoc[] objects = new Object JavaDoc[elementCount(dim)];
137             for (int i = 0; i < objects.length; i++) {
138                 objects[i] = i_handler.readQuery(a_trans, mf, true, a_bytes, true);
139             }
140             _reflectArray.shape(objects, 0, ret[0], dim, 0);
141         }
142         
143         if (Deploy.debug) {
144             a_bytes.readEnd();
145         }
146
147         return ret[0];
148     }
149
150     private int[] read1Create(Transaction a_trans, YapReader a_bytes, Object JavaDoc[] obj) {
151         ReflectClass[] clazz = new ReflectClass[1];
152         int[] dim = readDimensions(a_trans, a_bytes, clazz);
153         if (i_isPrimitive) {
154             obj[0] = a_trans.reflector().array().newInstance(i_handler.primitiveClassReflector(), dim);
155         } else {
156             if (clazz[0] != null) {
157                 obj[0] = a_trans.reflector().array().newInstance(clazz[0], dim);
158             }
159         }
160         return dim;
161     }
162
163     private final int[] readDimensions(Transaction a_trans, YapReader a_bytes, ReflectClass[] clazz) {
164         int[] dim = new int[readElementsAndClass(a_trans, a_bytes, clazz)];
165         for (int i = 0; i < dim.length; i++) {
166             dim[i] = a_bytes.readInt();
167         }
168         return dim;
169     }
170
171     public final void writeNew1(Object JavaDoc obj, YapWriter writer) {
172         
173         if (Deploy.debug) {
174             writer.writeBegin(identifier());
175         }
176         
177         int[] dim = _reflectArray.dimensions(obj);
178         writeClass(obj, writer);
179         writer.writeInt(dim.length);
180         for (int i = 0; i < dim.length; i++) {
181             writer.writeInt(dim[i]);
182         }
183         Object JavaDoc[] objects = allElements(obj);
184         
185         MarshallerFamily mf = MarshallerFamily.current();
186         
187         for (int i = 0; i < objects.length; i++) {
188             i_handler.writeNew(mf, element(objects, i), false, writer, true, true);
189         }
190         
191         if (Deploy.debug) {
192             writer.writeEnd();
193         }
194         
195     }
196
197     private Object JavaDoc element(Object JavaDoc a_array, int a_position) {
198         try {
199             return _reflectArray.get(a_array, a_position);
200         } catch (Exception JavaDoc e) {
201             return null;
202         }
203     }
204 }
205
Popular Tags