List of usage examples for java.lang.reflect Array getLength
@HotSpotIntrinsicCandidate public static native int getLength(Object array) throws IllegalArgumentException;
From source file:ArrayUtils.java
/** * Like {@link #equals(Object, Object)}, but compares arrays without regard * to order and uses an independent checker to check for equality of * elements/*w ww. ja v a 2s . c o m*/ * * @param o1 * The first object to compare * @param o2 * The second object to compare * @param checker * The checker to check elements for equality * @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 equalsUnordered(Object o1, Object o2, EqualsChecker checker) { if (o1 == null) return o2 == null; if (o2 == null) return false; if (o1 instanceof Object[] && o2 instanceof Object[]) return equalsUnordered((Object[]) o1, (Object[]) o2, checker); 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; if (len == 0) return true; long[] bs = new long[(len - 1) / 64 + 1]; long mask; int i, j, bsIdx; o1Loop: for (i = 0; i < len; i++) { mask = 0; for (j = 0; j < len; j++) { bsIdx = j / 64; if (mask == 0) mask = 0x8000000000000000L; if ((bs[bsIdx] & mask) == 0 && checker.equals(Array.get(o1, i), Array.get(o2, j))) { bs[bsIdx] |= mask; continue o1Loop; } mask >>>= 1; } return false; } return true; } return checker.equals(o1, o2); }
From source file:com.espertech.esper.regression.pattern.TestMatchUntilExpr.java
private void validateStmt(EPServiceProvider engine, String stmtText, int numEventsA, boolean match, Integer matchCount) {/*from w ww . ja va2 s .c o m*/ SupportUpdateListener listener = new SupportUpdateListener(); EPStatement stmt = engine.getEPAdministrator().createPattern(stmtText); stmt.addListener(listener); for (int i = 0; i < numEventsA; i++) { engine.getEPRuntime().sendEvent(new SupportBean("A", i)); } assertFalse(listener.isInvoked()); engine.getEPRuntime().sendEvent(new SupportBean("B", -1)); assertEquals(match, listener.isInvoked()); if (!match) { return; } Object valueATag = listener.assertOneGetNewAndReset().get("a"); if (matchCount == null) { assertNull(valueATag); } else { assertEquals((int) matchCount, Array.getLength(valueATag)); } }
From source file:com.greenlaw110.rythm.toString.ToStringStyle.java
/** * <p>Append to the <code>toString</code> the detail of an array type.</p> * * @param buffer the <code>StringBuilder</code> to populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to the <code>toString</code>, * not <code>null</code>//from w ww. j ava 2 s.c o m * @since 2.0 */ protected void reflectionAppendArrayDetail(StringBuilder buffer, String fieldName, Object array) { buffer.append(arrayStart); int length = Array.getLength(array); for (int i = 0; i < length; i++) { Object item = Array.get(array, i); if (i > 0) { buffer.append(arraySeparator); } if (item == null) { appendNullText(buffer, fieldName); } else { appendInternal(buffer, fieldName, item, arrayContentDetail); } } buffer.append(arrayEnd); }
From source file:edu.ucla.stat.SOCR.chart.ChartGenerator_JTable.java
/** * * @param numberOfSeries/* w w w . j a va 2 s. com*/ * @param pairs * @return */ private XYZDataset createXYZDatasetFromTable(int numberOfSeries, int[][] pairs) { double[] x, y, z; int[] xVar = new int[numberOfSeries]; int[] yVar = new int[numberOfSeries]; int[] zVar = new int[numberOfSeries]; String[] yHeaders = new String[numberOfSeries]; String[] xHeaders = new String[numberOfSeries]; String[] zHeaders = new String[numberOfSeries]; //independentVars store the column index for indep for (int i = 0; i < numberOfSeries; i++) { //System.out.println("x=pairs["+i+"][0]="+pairs[i][0]); //System.out.println("y=pairs["+i+"][1]="+pairs[i][1]); //System.out.println("z=pairs["+i+"][1]="+pairs[i][2]); xVar[i] = pairs[i][0]; //x, value(pie) yVar[i] = pairs[i][1]; //y, name(pie) zVar[i] = pairs[i][2]; xHeaders[i] = columnModel.getColumn(xVar[i]).getHeaderValue().toString().trim(); yHeaders[i] = columnModel.getColumn(yVar[i]).getHeaderValue().toString().trim(); zHeaders[i] = columnModel.getColumn(zVar[i]).getHeaderValue().toString().trim(); } int xLength = 0; int yLength = 0; int zLength = 0; String cellValue = null; ArrayList<String> xList = new ArrayList<String>(); ArrayList<String> yList = new ArrayList<String>(); ArrayList<String> zList = new ArrayList<String>(); yLength = dataTable.getRowCount(); y = new double[yLength]; z = new double[yLength]; x = new double[yLength]; try { //dependent Y for (int index2 = 0; index2 < numberOfSeries; index2++) { yList = new ArrayList<String>(); yLength = 0; for (int k = 0; k < dataTable.getRowCount(); k++) { try { cellValue = ((String) dataTable.getValueAt(k, yVar[index2])).trim(); if (cellValue != null && !cellValue.equals("")) { yList.add(yLength, cellValue); yLength++; } else { continue; // to the next for } } catch (Exception e) { // do nothing? } } try { for (int i = 0; i < yLength; i++) { y[i] = Double.parseDouble((String) yList.get(i)); //System.out.println("y["+i+"]="+y[i]); } } catch (NumberFormatException e) { System.out.println("Data format error!"); } } xyLength = yLength; for (int index2 = 0; index2 < numberOfSeries; index2++) { zList = new ArrayList<String>(); zLength = 0; for (int k = 0; k < dataTable.getRowCount(); k++) { try { cellValue = ((String) dataTable.getValueAt(k, zVar[index2])).trim(); if (cellValue != null && !cellValue.equals("")) { zList.add(zLength, cellValue); zLength++; } else { continue; // to the next for } } catch (Exception e) { // do nothing? } } try { for (int i = 0; i < zLength; i++) { z[i] = Double.parseDouble((String) zList.get(i)); //System.out.println("z["+i+"]="+z[i]); } } catch (NumberFormatException e) { System.out.println("Data format error!"); } } for (int index2 = 0; index2 < numberOfSeries; index2++) { xList = new ArrayList<String>(); xLength = 0; for (int k = 0; k < dataTable.getRowCount(); k++) { try { cellValue = ((String) dataTable.getValueAt(k, xVar[index2])).trim(); if (cellValue != null && !cellValue.equals("")) { xList.add(xLength, cellValue); xLength++; } else { continue; // to the next for } } catch (Exception e) { } } try { for (int i = 0; i < xLength; i++) { x[i] = Double.parseDouble((String) xList.get(i)); //System.out.println("x["+i+"]="+x[i]); } } catch (NumberFormatException e) { System.out.println("Data format error!"); } } } catch (Exception e) { System.out.println("Exception In outer catch: " + e); } 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)xHeaders[0].substring(0,xHeaders[0].indexOf(":")), xData, yData, zData);*/ double[][] data = new double[3][len]; for (int i = 0; i < len; i++) { data[0][i] = x[i]; data[1][i] = y[i]; data[2][i] = z[i]; } DefaultXYZDataset dataset = new DefaultXYZDataset(); dataset.addSeries((Comparable) xHeaders[0].substring(0, xHeaders[0].indexOf(":")), data); return dataset; }
From source file:ArrayUtils.java
/** * A compliment to the {@link #equals(Object, Object)} method, this method * returns a hashcode for <code>array</code> such that it is consistent with * the equals contract for the {@link Object#equals(Object)} method, * represented by the {@link #equals(Object, Object)} method. * //from w w w .j av a 2 s. co m * @param array * The array to get a hashcode for * @return A hashcode based on <code>array</code> and its contents, if any */ public static int hashCode(Object array) { if (array == null) return 0; if (!array.getClass().isArray()) return array.hashCode(); int ret = array.getClass().getComponentType().hashCode(); int len = Array.getLength(array); for (int i = 0; i < len; i++) { ret *= hashCode(Array.get(array, i)); ret += 29; } return ret; }
From source file:org.batoo.common.util.ObjectUtils.java
/** * Convert the given array (which may be a primitive array) to an object array (if necessary of primitive wrapper objects). * <p>//w w w . j a v a 2 s . c o m * A <code>null</code> source value will be converted to an empty Object array. * * @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(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); } final int length = Array.getLength(source); if (length == 0) { return new Object[0]; } final Class<?> wrapperType = Array.get(source, 0).getClass(); final 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:ArrayUtils.java
/** * A utility method for printing out the contents of an array (not deeply) * //from w w w . j a v a2 s .co m * @param array * The array to print * @return A String containing the representation of the contents of * <code>array</code> */ public static String toString(Object array) { if (array == null) return "" + null; else if (array instanceof Object[]) { Object[] oa = (Object[]) array; StringBuffer ret = new StringBuffer("["); for (int i = 0; i < oa.length; i++) { ret.append(toString(oa[i])); if (i < oa.length - 1) ret.append(", "); } ret.append("]"); return ret.toString(); } else if (array.getClass().isArray()) { StringBuffer ret = new StringBuffer("["); int len = Array.getLength(array); for (int i = 0; i < len; i++) { ret.append(Array.get(array, i)); if (i < len - 1) ret.append(", "); } ret.append("]"); return ret.toString(); } else return array.toString(); }
From source file:com.espertech.esper.filter.FilterSpecCompiler.java
private static FilterSpecParam handleInSetNode(ExprInNode constituent, LinkedHashMap<String, Pair<EventType, String>> arrayEventTypes, ExprEvaluatorContext exprEvaluatorContext, String statementName) throws ExprValidationException { ExprNode left = constituent.getChildNodes()[0]; if (!(left instanceof ExprFilterOptimizableNode)) { return null; }/*from w w w . ja v a2 s . co m*/ ExprFilterOptimizableNode filterOptimizableNode = (ExprFilterOptimizableNode) left; FilterSpecLookupable lookupable = filterOptimizableNode.getFilterLookupable(); FilterOperator op = FilterOperator.IN_LIST_OF_VALUES; if (constituent.isNotIn()) { op = FilterOperator.NOT_IN_LIST_OF_VALUES; } int expectedNumberOfConstants = constituent.getChildNodes().length - 1; List<FilterSpecParamInValue> listofValues = new ArrayList<FilterSpecParamInValue>(); Iterator<ExprNode> it = Arrays.asList(constituent.getChildNodes()).iterator(); it.next(); // ignore the first node as it's the identifier while (it.hasNext()) { ExprNode subNode = it.next(); if (ExprNodeUtility.isConstantValueExpr(subNode)) { ExprConstantNode constantNode = (ExprConstantNode) subNode; Object constant = constantNode.evaluate(null, true, exprEvaluatorContext); if (constant instanceof Collection) { return null; } if (constant instanceof Map) { return null; } if ((constant != null) && (constant.getClass().isArray())) { for (int i = 0; i < Array.getLength(constant); i++) { Object arrayElement = Array.get(constant, i); Object arrayElementCoerced = handleConstantsCoercion(lookupable, arrayElement); listofValues.add(new InSetOfValuesConstant(arrayElementCoerced)); if (i > 0) { expectedNumberOfConstants++; } } } else { constant = handleConstantsCoercion(lookupable, constant); listofValues.add(new InSetOfValuesConstant(constant)); } } if (subNode instanceof ExprContextPropertyNode) { ExprContextPropertyNode contextPropertyNode = (ExprContextPropertyNode) subNode; Class returnType = contextPropertyNode.getType(); if (JavaClassHelper.isImplementsInterface(contextPropertyNode.getType(), Collection.class) || JavaClassHelper.isImplementsInterface(contextPropertyNode.getType(), Map.class)) { return null; } if ((returnType != null) && (returnType.getClass().isArray())) { return null; } SimpleNumberCoercer coercer = getNumberCoercer(left.getExprEvaluator().getType(), contextPropertyNode.getType(), lookupable.getExpression()); listofValues.add(new InSetOfValuesContextProp(contextPropertyNode.getPropertyName(), contextPropertyNode.getGetter(), coercer)); } if (subNode instanceof ExprIdentNode) { ExprIdentNode identNodeInner = (ExprIdentNode) subNode; if (identNodeInner.getStreamId() == 0) { break; // for same event evals use the boolean expression, via count compare failing below } boolean isMustCoerce = false; Class numericCoercionType = JavaClassHelper.getBoxedType(lookupable.getReturnType()); if (identNodeInner.getExprEvaluator().getType() != lookupable.getReturnType()) { if (JavaClassHelper.isNumeric(lookupable.getReturnType())) { if (!JavaClassHelper.canCoerce(identNodeInner.getExprEvaluator().getType(), lookupable.getReturnType())) { throwConversionError(identNodeInner.getExprEvaluator().getType(), lookupable.getReturnType(), lookupable.getExpression()); } isMustCoerce = true; } else { break; // assumed not compatible } } FilterSpecParamInValue inValue; String streamName = identNodeInner.getResolvedStreamName(); if (arrayEventTypes != null && !arrayEventTypes.isEmpty() && arrayEventTypes.containsKey(streamName)) { Pair<Integer, String> indexAndProp = getStreamIndex(identNodeInner.getResolvedPropertyName()); inValue = new InSetOfValuesEventPropIndexed(identNodeInner.getResolvedStreamName(), indexAndProp.getFirst(), indexAndProp.getSecond(), isMustCoerce, numericCoercionType, statementName); } else { inValue = new InSetOfValuesEventProp(identNodeInner.getResolvedStreamName(), identNodeInner.getResolvedPropertyName(), isMustCoerce, numericCoercionType); } listofValues.add(inValue); } } // Fallback if not all values in the in-node can be resolved to properties or constants if (listofValues.size() == expectedNumberOfConstants) { return new FilterSpecParamIn(lookupable, op, listofValues); } return null; }
From source file:com.github.dozermapper.core.MappingProcessor.java
private Object mapArrayToArray(Object srcObj, Object srcCollectionValue, FieldMap fieldMap, Object destObj) { Class destEntryType = fieldMap.getDestFieldType(destObj.getClass()).getComponentType(); Class srcEntryType = srcCollectionValue.getClass().getComponentType(); int size = Array.getLength(srcCollectionValue); CopyByReferenceContainer copyByReferences = globalConfiguration.getCopyByReferences(); boolean isPrimitiveArray = CollectionUtils.isPrimitiveArray(srcCollectionValue.getClass()); boolean isFinal = Modifier.isFinal(srcEntryType.getModifiers()); boolean isCopyByReference = copyByReferences.contains(srcEntryType); if (destEntryType.isAssignableFrom(srcEntryType) && isFinal && (isPrimitiveArray || isCopyByReference)) { return addArrayContentCopy(fieldMap, size, srcCollectionValue, destObj, destEntryType); } else if (isPrimitiveArray) { return addToPrimitiveArray(srcObj, fieldMap, size, srcCollectionValue, destObj, destEntryType); } else {//from w w w. j a va2 s . co m List<?> list = Arrays.asList((Object[]) srcCollectionValue); List<?> returnList; if (!destEntryType.getName().equals(BASE_CLASS)) { returnList = addOrUpdateToList(srcObj, fieldMap, list, destObj, destEntryType); } else { returnList = addOrUpdateToList(srcObj, fieldMap, list, destObj, null); } return CollectionUtils.convertListToArray(returnList, destEntryType); } }
From source file:ArrayUtils.java
/** * Replaces <code>toReplace</code> with <code>replacement</code> in * <code>array</code> one time at the most * //from w w w . j ava 2 s .c o m * @param array * The array to search and replace in * @param toReplace * The object to replace * @param replacement * The object to replace <code>toReplace</code> with the first * time it is found * @return The index where <code>toReplace</code> was found and replaced, or * -1 if it was not found in <code>array</code> */ public static int replaceOnce(Object array, Object toReplace, Object replacement) { if (array == null) return -1; if (array instanceof Object[]) { Object[] array2 = (Object[]) array; for (int i = 0; i < array2.length; i++) { if (equals(array2[i], toReplace)) { array2[i] = replacement; return i; } } return -1; } else { int i, len; len = Array.getLength(array); for (i = 0; i < len; i++) { if (equals(Array.get(array, i), toReplace)) { put(array, replacement, i); return i; } } return -1; } }