/** Collect dimensions array of supplied array object. * Returns the integer array used for Array.newInstance. * @param arr to inspect * @return int array of dimensions */ public static int[] dimensions(Object arr) { int[] dims = new int[Types.arrayDimensions(arr.getClass())]; if ( 0 == dims.length || 0 == (dims[0] = Array.getLength(arr)) ) return dims; for ( int i = 1; i < dims.length; i++ ) if ( null != (arr = Array.get(arr, 0)) ) dims[i] = Array.getLength(arr); else break; return dims; }
/** Fill boxed numeric types with default numbers instead of nulls. * @param arr the array to fill. */ private void arrayFillDefaultValue(Object arr) { if (null == arr) return; Class<?> clas = arr.getClass(); Class<?> comp = Types.arrayElementType(clas); if ( !comp.isPrimitive() ) if ( Types.arrayDimensions(clas) > 1 ) for ( int i = 0; i < Array.getLength(arr); i++ ) arrayFillDefaultValue(Array.get(arr, i)); else Arrays.fill((Object[]) arr, Primitive.unwrap( Primitive.getDefaultValue(comp))); } }
/** Concatenate two arrays. * @param lhs 1st array * @param rhs 2nd array * @return a new array instance of concatenated contents. * @throws UtilEvalError for inconsistent dimensions. */ public static Object concat(Object lhs, Object rhs) throws UtilEvalError { Class<?> lhsType = lhs.getClass(); Class<?> rhsType = rhs.getClass(); if ( Types.arrayDimensions(lhsType) != Types.arrayDimensions(rhsType) ) throw new UtilEvalError("Cannot concat arrays with inconsistent dimensions." + " Attempting to concat array of type " + StringUtil.typeString(lhs) + " with array of type " + StringUtil.typeString(rhs) + "."); Class<?> toType = Types.getCommonType( Types.arrayElementType(lhsType), Types.arrayElementType(rhsType)); int[] dims = dimensions(lhs); dims[0] = Array.getLength(lhs) + Array.getLength(rhs); Object toArray = Array.newInstance(toType, dims); copy(toType, toArray, lhs, rhs); return toArray; }
length = Array.getLength(from[0]), total = from.length > 1 ? Array.getLength(to) : length; if ( Types.arrayDimensions(to.getClass()) == 1 ) { for ( int i = 0; i < total; i++ ) { Object value = Array.get(from[f], fi++);
dimensions += Types.arrayDimensions(Types.getType(ot));