List of usage examples for java.lang.reflect Array get
public static native Object get(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
From source file:edu.brown.utils.PartitionEstimator.java
/** * Calculate the partitions touched for the given column * /* www.j ava 2 s .c om*/ * @param partitions * @param params * @param predicates * @param catalog_col */ private void calculatePartitions(final PartitionSet partitions, final Object params[], final boolean is_array[], final List<Pair<ExpressionType, CatalogType>> predicates, final Column catalog_col) throws Exception { // Note that we have to go through all of the mappings from the partitioning column // to parameters. This can occur when the partitioning column is referenced multiple times // This allows us to handle complex WHERE clauses and what not. for (Pair<ExpressionType, CatalogType> pair : predicates) { ExpressionType expType = pair.getFirst(); CatalogType param = pair.getSecond(); // HACK HACK HACK // If this is not an equality comparison, then it has to go to all partitions. // If we ever want to support smarter range partitioning, then // we will need to move the logic that examines the expression type into // the hasher code. if (expType != ExpressionType.COMPARE_EQUAL) { partitions.addAll(this.all_partitions); break; } // STATEMENT PARAMETER // This is the common case if (param instanceof StmtParameter) { int param_idx = ((StmtParameter) param).getIndex(); // IMPORTANT: Check if the parameter is an array. If it is, then we // have to loop through and get the hash of all of the values if (is_array[param_idx]) { int num_elements = Array.getLength(params[param_idx]); if (trace.val) LOG.trace(String.format("%s is an array. Calculating multiple partitions", param)); for (int i = 0; i < num_elements; i++) { Object value = Array.get(params[param_idx], i); int partition_id = this.hasher.hash(value, catalog_col); if (trace.val) LOG.trace(String.format("%s HASHING PARAM ARRAY[%d][%d]: %s -> %d", catalog_col.fullName(), param_idx, i, value, partition_id)); partitions.add(partition_id); } // FOR } // Primitive Value else { int partition_id = this.hasher.hash(params[param_idx], catalog_col); if (trace.val) LOG.trace(String.format("%s HASHING PARAM [%d]: %s -> %d", catalog_col.fullName(), param_idx, params[param_idx], partition_id)); partitions.add(partition_id); } } // CONSTANT VALUE // This is more rare else if (param instanceof ConstantValue) { ConstantValue const_param = (ConstantValue) param; VoltType vtype = VoltType.get(const_param.getType()); Object const_value = VoltTypeUtil.getObjectFromString(vtype, const_param.getValue()); int partition_id = this.hasher.hash(const_value); partitions.add(partition_id); } // BUSTED! else { throw new RuntimeException("Unexpected parameter type: " + param.fullName()); } } // FOR return; }
From source file:edu.brown.utils.PartitionEstimator.java
/** * Return the partition touched for a given procedure's parameter value. * If the given parameter is an array, then we will just use the first element. * @param catalog_proc/* w w w . j a v a2 s . c o m*/ * @param partition_param_val * @param is_array Whether the value is an array. * @return * @throws Exception */ private int calculatePartition(final Procedure catalog_proc, Object param_val, final boolean is_array) throws Exception { // If the parameter is an array, then just use the first value if (is_array) { int num_elements = Array.getLength(param_val); if (num_elements == 0) { if (debug.val) LOG.warn("Empty partitioning parameter array for " + catalog_proc); return (HStoreConstants.NULL_PARTITION_ID); } else { param_val = Array.get(param_val, 0); } } else if (param_val == null) { if (debug.val) LOG.warn("Null ProcParameter value: " + catalog_proc); return (HStoreConstants.NULL_PARTITION_ID); } return (this.hasher.hash(param_val, catalog_proc)); }
From source file:jef.tools.XMLUtils.java
private static Element appendBean(Node parent, Object bean, Class<?> type, Boolean asAttrib, String tagName) { if (type == null) { if (bean == null) { return null; }/*from w w w . j a va 2 s .c om*/ type = bean.getClass(); } if (tagName == null || tagName.length() == 0) { tagName = type.getSimpleName(); } if (type.isArray()) { if (bean == null) return null; Element collection = addElement(parent, tagName); for (int i = 0; i < Array.getLength(bean); i++) { appendBean(collection, Array.get(bean, i), null, asAttrib, null); } return collection; } else if (Collection.class.isAssignableFrom(type)) { if (bean == null) return null; Element collection = addElement(parent, tagName); for (Object obj : (Collection<?>) bean) { appendBean(collection, obj, null, asAttrib, null); } return collection; } else if (Map.class.isAssignableFrom(type)) { Element map = addElement(parent, tagName); for (Entry<?, ?> e : ((Map<?, ?>) bean).entrySet()) { Element entry = XMLUtils.addElement(map, "entry"); Element key = XMLUtils.addElement(entry, "key"); appendBean(key, e.getKey(), null, asAttrib, null); Element value = XMLUtils.addElement(entry, "value"); appendBean(value, e.getValue(), null, asAttrib, null); } return map; } else if (CharSequence.class.isAssignableFrom(type)) { if (Boolean.TRUE.equals(asAttrib)) { ((Element) parent).setAttribute(tagName, StringUtils.toString(bean)); } else { addElement(parent, tagName, StringUtils.toString(bean)); } } else if (Date.class.isAssignableFrom(type)) { if (Boolean.FALSE.equals(asAttrib)) { addElement(parent, tagName, DateUtils.formatDateTime((Date) bean)); } else { ((Element) parent).setAttribute(tagName, DateUtils.formatDateTime((Date) bean)); } } else if (Number.class.isAssignableFrom(type) || type.isPrimitive() || type == Boolean.class) { if (Boolean.FALSE.equals(asAttrib)) { addElement(parent, tagName, StringUtils.toString(bean)); } else { ((Element) parent).setAttribute(tagName, StringUtils.toString(bean)); } } else { if (bean == null) return null; Element root = addElement(parent, type.getSimpleName()); BeanWrapper bw = BeanWrapper.wrap(bean); for (Property p : bw.getProperties()) { appendBean(root, p.get(bean), p.getType(), asAttrib, p.getName()); } return root; } return null; }
From source file:org.kchine.r.server.DirectJNI.java
public static Object getJavaArrayFromRArray__(RArray array) { int[] dim = array.getDim(); RVector vector = array.getValue();/* ww w. j av a 2 s .c o m*/ Class<?> componentType = null; if (vector instanceof RInteger) componentType = int.class; else if (vector instanceof RNumeric) componentType = double.class; else if (vector instanceof RChar) componentType = String.class; else if (vector instanceof RLogical) componentType = boolean.class; Object result = null; try { result = Array.newInstance(componentType, dim); } catch (Exception e) { e.printStackTrace(); } Vector<Integer> v = new Vector<Integer>(); int p = 1; for (int i = dim.length - 1; i > 0; --i) { p = p * dim[i]; v.add(0, p); } for (int bi = 0; bi < p * dim[0]; ++bi) { int bindex = bi; int[] indexes = new int[dim.length]; for (int i = 0; i < indexes.length - 1; ++i) { indexes[i] = bindex / v.elementAt(i); bindex = bindex % v.elementAt(i); } indexes[indexes.length - 1] = bindex; Object arrayTail = null; if (dim.length == 1) { arrayTail = result; } else { arrayTail = Array.get(result, indexes[0]); for (int i = 1; i < indexes.length - 1; ++i) arrayTail = Array.get(arrayTail, indexes[i]); } if (vector instanceof RInteger) Array.setInt(arrayTail, indexes[indexes.length - 1], ((RInteger) vector).getValue()[bi]); else if (vector instanceof RNumeric) Array.setDouble(arrayTail, indexes[indexes.length - 1], ((RNumeric) vector).getValue()[bi]); else if (vector instanceof RChar) Array.set(arrayTail, indexes[indexes.length - 1], ((RChar) vector).getValue()[bi]); else if (vector instanceof RLogical) Array.setBoolean(arrayTail, indexes[indexes.length - 1], ((RLogical) vector).getValue()[bi]); } return result; }
From source file:org.kchine.r.server.DirectJNI.java
public static Object getJavaArrayFromRArray(RArray array) { int[] dim = array.getDim(); RVector vector = array.getValue();//from ww w. j a va2 s .c o m Class<?> componentType = null; if (vector instanceof RInteger) componentType = int.class; else if (vector instanceof RNumeric) componentType = double.class; else if (vector instanceof RChar) componentType = String.class; else if (vector instanceof RLogical) componentType = boolean.class; Object result = null; try { result = Array.newInstance(componentType, dim); } catch (Exception e) { e.printStackTrace(); } Vector<Integer> v1 = new Vector<Integer>(); int p1 = 1; for (int i = dim.length - 1; i > 0; --i) { p1 = p1 * dim[i]; v1.add(0, p1); } Vector<Integer> v2 = new Vector<Integer>(); int p2 = 1; for (int i = 0; i < dim.length - 1; ++i) { p2 = p2 * dim[i]; v2.add(0, p2); } for (int bi = 0; bi < p1 * dim[0]; ++bi) { int bindex = bi; int[] indexes = new int[dim.length]; for (int i = 0; i < indexes.length - 1; ++i) { indexes[i] = bindex / v1.elementAt(i); bindex = bindex % v1.elementAt(i); } indexes[indexes.length - 1] = bindex; Object arrayTail = null; if (dim.length == 1) { arrayTail = result; } else { arrayTail = Array.get(result, indexes[0]); for (int i = 1; i < indexes.length - 1; ++i) arrayTail = Array.get(arrayTail, indexes[i]); } int linearVectorIndex = 0; for (int i = (indexes.length - 1); i > 0; --i) linearVectorIndex += indexes[i] * v2.elementAt((indexes.length - 1) - i); linearVectorIndex += indexes[0]; // System.out.println("linearVectorIndex:"+linearVectorIndex); if (vector instanceof RInteger) Array.setInt(arrayTail, indexes[indexes.length - 1], ((RInteger) vector).getValue()[linearVectorIndex]); else if (vector instanceof RNumeric) Array.setDouble(arrayTail, indexes[indexes.length - 1], ((RNumeric) vector).getValue()[linearVectorIndex]); else if (vector instanceof RChar) Array.set(arrayTail, indexes[indexes.length - 1], ((RChar) vector).getValue()[linearVectorIndex]); else if (vector instanceof RLogical) Array.setBoolean(arrayTail, indexes[indexes.length - 1], ((RLogical) vector).getValue()[linearVectorIndex]); } return result; }
From source file:org.kchine.r.server.DirectJNI.java
public static int[] getJavaArrayDimensions(Object table, Class<?>[] classHolder, int[] lengthHolder) { Vector<Integer> dimV = new Vector<Integer>(); Object obj = table;//www.ja va2s. co m while (Array.get(obj, 0).getClass().isArray()) { dimV.add(Array.getLength(obj)); obj = Array.get(obj, 0); } dimV.add(Array.getLength(obj)); classHolder[0] = Array.get(obj, 0).getClass(); int[] result = new int[dimV.size()]; lengthHolder[0] = 1; for (int i = 0; i < dimV.size(); ++i) { result[i] = dimV.elementAt(i); lengthHolder[0] = lengthHolder[0] * result[i]; } return result; }
From source file:org.kchine.r.server.DirectJNI.java
public static RArray getRArrayFromJavaArray(Object javaArray) { Class<?>[] classHolder = new Class<?>[1]; int[] lengthHolder = new int[1]; int[] dim = getJavaArrayDimensions(javaArray, classHolder, lengthHolder); RVector vector = null;// w w w. j a v a 2 s. c o m Class<?> componentType = classHolder[0]; if (componentType == Integer.class || componentType == int.class) vector = new RInteger(new int[lengthHolder[0]]); else if (componentType == Double.class || componentType == double.class) vector = new RNumeric(new double[lengthHolder[0]]); else if (componentType == Boolean.class || componentType == boolean.class) vector = new RLogical(new boolean[lengthHolder[0]]); else if (componentType == String.class) vector = new RChar(new String[lengthHolder[0]]); else throw new RuntimeException("unsupported elements class type :" + componentType); Vector<Integer> v1 = new Vector<Integer>(); int p1 = 1; for (int i = dim.length - 1; i > 0; --i) { p1 = p1 * dim[i]; v1.add(0, p1); } Vector<Integer> v2 = new Vector<Integer>(); int p2 = 1; for (int i = 0; i < dim.length - 1; ++i) { p2 = p2 * dim[i]; v2.add(0, p2); } for (int bi = 0; bi < p1 * dim[0]; ++bi) { int bindex = bi; int[] indexes = new int[dim.length]; for (int i = 0; i < indexes.length - 1; ++i) { indexes[i] = bindex / v1.elementAt(i); bindex = bindex % v1.elementAt(i); } indexes[indexes.length - 1] = bindex; Object arrayTail = null; if (dim.length == 1) { arrayTail = javaArray; } else { arrayTail = Array.get(javaArray, indexes[0]); for (int i = 1; i < indexes.length - 1; ++i) arrayTail = Array.get(arrayTail, indexes[i]); } int linearVectorIndex = 0; for (int i = (indexes.length - 1); i > 0; --i) linearVectorIndex += indexes[i] * v2.elementAt((indexes.length - 1) - i); linearVectorIndex += indexes[0]; // System.out.println("linearVectorIndex:"+linearVectorIndex); if (vector instanceof RInteger) ((RInteger) vector).getValue()[linearVectorIndex] = (Integer) Array.get(arrayTail, indexes[indexes.length - 1]); else if (vector instanceof RNumeric) ((RNumeric) vector).getValue()[linearVectorIndex] = (Double) Array.get(arrayTail, indexes[indexes.length - 1]); else if (vector instanceof RChar) ((RChar) vector).getValue()[linearVectorIndex] = (String) Array.get(arrayTail, indexes[indexes.length - 1]); else if (vector instanceof RLogical) ((RLogical) vector).getValue()[linearVectorIndex] = (Boolean) Array.get(arrayTail, indexes[indexes.length - 1]); } return new RArray(vector, dim, null); }
From source file:org.openTwoFactor.client.util.TwoFactorClientCommonUtils.java
/** * Get a specific index of an array or collection (note for collections and * iterating, it is more efficient to get an iterator and iterate * // w w w . j a v a 2 s.c om * @param arrayOrCollection * @param index * @return the object at that index */ public static Object get(Object arrayOrCollection, int index) { if (arrayOrCollection == null) { if (index == 0) { return null; } throw new RuntimeException("Trying to access index " + index + " of null"); } // no need to iterator on list (e.g. FastProxyList has no iterator if (arrayOrCollection instanceof List) { return ((List) arrayOrCollection).get(index); } if (arrayOrCollection instanceof Collection) { Iterator iterator = iterator(arrayOrCollection); for (int i = 0; i < index; i++) { next(arrayOrCollection, iterator, i); } return next(arrayOrCollection, iterator, index); } if (arrayOrCollection.getClass().isArray()) { return Array.get(arrayOrCollection, index); } if (index == 0) { return arrayOrCollection; } throw new RuntimeException("Trying to access index " + index + " of and object: " + arrayOrCollection); }
From source file:com.processing.core.PApplet.java
static public Object reverse(Object list) { Class<?> type = list.getClass().getComponentType(); int length = Array.getLength(list); Object outgoing = Array.newInstance(type, length); for (int i = 0; i < length; i++) { Array.set(outgoing, i, Array.get(list, (length - 1) - i)); }/*ww w . j a v a 2 s.c o m*/ return outgoing; }
From source file:ca.oson.json.Oson.java
private int[] json2ArrayInt(FieldData objectDTO) { if (objectDTO.valueToProcess == null) { return null; }/*ww w . j av a2s .co m*/ Function function = objectDTO.getDeserializer(); Object returnedValue = objectDTO.valueToProcess; if (function != null) { try { // suppose to return String, but in case not, try to process if (function instanceof Json2DataMapperFunction) { DataMapper classData = new DataMapper(objectDTO.returnType, objectDTO.valueToProcess, objectDTO.classMapper, objectDTO.level, getPrettyIndentation()); returnedValue = ((Json2DataMapperFunction) function).apply(classData); } else if (function instanceof Json2FieldDataFunction) { Json2FieldDataFunction f = (Json2FieldDataFunction) function; FieldData fieldData = objectDTO.clone(); returnedValue = f.apply(fieldData); } else if (function instanceof Json2ArrayFunction) { returnedValue = ((Json2ArrayFunction) function).apply(objectDTO.valueToProcess); } else { returnedValue = function.apply(objectDTO.valueToProcess); } } catch (Exception e) { e.printStackTrace(); } } if (returnedValue == null) { return null; } else if (returnedValue.getClass().isArray()) { if (returnedValue.getClass() == int[].class) { return (int[]) returnedValue; } int size = Array.getLength(returnedValue); int[] arr = new int[size]; int i = 0, j = 0; while (j < size) { try { arr[i] = (int) NumberUtil.getNumber(Array.get(returnedValue, j), int.class); i++; } catch (Exception ex) { } j++; } if (i == size) { return arr; } return Arrays.copyOfRange(arr, 0, i); } else if (!Collection.class.isAssignableFrom(returnedValue.getClass())) { return null; } Collection values = (Collection) returnedValue; int size = values.size(); int[] arr = new int[size]; int i = 0; for (Object value : values) { if (value != null) { try { arr[i] = (int) NumberUtil.getNumber(value, int.class); i++; } catch (Exception ex) { } } } if (i == size) { return arr; } return Arrays.copyOfRange(arr, 0, i); }