List of usage examples for java.lang.reflect Array getLength
@HotSpotIntrinsicCandidate public static native int getLength(Object array) throws IllegalArgumentException;
From source file:edu.ucla.stat.SOCR.analyses.gui.Chart.java
protected XYDataset createXYDataset(String serie1, double x[], double y[], String serie2, double y_intercept, double slope) { XYSeries series1 = new XYSeries(serie1); for (int i = 0; i < Array.getLength(x); i++) { series1.add(x[i], y[i]);//from ww w . ja v a2 s. com } XYSeries series2 = new XYSeries(serie2); double min_x, min_y, max_x, max_y; min_x = 1000; max_x = -1000; for (int i = 0; i < Array.getLength(x); i++) { if (min_x > x[i]) min_x = x[i]; if (max_x < x[i]) max_x = x[i]; } min_y = slope * min_x + y_intercept; max_y = slope * max_x + y_intercept; series2.add(min_x, min_y); series2.add(max_x, max_y); XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries(series1); dataset.addSeries(series2); return dataset; }
From source file:com.googlecode.fightinglayoutbugs.helpers.TestHelper.java
/** * Works for strings, arrays, collections, and maps. *//* ww w. j a v a2s. co m*/ public static Matcher<Object> isEmpty() { return new BaseMatcher<Object>() { @Override public boolean matches(Object o) { final boolean result; if (o == null) { result = false; } else if (o instanceof CharSequence || o instanceof StringWriter) { final String s = o.toString(); result = (s.length() == 0); } else if (o instanceof Collection) { result = ((Collection) o).isEmpty(); } else if (o instanceof Map) { result = ((Map) o).isEmpty(); } else if (o.getClass().isArray()) { result = (Array.getLength(o) == 0); } else { throw new RuntimeException("Don't know how to handle object of type " + o.getClass().getName()); } return result; } @Override public void describeTo(Description description) { description.appendText("empty"); } }; }
From source file:com.streamsets.datacollector.http.JMXJsonServlet.java
private void writeObject(JsonGenerator jg, Object value) throws IOException { if (value == null) { jg.writeNull();/* w ww . j av a 2 s . com*/ } else { Class<?> c = value.getClass(); if (c.isArray()) { jg.writeStartArray(); int len = Array.getLength(value); for (int j = 0; j < len; j++) { Object item = Array.get(value, j); writeObject(jg, item); } jg.writeEndArray(); } else if (value instanceof Number) { Number n = (Number) value; jg.writeNumber(n.toString()); } else if (value instanceof Boolean) { Boolean b = (Boolean) value; jg.writeBoolean(b); } else if (value instanceof CompositeData) { CompositeData cds = (CompositeData) value; CompositeType comp = cds.getCompositeType(); Set<String> keys = comp.keySet(); jg.writeStartObject(); for (String key : keys) { writeAttribute(jg, key, cds.get(key)); } jg.writeEndObject(); } else if (value instanceof TabularData) { TabularData tds = (TabularData) value; jg.writeStartArray(); for (Object entry : tds.values()) { writeObject(jg, entry); } jg.writeEndArray(); } else if (value instanceof GaugeValue) { ((GaugeValue) value).serialize(jg); } else { jg.writeString(value.toString()); } } }
From source file:adams.ml.Dataset.java
@Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < count(); i++) { DataRow dr = get(i);/* ww w.j a v a2 s . c o m*/ for (String s : dr.getKeys()) { Object o = dr.getObject(s); if (o != null && o.getClass().isArray()) { sb.append("(" + s + ":"); for (int j = 0; j < Array.getLength(o); j++) { Object p = Array.get(o, j); sb.append(p.toString()); if (j < Array.getLength(o) - 1) { sb.append(","); } } sb.append(")"); } else if (o != null) { sb.append("(" + s + ":" + o.toString() + ")"); } } sb.append("\n"); } return (sb.toString()); }
From source file:cz.vse.esper.XMLRenderer.java
private static void recursiveRender(EventBean theEvent, StringBuilder buf, int level, RendererMeta meta, RendererMetaOptions rendererMetaOptions) { GetterPair[] simpleProps = meta.getSimpleProperties(); for (GetterPair simpleProp : simpleProps) { Object value = simpleProp.getGetter().get(theEvent); if (value == null) { continue; }/*from w w w . j a va2s . co m*/ ident(buf, level); buf.append('<'); buf.append(simpleProp.getName()); buf.append('>'); if (rendererMetaOptions.getRenderer() == null) { simpleProp.getOutput().render(value, buf); } else { EventPropertyRendererContext context = rendererMetaOptions.getRendererContext(); context.setStringBuilderAndReset(buf); context.setPropertyName(simpleProp.getName()); context.setPropertyValue(value); context.setDefaultRenderer(simpleProp.getOutput()); rendererMetaOptions.getRenderer().render(context); } buf.append("</"); buf.append(simpleProp.getName()); buf.append('>'); buf.append(NEWLINE); } GetterPair[] indexProps = meta.getIndexProperties(); for (GetterPair indexProp : indexProps) { Object value = indexProp.getGetter().get(theEvent); if (value == null) { continue; } if (!value.getClass().isArray()) { log.warn("Property '" + indexProp.getName() + "' returned a non-array object"); continue; } for (int i = 0; i < Array.getLength(value); i++) { Object arrayItem = Array.get(value, i); if (arrayItem == null) { continue; } ident(buf, level); buf.append('<'); buf.append(indexProp.getName()); buf.append('>'); if (rendererMetaOptions.getRenderer() == null) { indexProp.getOutput().render(arrayItem, buf); } else { EventPropertyRendererContext context = rendererMetaOptions.getRendererContext(); context.setStringBuilderAndReset(buf); context.setPropertyName(indexProp.getName()); context.setPropertyValue(arrayItem); context.setIndexedPropertyIndex(i); context.setDefaultRenderer(indexProp.getOutput()); rendererMetaOptions.getRenderer().render(context); } buf.append("</"); buf.append(indexProp.getName()); buf.append('>'); buf.append(NEWLINE); } } GetterPair[] mappedProps = meta.getMappedProperties(); for (GetterPair mappedProp : mappedProps) { Object value = mappedProp.getGetter().get(theEvent); if ((value != null) && (!(value instanceof Map))) { log.warn("Property '" + mappedProp.getName() + "' expected to return Map and returned " + value.getClass() + " instead"); continue; } ident(buf, level); buf.append('<'); buf.append(mappedProp.getName()); buf.append('>'); buf.append(NEWLINE); if (value != null) { Map<String, Object> map = (Map<String, Object>) value; if (!map.isEmpty()) { String localDelimiter = ""; Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator(); for (; it.hasNext();) { Map.Entry<String, Object> entry = it.next(); if (entry.getKey() == null) { continue; } buf.append(localDelimiter); ident(buf, level + 1); buf.append('<'); buf.append(entry.getKey()); buf.append('>'); if (entry.getValue() != null) { OutputValueRenderer outputValueRenderer = new OutputValueRendererXMLString(); if (rendererMetaOptions.getRenderer() == null) { outputValueRenderer.render(entry.getValue(), buf); } else { EventPropertyRendererContext context = rendererMetaOptions.getRendererContext(); context.setStringBuilderAndReset(buf); context.setPropertyName(mappedProp.getName()); context.setPropertyValue(entry.getValue()); context.setMappedPropertyKey(entry.getKey()); context.setDefaultRenderer(outputValueRenderer); rendererMetaOptions.getRenderer().render(context); } } buf.append('<'); buf.append(entry.getKey()); buf.append('>'); localDelimiter = NEWLINE; } } } buf.append(NEWLINE); ident(buf, level); buf.append("</"); buf.append(mappedProp.getName()); buf.append('>'); buf.append(NEWLINE); } NestedGetterPair[] nestedProps = meta.getNestedProperties(); for (NestedGetterPair nestedProp : nestedProps) { Object value = nestedProp.getGetter().getFragment(theEvent); if (value == null) { continue; } if (!nestedProp.isArray()) { if (!(value instanceof EventBean)) { log.warn("Property '" + nestedProp.getName() + "' expected to return EventBean and returned " + value.getClass() + " instead"); buf.append("null"); continue; } renderElementFragment((EventBean) value, buf, level, nestedProp, rendererMetaOptions); } else { if (!(value instanceof EventBean[])) { log.warn("Property '" + nestedProp.getName() + "' expected to return EventBean[] and returned " + value.getClass() + " instead"); buf.append("null"); continue; } EventBean[] nestedEventArray = (EventBean[]) value; for (int i = 0; i < nestedEventArray.length; i++) { EventBean arrayItem = nestedEventArray[i]; if (arrayItem == null) { continue; } renderElementFragment(arrayItem, buf, level, nestedProp, rendererMetaOptions); } } } }
From source file:io.wcm.caconfig.editor.impl.ConfigDataServlet.java
private Object toJsonValue(Object value) { if (value == null) { return null; }//from w w w. j av a2s . c o m if (value.getClass().isArray()) { JSONArray array = new JSONArray(); for (int i = 0; i < Array.getLength(value); i++) { array.put(Array.get(value, i)); } return array; } else { return value; } }
From source file:com.espertech.esper.event.util.XMLRendererImpl.java
private static void recursiveRender(EventBean theEvent, StringBuilder buf, int level, RendererMeta meta, RendererMetaOptions rendererMetaOptions) { GetterPair[] simpleProps = meta.getSimpleProperties(); for (GetterPair simpleProp : simpleProps) { Object value = simpleProp.getGetter().get(theEvent); if (value == null) { continue; }/* ww w. j av a2 s . co m*/ ident(buf, level); buf.append('<'); buf.append(simpleProp.getName()); buf.append('>'); if (rendererMetaOptions.getRenderer() == null) { simpleProp.getOutput().render(value, buf); } else { EventPropertyRendererContext context = rendererMetaOptions.getRendererContext(); context.setStringBuilderAndReset(buf); context.setPropertyName(simpleProp.getName()); context.setPropertyValue(value); context.setDefaultRenderer(simpleProp.getOutput()); rendererMetaOptions.getRenderer().render(context); } buf.append("</"); buf.append(simpleProp.getName()); buf.append('>'); buf.append(NEWLINE); } GetterPair[] indexProps = meta.getIndexProperties(); for (GetterPair indexProp : indexProps) { Object value = indexProp.getGetter().get(theEvent); if (value == null) { continue; } if (!value.getClass().isArray()) { log.warn("Property '" + indexProp.getName() + "' returned a non-array object"); continue; } for (int i = 0; i < Array.getLength(value); i++) { Object arrayItem = Array.get(value, i); if (arrayItem == null) { continue; } ident(buf, level); buf.append('<'); buf.append(indexProp.getName()); buf.append('>'); if (rendererMetaOptions.getRenderer() == null) { indexProp.getOutput().render(arrayItem, buf); } else { EventPropertyRendererContext context = rendererMetaOptions.getRendererContext(); context.setStringBuilderAndReset(buf); context.setPropertyName(indexProp.getName()); context.setPropertyValue(arrayItem); context.setIndexedPropertyIndex(i); context.setDefaultRenderer(indexProp.getOutput()); rendererMetaOptions.getRenderer().render(context); } buf.append("</"); buf.append(indexProp.getName()); buf.append('>'); buf.append(NEWLINE); } } GetterPair[] mappedProps = meta.getMappedProperties(); for (GetterPair mappedProp : mappedProps) { Object value = mappedProp.getGetter().get(theEvent); if ((value != null) && (!(value instanceof Map))) { log.warn("Property '" + mappedProp.getName() + "' expected to return Map and returned " + value.getClass() + " instead"); continue; } ident(buf, level); buf.append('<'); buf.append(mappedProp.getName()); buf.append('>'); buf.append(NEWLINE); if (value != null) { Map<String, Object> map = (Map<String, Object>) value; if (!map.isEmpty()) { String localDelimiter = ""; Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator(); for (; it.hasNext();) { Map.Entry<String, Object> entry = it.next(); if (entry.getKey() == null) { continue; } buf.append(localDelimiter); ident(buf, level + 1); buf.append('<'); buf.append(entry.getKey()); buf.append('>'); if (entry.getValue() != null) { OutputValueRenderer outputValueRenderer = OutputValueRendererFactory .getOutputValueRenderer(entry.getValue().getClass(), rendererMetaOptions); if (rendererMetaOptions.getRenderer() == null) { outputValueRenderer.render(entry.getValue(), buf); } else { EventPropertyRendererContext context = rendererMetaOptions.getRendererContext(); context.setStringBuilderAndReset(buf); context.setPropertyName(mappedProp.getName()); context.setPropertyValue(entry.getValue()); context.setMappedPropertyKey(entry.getKey()); context.setDefaultRenderer(outputValueRenderer); rendererMetaOptions.getRenderer().render(context); } } buf.append('<'); buf.append(entry.getKey()); buf.append('>'); localDelimiter = NEWLINE; } } } buf.append(NEWLINE); ident(buf, level); buf.append("</"); buf.append(mappedProp.getName()); buf.append('>'); buf.append(NEWLINE); } NestedGetterPair[] nestedProps = meta.getNestedProperties(); for (NestedGetterPair nestedProp : nestedProps) { Object value = nestedProp.getGetter().getFragment(theEvent); if (value == null) { continue; } if (!nestedProp.isArray()) { if (!(value instanceof EventBean)) { log.warn("Property '" + nestedProp.getName() + "' expected to return EventBean and returned " + value.getClass() + " instead"); buf.append("null"); continue; } renderElementFragment((EventBean) value, buf, level, nestedProp, rendererMetaOptions); } else { if (!(value instanceof EventBean[])) { log.warn("Property '" + nestedProp.getName() + "' expected to return EventBean[] and returned " + value.getClass() + " instead"); buf.append("null"); continue; } EventBean[] nestedEventArray = (EventBean[]) value; for (int i = 0; i < nestedEventArray.length; i++) { EventBean arrayItem = nestedEventArray[i]; if (arrayItem == null) { continue; } renderElementFragment(arrayItem, buf, level, nestedProp, rendererMetaOptions); } } } }
From source file:com.streamsets.datacollector.bundles.content.SdcInfoContentGenerator.java
private void writeObject(JsonGenerator jg, Object value) throws IOException { if (value == null) { jg.writeNull();//from w ww . j a v a2s .c o m } else { Class<?> c = value.getClass(); if (c.isArray()) { jg.writeStartArray(); int len = Array.getLength(value); for (int j = 0; j < len; j++) { Object item = Array.get(value, j); writeObject(jg, item); } jg.writeEndArray(); } else if (value instanceof Number) { Number n = (Number) value; if (value instanceof Double && (((Double) value).isInfinite() || ((Double) value).isNaN())) { jg.writeString(n.toString()); } else { jg.writeNumber(n.toString()); } } else if (value instanceof Boolean) { Boolean b = (Boolean) value; jg.writeBoolean(b); } else if (value instanceof CompositeData) { CompositeData cds = (CompositeData) value; CompositeType comp = cds.getCompositeType(); Set<String> keys = comp.keySet(); jg.writeStartObject(); for (String key : keys) { writeAttribute(jg, key, cds.get(key)); } jg.writeEndObject(); } else if (value instanceof TabularData) { TabularData tds = (TabularData) value; jg.writeStartArray(); for (Object entry : tds.values()) { writeObject(jg, entry); } jg.writeEndArray(); } else if (value instanceof GaugeValue) { ((GaugeValue) value).serialize(jg); } else { jg.writeString(value.toString()); } } }
From source file:gov.nih.nci.caarray.plugins.agilent.AgilentRawTextDataHandlerTest.java
private void checkColumnLength(HybridizationData hybridizationData, AgilentTextQuantitationType quantitationType, int expectedColumnLength) { try {//from w w w . j av a 2s . co m final AbstractDataColumn column = hybridizationData.getColumn(quantitationType); assertNotNull(column); final Object array = PropertyUtils.getProperty(column, "values"); assertEquals(expectedColumnLength, Array.getLength(array)); } catch (final IllegalAccessException ex) { ex.printStackTrace(); fail(ex.toString()); } catch (final InvocationTargetException ex) { ex.printStackTrace(); fail(ex.toString()); } catch (final NoSuchMethodException ex) { ex.printStackTrace(); fail(ex.toString()); } }
From source file:com.google.feedserver.util.BeanUtil.java
/** * Compares two JavaBeans for equality by comparing their properties and the * class of the beans.//from w w w .jav a 2 s. c o m * * @param bean1 Bean to compare * @param bean2 Bean to compare * @return True if {@code bean2} has the same properties with the same values * as {@code bean1} and if they share the same class. */ public boolean equals(Object bean1, Object bean2) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { if ((null == bean1) && (null == bean2)) { return true; } else if ((null == bean1) || (null == bean2)) { return false; } if (bean1.getClass() != bean2.getClass()) { return false; } if (bean1.getClass().isArray() && bean2.getClass().isArray()) { if (Array.getLength(bean1) != Array.getLength(bean2)) { return false; } for (int i = 0; i < Array.getLength(bean1); i++) { if (!equals(Array.get(bean1, i), Array.get(bean2, i))) { return false; } } return true; } else if (bean1.getClass().isArray()) { return false; } else if (bean2.getClass().isArray()) { return false; } else if (isBean(bean1.getClass())) { BeanInfo bean1Info; try { bean1Info = Introspector.getBeanInfo(bean1.getClass(), Object.class); } catch (IntrospectionException e) { return false; } for (PropertyDescriptor p : bean1Info.getPropertyDescriptors()) { Method reader = p.getReadMethod(); if (reader != null) { try { Object value1 = reader.invoke(bean1); Object value2 = reader.invoke(bean2); if (!equals(value1, value2)) { return false; } } catch (IllegalArgumentException e) { return false; } catch (IllegalAccessException e) { return false; } catch (InvocationTargetException e) { return false; } } } return true; } else { return bean1.equals(bean2); } }