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:org.eclipse.dataset.AbstractDataset.java
/** * Fill dataset from object at depth dimension * @param obj/*from w w w . jav a2s .c om*/ * @param depth * @param pos position */ protected void fillData(Object obj, final int depth, final int[] pos) { if (obj == null) { int dtype = getDtype(); if (dtype == FLOAT32) set(Float.NaN, pos); else if (dtype == FLOAT64) set(Double.NaN, pos); return; } if (obj instanceof List<?>) { List<?> jl = (List<?>) obj; int l = jl.size(); for (int i = 0; i < l; i++) { Object lo = jl.get(i); fillData(lo, depth + 1, pos); pos[depth]++; } pos[depth] = 0; } else if (obj.getClass().isArray()) { int l = Array.getLength(obj); for (int i = 0; i < l; i++) { Object lo = Array.get(obj, i); fillData(lo, depth + 1, pos); pos[depth]++; } pos[depth] = 0; } else if (obj instanceof IDataset) { boolean[] a = new boolean[shape.length]; for (int i = depth; i < a.length; i++) a[i] = true; setSlice(obj, getSliceIteratorFromAxes(pos, a)); } else { set(obj, pos); } }
From source file:org.apache.felix.webconsole.internal.compendium.ConfigManager.java
private Dictionary mergeWithMetaType(Dictionary propsDictionary, ObjectClassDefinition classDef, JSONWriter jsonWriter) throws JSONException { if (propsDictionary == null) { propsDictionary = new Hashtable(); }//from w w w. j a va 2s. com if (classDef != null) { jsonWriter.key("title"); jsonWriter.value(classDef.getName()); if (classDef.getDescription() != null) { jsonWriter.key("description"); jsonWriter.value(classDef.getDescription()); } AttributeDefinition[] currAttrDef = classDef.getAttributeDefinitions(ObjectClassDefinition.ALL); if (currAttrDef != null) { JSONArray propertyList = new JSONArray(); for (int i = 0; i < currAttrDef.length; i++) { jsonWriter.key(currAttrDef[i].getID()); jsonWriter.object(); Object idValue = propsDictionary.get(currAttrDef[i].getID()); if (idValue == null) { idValue = currAttrDef[i].getDefaultValue(); if (idValue == null) { if (currAttrDef[i].getCardinality() == 0) { idValue = ""; } else { idValue = new String[0]; } } } jsonWriter.key("name"); jsonWriter.value(currAttrDef[i].getName()); jsonWriter.key("type"); if (currAttrDef[i].getOptionLabels() != null && currAttrDef[i].getOptionLabels().length > 0) { jsonWriter.object(); jsonWriter.key("labels"); jsonWriter.value(Arrays.asList(currAttrDef[i].getOptionLabels())); jsonWriter.key("values"); jsonWriter.value(Arrays.asList(currAttrDef[i].getOptionValues())); jsonWriter.endObject(); } else { jsonWriter.value(currAttrDef[i].getType()); } if (currAttrDef[i].getCardinality() == 0) { // scalar if (idValue instanceof Vector) { idValue = ((Vector) idValue).get(0); } else if (idValue.getClass().isArray()) { idValue = Array.get(idValue, 0); } jsonWriter.key("value"); jsonWriter.value(idValue); } else { if (idValue instanceof Vector) { idValue = new JSONArray((Vector) idValue); } else if (idValue.getClass().isArray()) { idValue = new JSONArray(Arrays.asList((Object[]) idValue)); } else { JSONArray tmp = new JSONArray(); tmp.put(idValue); idValue = tmp; } jsonWriter.key("values"); jsonWriter.value(idValue); } if (currAttrDef[i].getDescription() != null) { jsonWriter.key("description"); jsonWriter.value(currAttrDef[i].getDescription()); } jsonWriter.endObject(); propertyList.put(currAttrDef[i].getID()); } jsonWriter.key("propertylist"); jsonWriter.value(propertyList); } // nothing more to display propsDictionary = null; } return propsDictionary; }
From source file:ArrayUtils.java
/** * A utility version of equals that allows comparison of null objects and * arrays. WARNING: Does not account for an array that contains a reference * to itself, directly or indirectly/*from w w w. j a v a2 s .c om*/ * * @param o1 * The first object to compare * @param o2 * The second object to compare * @return true if <code>o1</code> and <code>o2</code> are arrays and their * elements are equivalent or if either * <code>o1==null && o2==null</code> or <code>o1.equals(o2)</code>, * false otherwise */ public static boolean equals(Object o1, Object o2) { if (o1 == null) return o2 == null; if (o2 == null) return false; if (o1 instanceof Object[] && o2 instanceof Object[]) return equals((Object[]) o1, (Object[]) o2); if (o1.getClass().isArray() && o2.getClass().isArray()) { if (!o1.getClass().equals(o2.getClass())) return false; int len = Array.getLength(o1); if (len != Array.getLength(o2)) return false; for (int i = 0; i < len; i++) { if (!equals(Array.get(o1, i), Array.get(o2, i))) return false; } return true; } return o1.equals(o2); }
From source file:cn.remex.core.util.ObjectUtils.java
/** * Convert the given array (which may be a primitive array) to an * object array (if necessary of primitive wrapper objects). * <p>A <code>null</code> source value will be converted to an * empty Object array./*from w w w . j av a2s . c om*/ * @param source the (potentially primitive) array * @return the corresponding object array (never <code>null</code>) * @throws IllegalArgumentException if the parameter is not an array */ public static Object[] toObjectArray(final Object source) { if (source instanceof Object[]) { return (Object[]) source; } if (source == null) { return new Object[0]; } if (!source.getClass().isArray()) { throw new IllegalArgumentException("Source is not an array: " + source); } int length = Array.getLength(source); if (length == 0) { return new Object[0]; } Class<?> wrapperType = Array.get(source, 0).getClass(); Object[] newArray = (Object[]) Array.newInstance(wrapperType, length); for (int i = 0; i < length; i++) { newArray[i] = Array.get(source, i); } return newArray; }
From source file:org.eclipse.dataset.AbstractDataset.java
protected static double toReal(final Object b) { if (b instanceof Number) { return ((Number) b).doubleValue(); } else if (b instanceof Boolean) { return ((Boolean) b).booleanValue() ? 1 : 0; } else if (b instanceof Complex) { return ((Complex) b).getReal(); } else if (b.getClass().isArray()) { if (Array.getLength(b) == 0) return 0; return toReal(Array.get(b, 0)); } else if (b instanceof Dataset) { Dataset db = (Dataset) b;/* ww w.j a v a2 s.c o m*/ if (db.getSize() != 1) { logger.error("Given dataset must have only one item"); throw new IllegalArgumentException("Given dataset must have only one item"); } return toReal(db.getObjectAbs(0)); } else if (b instanceof IDataset) { IDataset db = (Dataset) b; if (db.getSize() != 1) { logger.error("Given dataset must have only one item"); throw new IllegalArgumentException("Given dataset must have only one item"); } return toReal(db.getObject(new int[db.getRank()])); } else { logger.error("Argument is of unsupported class"); throw new IllegalArgumentException("Argument is of unsupported class"); } }
From source file:jef.tools.ArrayUtils.java
/** * ?:get/*w ww. j a va 2s .c o m*/ * * @param obj * * @param index * ?? ??-1?-2 */ public final static Object get(Object obj, int index) { if (index >= 0) { return Array.get(obj, index); } else { return Array.get(obj, Array.getLength(obj) + index); } }
From source file:org.eclipse.dataset.AbstractDataset.java
protected static double toImag(final Object b) { if (b instanceof Number) { return 0; } else if (b instanceof Boolean) { return 0; } else if (b instanceof Complex) { return ((Complex) b).getImaginary(); } else if (b.getClass().isArray()) { if (Array.getLength(b) < 2) return 0; return toReal(Array.get(b, 1)); } else if (b instanceof Dataset) { Dataset db = (Dataset) b;//ww w . j av a2s.c o m if (db.getSize() != 1) { logger.error("Given dataset must have only one item"); throw new IllegalArgumentException("Given dataset must have only one item"); } return toImag(db.getObjectAbs(0)); } else if (b instanceof IDataset) { IDataset db = (Dataset) b; if (db.getSize() != 1) { logger.error("Given dataset must have only one item"); throw new IllegalArgumentException("Given dataset must have only one item"); } return toImag(db.getObject(new int[db.getRank()])); } else { logger.error("Argument is of unsupported class"); throw new IllegalArgumentException("Argument is of unsupported class"); } }
From source file:com.github.helenusdriver.driver.tools.Tool.java
/** * Gets the initial objects to insert using the specified initial method and * suffixes// ww w. java2 s . co m * * @author paouelle * * @param initial a non-<code>null</code> initial method to retreive objects with * @param suffixes the non-<code>null</code> map of suffixes configured * @return the initial objects to insert in the table or <code>null</code> * if none needs to be inserted */ private static List<Object> getInitialObjects(Method initial, Map<String, String> suffixes) { try { final Object array = initial.invoke(null, suffixes); if (array == null) { return Collections.emptyList(); } final int length = Array.getLength(array); final List<Object> objects = new ArrayList<>(length); for (int i = 0; i < length; i++) { objects.add(Array.get(array, i)); } return objects; } catch (IllegalAccessException e) { // should not happen throw new IllegalStateException(e); } catch (InvocationTargetException e) { final Throwable t = e.getTargetException(); if (t instanceof Error) { throw (Error) t; } else if (t instanceof RuntimeException) { throw (RuntimeException) t; } else { // we don't expect any of those throw new IllegalStateException(t); } } }
From source file:demo.config.PropertyConverter.java
/** * Returns a collection with all values contained in the specified object. * This method is used for instance by the {@code addProperty()} * implementation of the default configurations to gather all values of the * property to add. Depending on the type of the passed in object the * following things happen://from w w w . j a v a2 s.c o m * <ul> * <li>Strings are checked for delimiter characters and split if necessary.</li> * <li>For objects implementing the {@code Iterable} interface, the * corresponding {@code Iterator} is obtained, and contained elements are * added to the resulting collection.</li> * <li>Arrays are treated as {@code Iterable} objects.</li> * <li>All other types are directly inserted.</li> * <li>Recursive combinations are supported, e.g. a collection containing an * array that contains strings: The resulting collection will only contain * primitive objects (hence the name "flatten").</li> * </ul> * * @param value * the value to be processed * @param delimiter * the delimiter for String values * @return a "flat" collection containing all primitive values of * the passed in object */ private static Collection<?> flatten(Object value, char delimiter) { if (value instanceof String) { String s = (String) value; if (s.indexOf(delimiter) > -1) { return split(s, delimiter); } } Collection<Object> result = new LinkedList<Object>(); if (value instanceof Iterable) { flattenIterator(result, ((Iterable<?>) value).iterator(), delimiter); } else if (value instanceof Iterator) { flattenIterator(result, (Iterator<?>) value, delimiter); } else if (value != null) { if (value.getClass().isArray()) { for (int len = Array.getLength(value), idx = 0; idx < len; idx++) { result.addAll(flatten(Array.get(value, idx), delimiter)); } } else { result.add(value); } } return result; }
From source file:lodsve.core.config.properties.PropertyConverter.java
/** * Returns a collection with all values contained in the specified object. * This method is used for instance by the {@code addProperty()} * implementation of the default configurations to gather all values of the * property to add. Depending on the type of the passed in object the * following things happen:/*from w w w .j a v a 2s . c o m*/ * <ul> * <li>Strings are checked for delimiter characters and split if necessary.</li> * <li>For objects implementing the {@code Iterable} interface, the * corresponding {@code Iterator} is obtained, and contained elements are * added to the resulting collection.</li> * <li>Arrays are treated as {@code Iterable} objects.</li> * <li>All other types are directly inserted.</li> * <li>Recursive combinations are supported, e.g. a collection containing an * array that contains strings: The resulting collection will only contain * primitive objects (hence the name "flatten").</li> * </ul> * * @param value * the value to be processed * @param delimiter * the delimiter for String values * @return a "flat" collection containing all primitive values of * the passed in object */ private static Collection<?> flatten(Object value, char delimiter) { if (value instanceof String) { String s = (String) value; if (s.indexOf(delimiter) > 0) { return split(s, delimiter); } } Collection<Object> result = new LinkedList<Object>(); if (value instanceof Iterable) { flattenIterator(result, ((Iterable<?>) value).iterator(), delimiter); } else if (value instanceof Iterator) { flattenIterator(result, (Iterator<?>) value, delimiter); } else if (value != null) { if (value.getClass().isArray()) { for (int len = Array.getLength(value), idx = 0; idx < len; idx++) { result.addAll(flatten(Array.get(value, idx), delimiter)); } } else { result.add(value); } } return result; }