List of usage examples for java.lang.reflect Array getLength
@HotSpotIntrinsicCandidate public static native int getLength(Object array) throws IllegalArgumentException;
From source file:Main.java
/** * Checks if the specified collection/array/iterator is empty. * <p>/* w w w . j a v a 2 s . com*/ * This method can handles objects as follows * <ul> * <li>Collection - via collection isEmpty * <li>Map - via map isEmpty * <li>Array - using array size * <li>Iterator - via hasNext * <li>Enumeration - via hasMoreElements * </ul> * <p> * Note: This method is named to avoid clashing with * {@link #isEmpty(Collection)}. * * @param object the object to get the size of, may be null * @return true if empty or null * @throws IllegalArgumentException thrown if object is not recognised * @since 3.2 */ public static boolean sizeIsEmpty(final Object object) { if (object == null) { return true; } else if (object instanceof Collection<?>) { return ((Collection<?>) object).isEmpty(); } else if (object instanceof Map<?, ?>) { return ((Map<?, ?>) object).isEmpty(); } else if (object instanceof Object[]) { return ((Object[]) object).length == 0; } else if (object instanceof Iterator<?>) { return ((Iterator<?>) object).hasNext() == false; } else if (object instanceof Enumeration<?>) { return ((Enumeration<?>) object).hasMoreElements() == false; } else { try { return Array.getLength(object) == 0; } catch (final IllegalArgumentException ex) { throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); } } }
From source file:com.chadekin.jadys.commons.formatters.ArrayValueFormatter.java
@Override public String format(Object value) { String result = StringUtils.EMPTY; if (value != null) { List convertedValue = Lists.newArrayList(); for (int index = 0; index < Array.getLength(value); index++) { convertedValue.add(Array.get(value, index)); }/* w w w . j ava 2s . c o m*/ result = super.format(convertedValue); } return result; }
From source file:com.espertech.esper.epl.expression.dot.ExprDotEvalArraySize.java
public Object evaluate(Object target, EventBean[] eventsPerStream, boolean isNewData, ExprEvaluatorContext exprEvaluatorContext) { if (target == null) { return null; }/* w ww. j av a 2 s. c om*/ return Array.getLength(target); }
From source file:mil.jpeojtrs.sca.util.PrimitiveArrayUtils.java
public static long[] convertToLongArray(final Object array) { if (array == null) { return null; }/*w w w . j a va2 s . c o m*/ if (array instanceof long[]) { return (long[]) array; } if (array instanceof Long[]) { return ArrayUtils.toPrimitive((Long[]) array); } final long[] newArray = new long[Array.getLength(array)]; for (int i = 0; i < newArray.length; i++) { final Number val = (Number) Array.get(array, i); newArray[i] = val.longValue(); } return newArray; }
From source file:de.ncoder.sensorsystem.android.logging.JSONUtils.java
public static Object wrap(Object o) { if (o == null) { return JSONObject.NULL; } else if (o instanceof JSONArray || o instanceof JSONObject) { return o; } else if (o.equals(JSONObject.NULL)) { return o; } else if (o instanceof Boolean || o instanceof Byte || o instanceof Character || o instanceof Double || o instanceof Float || o instanceof Integer || o instanceof Long || o instanceof Short) { return o; } else if (o instanceof CharSequence) { return JSONObject.quote(o.toString()); } else if (o instanceof Collection) { return new JSONArray((Collection) o); } else if (o.getClass().isArray()) { final int length = Array.getLength(o); List<Object> values = new ArrayList<>(length); for (int i = 0; i < length; ++i) { values.add(wrap(Array.get(o, i))); }//from ww w . j a v a2 s . c o m return new JSONArray(values); } else if (o instanceof Map) { return new JSONObject((Map) o); } else if (o instanceof Location) { return wrapLocation((Location) o); } else if (o instanceof Bundle) { return wrapBundle((Bundle) o); } return o.toString(); }
From source file:Main.java
/** * Checks if the specified collection/array/iterator is empty. * <p>//from w w w .ja v a 2s. co m * This method can handles objects as follows * <ul> * <li>Collection - via collection isEmpty * <li>Map - via map isEmpty * <li>Array - using array size * <li>Iterator - via hasNext * <li>Enumeration - via hasMoreElements * </ul> * <p> * Note: This method is named to avoid clashing with * {@link #isEmpty(Collection)}. * * @param object the object to get the size of, not null * @return true if empty * @throws IllegalArgumentException thrown if object is not recognised or null * @since Commons Collections 3.2 */ public static boolean sizeIsEmpty(Object object) { if (object instanceof Collection) { return ((Collection) object).isEmpty(); } else if (object instanceof Map) { return ((Map) object).isEmpty(); } else if (object instanceof Object[]) { return ((Object[]) object).length == 0; } else if (object instanceof Iterator) { return ((Iterator) object).hasNext() == false; } else if (object instanceof Enumeration) { return ((Enumeration) object).hasMoreElements() == false; } else if (object == null) { throw new IllegalArgumentException("Unsupported object type: null"); } else { try { return Array.getLength(object) == 0; } catch (IllegalArgumentException ex) { throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); } } }
From source file:at.alladin.rmbt.qos.testscript.SystemApi.java
public int getCount(Object array) { if (array != null && array.getClass().isArray()) { return Array.getLength(array); }/*from ww w . jav a2s .com*/ return 0; }
From source file:com.base.dao.sql.ReflectionUtils.java
public static Object convertValue(Object value, Class toType) { Object result = null;// ww w . j a v a 2s. c o m if (value != null) { if (value.getClass().isArray() && toType.isArray()) { Class componentType = toType.getComponentType(); result = Array.newInstance(componentType, Array.getLength(value)); for (int i = 0, icount = Array.getLength(value); i < icount; i++) { Array.set(result, i, convertValue(Array.get(value, i), componentType)); } } else { if ((toType == Integer.class) || (toType == Integer.TYPE)) result = Integer.valueOf((int) longValue(value)); if ((toType == Double.class) || (toType == Double.TYPE)) result = new Double(doubleValue(value)); if ((toType == Boolean.class) || (toType == Boolean.TYPE)) result = booleanValue(value) ? Boolean.TRUE : Boolean.FALSE; if ((toType == Byte.class) || (toType == Byte.TYPE)) result = Byte.valueOf((byte) longValue(value)); if ((toType == Character.class) || (toType == Character.TYPE)) result = new Character((char) longValue(value)); if ((toType == Short.class) || (toType == Short.TYPE)) result = Short.valueOf((short) longValue(value)); if ((toType == Long.class) || (toType == Long.TYPE)) result = Long.valueOf(longValue(value)); if ((toType == Float.class) || (toType == Float.TYPE)) result = new Float(doubleValue(value)); if (toType == BigInteger.class) result = bigIntValue(value); if (toType == BigDecimal.class) result = bigDecValue(value); if (toType == String.class) result = stringValue(value); if (toType == Date.class) { result = DateUtils.toDate(stringValue(value)); } if (Enum.class.isAssignableFrom(toType)) result = enumValue((Class<Enum>) toType, value); } } else { if (toType.isPrimitive()) { result = primitiveDefaults.get(toType); } } return result; }
From source file:ArrayListWrapper.java
public int size() { return Array.getLength(array); }
From source file:com.github.britter.beanvalidators.EmptyConstraintValidator.java
@Override public boolean isValid(final Object value, final ConstraintValidatorContext context) { if (value == null) { return true; } else if (value instanceof String) { return StringUtils.isEmpty((String) value); } else if (value instanceof Collection) { return ((Collection) value).isEmpty(); } else if (value instanceof Map) { return ((Map) value).isEmpty(); } else if (value.getClass().isArray()) { return Array.getLength(value) == 0; } else {/* ww w. j a v a2 s. c o m*/ // Is this the correct behavior? throw new ValidationException("@Empty can not be applied to objects of type " + value.getClass()); } }