01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: 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: /** @param reflector */
30: SelfArray(Reflector reflector, SelfReflectionRegistry registry) {
31: _registry = registry;
32: }
33:
34: public int[] dimensions(Object arr) {
35: return new int[] { getLength(arr) };
36: }
37:
38: public int flatten(Object a_shaped, int[] a_dimensions,
39: int a_currentDimension, Object[] a_flat, int a_flatElement) {
40: if (a_shaped instanceof Object[]) {
41: Object[] shaped = (Object[]) a_shaped;
42: System.arraycopy(shaped, 0, a_flat, 0, shaped.length);
43: return shaped.length;
44: }
45: return _registry.flattenArray(a_shaped, a_flat);
46: }
47:
48: public Object get(Object onArray, int index) {
49: if (onArray instanceof Object[]) {
50: return ((Object[]) onArray)[index];
51: }
52: return _registry.getArray(onArray, index);
53: }
54:
55: public ReflectClass getComponentType(ReflectClass a_class) {
56: return ((SelfClass) a_class).getComponentType();
57: }
58:
59: public int getLength(Object array) {
60: if (array instanceof Object[]) {
61: return ((Object[]) array).length;
62: }
63: return _registry.arrayLength(array);
64: }
65:
66: public boolean isNDimensional(ReflectClass a_class) {
67: return false;
68: }
69:
70: public Object newInstance(ReflectClass componentType, int length) {
71: return _registry.arrayFor(((SelfClass) componentType)
72: .getJavaClass(), length);
73: }
74:
75: public Object newInstance(ReflectClass componentType,
76: int[] dimensions) {
77: return newInstance(componentType, dimensions[0]);
78: }
79:
80: public void set(Object onArray, int index, Object element) {
81: if (onArray instanceof Object[]) {
82: ((Object[]) onArray)[index] = element;
83: return;
84: }
85: _registry.setArray(onArray, index, element);
86: }
87:
88: public int shape(Object[] a_flat, int a_flatElement,
89: Object a_shaped, int[] a_dimensions, int a_currentDimension) {
90: if (a_shaped instanceof Object[]) {
91: Object[] shaped = (Object[]) a_shaped;
92: System.arraycopy(a_flat, 0, shaped, 0, a_flat.length);
93: return a_flat.length;
94: }
95: return _registry.shapeArray(a_flat, a_shaped);
96: }
97:
98: }
|