1 2 3 package com.db4odoc.f1.reflections; 4 5 import java.lang.reflect.Array ; 6 7 import com.db4o.reflect.ReflectArray; 8 import com.db4o.reflect.ReflectClass; 9 import com.db4o.reflect.Reflector; 10 import com.db4o.reflect.jdk.JdkReflector; 11 12 13 public class LoggingArray implements ReflectArray { 14 15 private final Reflector _reflector; 16 17 LoggingArray(Reflector reflector){ 18 _reflector = reflector; 19 } 20 21 public int[] dimensions(Object arr){ 22 int count = 0; 23 ReflectClass claxx = _reflector.forObject(arr); 24 while (claxx.isArray()) { 25 count++; 26 claxx = claxx.getComponentType(); 27 } 28 int dim[] = new int[count]; 29 for (int i = 0; i < count; i++) { 30 try { 31 dim[i] = getLength(arr); 32 arr = get(arr, 0); 33 } catch (Exception e) { 34 return dim; 35 } 36 } 37 return dim; 38 } 39 40 public int flatten( 41 Object a_shaped, 42 int[] a_dimensions, 43 int a_currentDimension, 44 Object [] a_flat, 45 int a_flatElement) { 46 if (a_currentDimension == (a_dimensions.length - 1)) { 47 for (int i = 0; i < a_dimensions[a_currentDimension]; i++) { 48 a_flat[a_flatElement++] = getNoExceptions(a_shaped, i); 49 } 50 } else { 51 for (int i = 0; i < a_dimensions[a_currentDimension]; i++) { 52 a_flatElement = 53 flatten( 54 getNoExceptions(a_shaped, i), 55 a_dimensions, 56 a_currentDimension + 1, 57 a_flat, 58 a_flatElement); 59 } 60 } 61 return a_flatElement; 62 } 63 64 public Object get(Object onArray, int index) { 65 return Array.get(onArray, index); 66 } 67 68 public ReflectClass getComponentType(ReflectClass a_class) { 69 while (a_class.isArray()) { 70 a_class = a_class.getComponentType(); 71 } 72 return a_class; 73 } 74 75 public int getLength(Object array) { 76 return Array.getLength(array); 77 } 78 79 private final Object getNoExceptions(Object onArray, int index){ 80 try { 81 return get(onArray, index); 82 } catch (Exception e) { 83 return null; 84 } 85 } 86 87 public boolean isNDimensional(ReflectClass a_class) { 88 return a_class.getComponentType().isArray(); 89 } 90 91 public Object newInstance(ReflectClass componentType, int length) { 92 return Array.newInstance(JdkReflector.toNative(componentType), length); 93 } 94 95 public Object newInstance(ReflectClass componentType, int[] dimensions) { 96 return Array.newInstance(JdkReflector.toNative(componentType), dimensions); 97 } 98 99 public void set(Object onArray, int index, Object element) { 100 if(element == null){ 101 try{ 102 Array.set(onArray, index, element); 103 }catch(Exception e){ 104 } 107 108 }else{ 109 Array.set(onArray, index, element); 110 } 111 } 112 113 public int shape( 114 Object [] a_flat, 115 int a_flatElement, 116 Object a_shaped, 117 int[] a_dimensions, 118 int a_currentDimension) { 119 if (a_currentDimension == (a_dimensions.length - 1)) { 120 for (int i = 0; i < a_dimensions[a_currentDimension]; i++) { 121 set(a_shaped, i, a_flat[a_flatElement++]); 122 } 123 } else { 124 for (int i = 0; i < a_dimensions[a_currentDimension]; i++) { 125 a_flatElement = 126 shape( 127 a_flat, 128 a_flatElement, 129 get(a_shaped, i), 130 a_dimensions, 131 a_currentDimension + 1); 132 } 133 } 134 return a_flatElement; 135 } 136 137 138 } 139 140 | Popular Tags |