List of usage examples for java.lang.reflect Array getLength
@HotSpotIntrinsicCandidate public static native int getLength(Object array) throws IllegalArgumentException;
From source file:net.radai.beanz.codecs.ArrayCodec.java
@Override public String encode(Object object) { if (object == null) { return null; }/*w w w.j ava 2 s . co m*/ if (!ReflectionUtil.isArray(object.getClass())) { throw new IllegalArgumentException(); } int size = Array.getLength(object); if (size == 0) { return "[]"; } StrBuilder sb = new StrBuilder(); sb.append("["); for (int i = 0; i < size; i++) { Object element = Array.get(object, i); String encoded = elementCodec.encode(element); sb.append(encoded).append(", "); } sb.delete(sb.length() - 2, sb.length()); //last ", " sb.append("]"); return sb.toString(); }
From source file:ArrayUtils.java
/** * <p>Returns the length of the specified array. * This method can deal with <code>Object</code> arrays and with primitive arrays.</p> * * <p>If the input array is <code>null</code>, <code>0</code> is returned.</p> * * <pre>/*from w w w .ja v a 2 s . c o m*/ * ArrayUtils.getLength(null) = 0 * ArrayUtils.getLength([]) = 0 * ArrayUtils.getLength([null]) = 1 * ArrayUtils.getLength([true, false]) = 2 * ArrayUtils.getLength([1, 2, 3]) = 3 * ArrayUtils.getLength(["a", "b", "c"]) = 3 * </pre> * * @param array the array to retrieve the length from, may be null * @return The length of the array, or <code>0</code> if the array is <code>null</code> * @throws IllegalArgumentException if the object arguement is not an array. */ public static int getLength(final Object array) { if (array == null) { return 0; } else { return Array.getLength(array); } }
From source file:com.espertech.esper.epl.core.MethodPollingExecStrategy.java
public List<EventBean> poll(Object[] lookupValues) { List<EventBean> rowResult = null; try {//from w w w. j a v a 2 s. c om Object invocationResult = method.invoke(null, lookupValues); if (invocationResult != null) { if (isArray) { int length = Array.getLength(invocationResult); if (length > 0) { rowResult = new ArrayList<EventBean>(); for (int i = 0; i < length; i++) { Object value = Array.get(invocationResult, i); if (value == null) { log.warn("Expected non-null return result from method '" + method.getName() + "', but received null value"); continue; } EventBean theEvent; if (useMapType) { if (!(value instanceof Map)) { log.warn("Expected Map-type return result from method '" + method.getName() + "', but received type '" + value.getClass() + "'"); continue; } Map mapValues = (Map) value; theEvent = eventAdapterService.adapterForTypedMap(mapValues, eventType); } else { theEvent = eventAdapterService.adapterForBean(value); } rowResult.add(theEvent); } } } else { rowResult = new LinkedList<EventBean>(); EventBean theEvent; if (useMapType) { if (!(invocationResult instanceof Map)) { log.warn("Expected Map-type return result from method '" + method.getName() + "', but received type '" + invocationResult.getClass() + "'"); } else { Map mapValues = (Map) invocationResult; theEvent = eventAdapterService.adapterForTypedMap(mapValues, eventType); rowResult.add(theEvent); } } else { theEvent = eventAdapterService.adapterForBean(invocationResult); rowResult.add(theEvent); } } } } catch (InvocationTargetException ex) { throw new EPException("Method '" + method.getName() + "' of class '" + method.getJavaMethod().getDeclaringClass().getName() + "' reported an exception: " + ex.getTargetException(), ex.getTargetException()); } return rowResult; }
From source file:net.sf.morph.util.TestUtils.java
public static boolean equals(Object object1, Object object2) { if (log.isTraceEnabled()) { log.trace("Testing for equality between " + ObjectUtils.getObjectDescription(object1) + " and " + ObjectUtils.getObjectDescription(object2)); }//from w ww . j a v a2 s . c om // if both objects are == (incl. null) they are equal if (object1 == object2) { return true; } // if one object is null and the other is not, the two objects aren't // equal if (object1 == null || object2 == null) { return false; } if (object1 instanceof Calendar && object2 instanceof Calendar) { return equals(((Calendar) object1).getTime(), ((Calendar) object2).getTime()); } if (object1 instanceof Comparable && object1.getClass() == object2.getClass()) { return ((Comparable) object1).compareTo(object2) == 0; } if (object1 instanceof Map.Entry && object2 instanceof Map.Entry) { if (object1.getClass() != object2.getClass()) { return false; } Map.Entry me1 = (Map.Entry) object1; Map.Entry me2 = (Map.Entry) object2; return equals(me1.getKey(), me2.getKey()) && equals(me1.getValue(), me2.getValue()); } // if both objects are arrays if (object1.getClass().isArray() && object2.getClass().isArray()) { // if the arrays aren't of the same class, the two objects aren't // equal if (object1.getClass() != object2.getClass()) { return false; } // else, same type of array // if the arrays are different sizes, they aren't equal if (Array.getLength(object1) != Array.getLength(object2)) { return false; } // else arrays are the same size // iterate through the arrays and check if all elements are // equal for (int i = 0; i < Array.getLength(object1); i++) { // if one item isn't equal to the other if (!equals(Array.get(object1, i), Array.get(object2, i))) { // the arrays aren't equal return false; } } // if we iterated through both arrays and found no items // that weren't equal to each other, the collections are // equal return true; } if (object1 instanceof Iterator && object2 instanceof Iterator) { Iterator iterator1 = (Iterator) object1; Iterator iterator2 = (Iterator) object2; while (iterator1.hasNext()) { if (!iterator2.hasNext()) { return false; } // if one item isn't equal to the other if (!equals(iterator1.next(), iterator2.next())) { // the arrays aren't equal return false; } } // if we iterated through both collections and found // no items that weren't equal to each other, the // collections are equal return !iterator2.hasNext(); } if (object1 instanceof Enumeration && object2 instanceof Enumeration) { return equals(new EnumerationIterator((Enumeration) object1), new EnumerationIterator((Enumeration) object2)); } if ((object1 instanceof List && object2 instanceof List) || (object1 instanceof SortedSet && object2 instanceof SortedSet)) { // if the collections aren't of the same type, they aren't equal if (object1.getClass() != object2.getClass()) { return false; } // else same type of collection return equals(((Collection) object1).iterator(), ((Collection) object2).iterator()); } if (object1 instanceof Set && object2 instanceof Set) { // if the sets aren't of the same type, they aren't equal if (object1.getClass() != object2.getClass()) { return false; } // else same type of set Set set1 = (Set) object1; Set set2 = (Set) object2; // if the sets aren't the same size, they aren't equal if (set1.size() != set2.size()) { return false; } // else sets are the same size Iterator iterator1 = set1.iterator(); while (iterator1.hasNext()) { Object next = iterator1.next(); if (!contains(set2, next)) { return false; } } return true; } if (object1 instanceof Map && object2 instanceof Map) { return equals(((Map) object1).entrySet(), ((Map) object2).entrySet()); } if (object1.getClass() == object2.getClass() && object1 instanceof StringBuffer) { return object1.toString().equals(object2.toString()); } // for primitives, use their equals methods if (object1.getClass() == object2.getClass() && (object1 instanceof String || object1 instanceof Number || object1 instanceof Boolean || //object1 instanceof StringBuffer || object1 instanceof Character)) { return object1.equals(object2); } // for non-primitives, compare field-by-field return MorphEqualsBuilder.reflectionEquals(object1, object2); }
From source file:org.apache.bval.util.IndexedAccess.java
/** * {@inheritDoc}// w w w .j a v a 2s . c om */ @Override public Object get(Object instance) { if (index == null) { throw new UnsupportedOperationException("Cannot read null index"); } if (instance != null && instance.getClass().isArray()) { if (Array.getLength(instance) - index > 0) { return Array.get(instance, index); } } else if (instance instanceof List<?>) { List<?> list = (List<?>) instance; if (list.size() - index > 0) { return list.get(index); } } else if (instance instanceof Iterable<?>) { int i = 0; for (Object o : (Iterable<?>) instance) { if (++i == index) { return o; } } } return null; }
From source file:ArrayHelper.java
public static List toList(Object array) { if (array instanceof Object[]) return Arrays.asList((Object[]) array); // faster? int size = Array.getLength(array); ArrayList list = new ArrayList(size); for (int i = 0; i < size; i++) { list.add(Array.get(array, i)); }//from w w w. j a v a2 s . c om return list; }
From source file:Main.java
/** * Underlying implementation of add(array, index, element) methods. * The last parameter is the class, which may not equal element.getClass * for primitives./* w w w . ja va 2s .c o m*/ * * @param array the array to add the element to, may be <code>null</code> * @param index the position of the new object * @param element the object to add * @param clss the type of the element being added * @return A new array containing the existing elements and the new element */ private static Object add(Object array, int index, Object element, Class clss) { if (array == null) { if (index != 0) { throw new IndexOutOfBoundsException("Index: " + index + ", Length: 0"); } Object joinedArray = Array.newInstance(clss, 1); Array.set(joinedArray, 0, element); return joinedArray; } int length = Array.getLength(array); if (index > length || index < 0) { throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length); } Object result = Array.newInstance(clss, length + 1); System.arraycopy(array, 0, result, 0, index); Array.set(result, index, element); if (index < length) { System.arraycopy(array, index, result, index + 1, length - index); } return result; }
From source file:com.smallchill.core.toolbox.Func.java
/** * length?sizelength?????//from w w w . j a va 2 s .c om * * @param obj * * @return */ public static int length(Object obj) { if (obj == null) { return 0; } if (obj instanceof CharSequence) { return ((CharSequence) obj).length(); } if (obj instanceof Collection) { return ((Collection<?>) obj).size(); } if (obj instanceof Map) { return ((Map<?, ?>) obj).size(); } int count; if (obj instanceof Iterator) { Iterator<?> iter = (Iterator<?>) obj; count = 0; while (iter.hasNext()) { count++; iter.next(); } return count; } if (obj instanceof Enumeration) { Enumeration<?> enumeration = (Enumeration<?>) obj; count = 0; while (enumeration.hasMoreElements()) { count++; enumeration.nextElement(); } return count; } if (obj.getClass().isArray() == true) { return Array.getLength(obj); } return -1; }
From source file:com.qpark.eip.core.ToString.java
private static String toStringNativeArray(final String name, final Object o) { StringBuffer sb = new StringBuffer(4098); int n = Array.getLength(o); if (byte[].class.isInstance(o)) { sb.append(outName(name, o));//from w w w. j a v a2s . c o m sb.append("[").append(n).append("]byte{...}"); } else if (boolean[].class.isInstance(o)) { sb.append(outName(name, o)); sb.append("[").append(n).append("]boolean{"); boolean[] booleans = (boolean[]) o; for (int j = 0; j < n; j++) { if (j > 0) { sb.append(","); } if (booleans[j]) { sb.append("1"); } else { sb.append("0"); } } sb.append("}"); } else if (char[].class.isInstance(o)) { sb.append(outName(name, o)); sb.append("[").append(n).append("]char{"); sb.append(new String((char[]) o)); sb.append("}"); } else if (short[].class.isInstance(o)) { sb.append(outName(name, o)); sb.append("[").append(n).append("]short{"); short[] shorts = (short[]) o; for (int j = 0; j < n; j++) { if (j > 0) { sb.append(","); } sb.append(shorts[j]); } sb.append("}"); } else if (int[].class.isInstance(o)) { sb.append(outName(name, o)); sb.append("[").append(n).append("]int{"); int[] ints = (int[]) o; for (int j = 0; j < n; j++) { if (j > 0) { sb.append(","); } sb.append(ints[j]); } sb.append("}"); } else if (long[].class.isInstance(o)) { sb.append(outName(name, o)); sb.append("[").append(n).append("]long{"); long[] longs = (long[]) o; for (int j = 0; j < n; j++) { if (j > 0) { sb.append(","); } sb.append(longs[j]); } sb.append("}"); } else if (float[].class.isInstance(o)) { sb.append(outName(name, o)); sb.append("[").append(n).append("]float{"); float[] floats = (float[]) o; for (int j = 0; j < n; j++) { if (j > 0) { sb.append(","); } sb.append(floats[j]); } sb.append("}"); } else if (double[].class.isInstance(o)) { sb.append(outName(name, o)); sb.append("[").append(n).append("]double{"); double[] doubles = (double[]) o; for (int j = 0; j < n; j++) { if (j > 0) { sb.append(","); } sb.append(doubles[j]); } sb.append("}"); } else { toString(name, (Object[]) o); } return sb.toString(); }
From source file:edu.ucla.stat.SOCR.chart.SuperBubbleChart.java
/** * // w w w . j av a 2s . c o m * @param isDemo data come from demo(true) or dataTable(false) * @return */ protected XYZDataset createDataset(boolean isDemo) { if (isDemo) { XYZDataset dataset = new SampleXYZDataset(); domainLabel = "X"; rangeLabel = "Y"; return dataset; } else { setXYZArray(); int len = Array.getLength(x); /* Object[] xData = new Object[len]; Object[] yData = new Object[len]; Object[] zData = new Object[len]; for (int i=0; i<len; i++){ xData[i]=new Double(x[i]); yData[i]=new Double(y[i]); zData[i]=new Double(z[i]); } // create the dataset... DefaultContourDataset dataset = new DefaultContourDataset((Comparable)independentHeaders[0].substring(0,independentHeaders[0].indexOf(":")), xData, yData, zData); */ double[][] data = new double[3][len]; double zMax = z[0]; double xMax = x[0]; double yMax = y[0]; for (int i = 1; i < len; i++) { if (x[i] > xMax) xMax = x[i]; if (y[i] > yMax) yMax = y[i]; if (z[i] > zMax) zMax = z[i]; } if (xMax > yMax) xMax = yMax; if (zMax > xMax * 0.75) zScale = (xMax * 0.75) / zMax; zScale = zScale * zShrinkPercent / 100; //System.out.println("zScale="+zScale); for (int i = 0; i < len; i++) { data[0][i] = x[i]; data[1][i] = y[i]; data[2][i] = z[i] * zScale; } DefaultXYZDataset dataset = new DefaultXYZDataset(); String serieName = independentHeaders[0]; if (independentHeaders[0].indexOf(":") != -1) serieName = independentHeaders[0].substring(0, independentHeaders[0].indexOf(":")); dataset.addSeries((Comparable) serieName, data); return dataset; } }