KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > reflect > self > SelfArray


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.self;
22
23 import com.db4o.reflect.*;
24
25 public class SelfArray implements ReflectArray {
26     
27     private final SelfReflectionRegistry _registry;
28
29     SelfArray(Reflector reflector,SelfReflectionRegistry registry) {
30         _registry=registry;
31     }
32
33     public int[] dimensions(Object JavaDoc arr) {
34         return new int[]{getLength(arr)};
35     }
36
37     public int flatten(Object JavaDoc a_shaped, int[] a_dimensions,
38             int a_currentDimension, Object JavaDoc[] a_flat, int a_flatElement) {
39         if(a_shaped instanceof Object JavaDoc[]) {
40             Object JavaDoc[] shaped=(Object JavaDoc[])a_shaped;
41             System.arraycopy(shaped, 0, a_flat, 0, shaped.length);
42             return shaped.length;
43         }
44         return _registry.flattenArray(a_shaped,a_flat);
45     }
46
47     public Object JavaDoc get(Object JavaDoc onArray, int index) {
48         if(onArray instanceof Object JavaDoc[]) {
49             return ((Object JavaDoc[])onArray)[index];
50         }
51         return _registry.getArray(onArray,index);
52     }
53
54     public ReflectClass getComponentType(ReflectClass a_class) {
55         return ((SelfClass)a_class).getComponentType();
56     }
57
58     public int getLength(Object JavaDoc array) {
59         if(array instanceof Object JavaDoc[]) {
60             return ((Object JavaDoc[])array).length;
61         }
62         return _registry.arrayLength(array);
63     }
64
65     public boolean isNDimensional(ReflectClass a_class) {
66         return false;
67     }
68
69     public Object JavaDoc newInstance(ReflectClass componentType, int length) {
70         return _registry.arrayFor(((SelfClass)componentType).getJavaClass(),length);
71     }
72
73     public Object JavaDoc newInstance(ReflectClass componentType, int[] dimensions) {
74         return newInstance(componentType,dimensions[0]);
75     }
76
77     public void set(Object JavaDoc onArray, int index, Object JavaDoc element) {
78         if(onArray instanceof Object JavaDoc[]) {
79             ((Object JavaDoc[])onArray)[index]=element;
80             return;
81         }
82         _registry.setArray(onArray,index,element);
83     }
84
85     public int shape(Object JavaDoc[] a_flat, int a_flatElement, Object JavaDoc a_shaped,
86             int[] a_dimensions, int a_currentDimension) {
87         if(a_shaped instanceof Object JavaDoc[]) {
88             Object JavaDoc[] shaped=(Object JavaDoc[])a_shaped;
89             System.arraycopy(a_flat, 0, shaped, 0, a_flat.length);
90             return a_flat.length;
91         }
92         return _registry.shapeArray(a_flat,a_shaped);
93     }
94
95 }
96
Popular Tags