KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > reflect > core > AbstractReflectArray


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.reflect.core;
22
23 import java.lang.reflect.Array JavaDoc;
24
25 import com.db4o.reflect.*;
26
27 /**
28  * @exclude
29  */

30 public abstract class AbstractReflectArray implements ReflectArray {
31     
32     protected final Reflector _reflector;
33
34     public AbstractReflectArray(Reflector reflector) {
35         _reflector = reflector;
36     }
37
38     public abstract Object JavaDoc newInstance(ReflectClass componentType, int[] dimensions);
39
40     public abstract Object JavaDoc newInstance(ReflectClass componentType, int length);
41
42     public int[] dimensions(Object JavaDoc arr) {
43         int count = 0;
44         ReflectClass claxx = _reflector.forObject(arr);
45         while (claxx.isArray()) {
46             count++;
47             claxx = claxx.getComponentType();
48         }
49         int dim[] = new int[count];
50         for (int i = 0; i < count; i++) {
51             try {
52                 dim[i] = getLength(arr);
53                 arr = get(arr, 0);
54             } catch (Exception JavaDoc e) {
55                 return dim;
56             }
57         }
58         return dim;
59     }
60
61     public int flatten(Object JavaDoc a_shaped, int[] a_dimensions, int a_currentDimension, Object JavaDoc[] a_flat,
62             int a_flatElement) {
63                 if (a_currentDimension == (a_dimensions.length - 1)) {
64                     for (int i = 0; i < a_dimensions[a_currentDimension]; i++) {
65                         a_flat[a_flatElement++] = getNoExceptions(a_shaped, i);
66                     }
67                 } else {
68                     for (int i = 0; i < a_dimensions[a_currentDimension]; i++) {
69                         a_flatElement =
70                             flatten(
71                                 getNoExceptions(a_shaped, i),
72                                 a_dimensions,
73                                 a_currentDimension + 1,
74                                 a_flat,
75                                 a_flatElement);
76                     }
77                 }
78                 return a_flatElement;
79             }
80
81     public Object JavaDoc get(Object JavaDoc onArray, int index) {
82         return Array.get(onArray, index);
83     }
84
85     public ReflectClass getComponentType(ReflectClass a_class) {
86         while (a_class.isArray()) {
87             a_class = a_class.getComponentType();
88         }
89         return a_class;
90     }
91
92     public int getLength(Object JavaDoc array) {
93         return Array.getLength(array);
94     }
95
96     private final Object JavaDoc getNoExceptions(Object JavaDoc onArray, int index) {
97         try {
98             return get(onArray, index);
99         } catch (Exception JavaDoc e) {
100             return null;
101         }
102     }
103
104     public boolean isNDimensional(ReflectClass a_class) {
105         return a_class.getComponentType().isArray();
106     }
107
108     public void set(Object JavaDoc onArray, int index, Object JavaDoc element) {
109         if(element == null){
110             try{
111                 Array.set(onArray, index, element);
112             }catch(Exception JavaDoc e){
113                 // This can happen on primitive arrays
114
// and we are fine with ignoring it.
115
// TODO: check if it's a primitive array first and don't ignore exceptions
116
}
117             
118         }else{
119             Array.set(onArray, index, element);
120         }
121     }
122
123     public int shape(Object JavaDoc[] a_flat, int a_flatElement, Object JavaDoc a_shaped, int[] a_dimensions,
124             int a_currentDimension) {
125                 if (a_currentDimension == (a_dimensions.length - 1)) {
126                     for (int i = 0; i < a_dimensions[a_currentDimension]; i++) {
127                         set(a_shaped, i, a_flat[a_flatElement++]);
128                     }
129                 } else {
130                     for (int i = 0; i < a_dimensions[a_currentDimension]; i++) {
131                         a_flatElement =
132                             shape(
133                                 a_flat,
134                                 a_flatElement,
135                                 get(a_shaped, i),
136                                 a_dimensions,
137                                 a_currentDimension + 1);
138                     }
139                 }
140                 return a_flatElement;
141             }
142
143 }
144
Popular Tags