| 1 21 package com.db4o.reflect.core; 22 23 import java.lang.reflect.Array ; 24 25 import com.db4o.reflect.*; 26 27 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 newInstance(ReflectClass componentType, int[] dimensions); 39 40 public abstract Object newInstance(ReflectClass componentType, int length); 41 42 public int[] dimensions(Object 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 e) { 55 return dim; 56 } 57 } 58 return dim; 59 } 60 61 public int flatten(Object a_shaped, int[] a_dimensions, int a_currentDimension, Object [] 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 get(Object 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 array) { 93 return Array.getLength(array); 94 } 95 96 private final Object getNoExceptions(Object onArray, int index) { 97 try { 98 return get(onArray, index); 99 } catch (Exception 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 onArray, int index, Object element) { 109 if(element == null){ 110 try{ 111 Array.set(onArray, index, element); 112 }catch(Exception e){ 113 } 117 118 }else{ 119 Array.set(onArray, index, element); 120 } 121 } 122 123 public int shape(Object [] a_flat, int a_flatElement, Object 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 |