Example usage for java.lang.reflect Array getLength

List of usage examples for java.lang.reflect Array getLength

Introduction

In this page you can find the example usage for java.lang.reflect Array getLength.

Prototype

@HotSpotIntrinsicCandidate
public static native int getLength(Object array) throws IllegalArgumentException;

Source Link

Document

Returns the length of the specified array object, as an int .

Usage

From source file:IntStack.java

/**
 * Copy data after array resize. This just copies the entire contents of the
 * old array to the start of the new array. It should be overridden in cases
 * where data needs to be rearranged in the array after a resize.
 * /*from w  ww.j  a  v  a 2 s  .c o  m*/
 * @param base original array containing data
 * @param grown resized array for data
 */

private void resizeCopy(Object base, Object grown) {
    System.arraycopy(base, 0, grown, 0, Array.getLength(base));
}

From source file:com.jilk.ros.rosbridge.implementation.JSON.java

private static JSONArray convertArrayToJSONArray(Object array) {
    JSONArray result = new JSONArray();
    for (int i = 0; i < Array.getLength(array); i++) {
        Object elementObject = Array.get(array, i);
        if (elementObject != null) {
            Object resultObject = convertElementToJSON(elementObject);
            result.add(resultObject);/*from w w  w.  j  av  a2 s.c  o m*/
        }
    }
    return result;
}

From source file:com.android.camera2.its.ItsSerializer.java

@SuppressWarnings("unchecked")
private static Object serializeStreamConfigurationMap(StreamConfigurationMap map)
        throws org.json.JSONException {
    // TODO: Serialize the rest of the StreamConfigurationMap fields.
    JSONObject mapObj = new JSONObject();
    JSONArray cfgArray = new JSONArray();
    int fmts[] = map.getOutputFormats();
    if (fmts != null) {
        for (int fi = 0; fi < Array.getLength(fmts); fi++) {
            Size sizes[] = map.getOutputSizes(fmts[fi]);
            if (sizes != null) {
                for (int si = 0; si < Array.getLength(sizes); si++) {
                    JSONObject obj = new JSONObject();
                    obj.put("format", fmts[fi]);
                    obj.put("width", sizes[si].getWidth());
                    obj.put("height", sizes[si].getHeight());
                    obj.put("input", false);
                    obj.put("minFrameDuration", map.getOutputMinFrameDuration(fmts[fi], sizes[si]));
                    cfgArray.put(obj);//from  w ww.j a va  2 s  .c o m
                }
            }
        }
    }
    mapObj.put("availableStreamConfigurations", cfgArray);
    return mapObj;
}

From source file:com.jaspersoft.jasperserver.export.modules.common.DefaultReportParametersTranslator.java

protected Object[] toBeanParameterValues(Object value) {
    Object[] values;//w  w  w  . jav  a 2s  .  c  o  m
    if (value == null) {
        values = new Object[] { toBeanParameterValue(null) };
    } else if (value.getClass().isArray()) {
        int count = Array.getLength(value);
        values = new Object[count];
        for (int idx = 0; idx < count; idx++) {
            values[idx] = toBeanParameterValue(Array.get(value, idx));
        }
    } else if (value instanceof Collection) {
        Collection valueCollection = (Collection) value;
        values = new Object[valueCollection.size()];

        Iterator iterator = valueCollection.iterator();
        for (int i = 0; iterator.hasNext(); i++) {
            values[i] = toBeanParameterValue(iterator.next());
        }
    } else {
        values = new Object[] { toBeanParameterValue(value) };
    }

    return values;
}

From source file:org.crazydog.util.spring.ObjectUtils.java

/**
 * Determine whether the given object is empty.
 * <p>This method supports the following object types.
 * <ul>//from  www.  j ava 2  s .c o m
 * <li>{@code Array}: considered empty if its length is zero</li>
 * <li>{@link CharSequence}: considered empty if its length is zero</li>
 * <li>{@link Collection}: delegates to {@link Collection#isEmpty()}</li>
 * <li>{@link Map}: delegates to {@link Map#isEmpty()}</li>
 * </ul>
 * <p>If the given object is non-null and not one of the aforementioned
 * supported types, this method returns {@code false}.
 * @param obj the object to check
 * @return {@code true} if the object is {@code null} or <em>empty</em>
 * @since 4.2
 * @see ObjectUtils#isEmpty(Object[])
 * @see StringUtils#hasLength(CharSequence)
 * @see StringUtils#isEmpty(Object)
 * @see org.springframework.util.CollectionUtils#isEmpty(Collection)
 * @see CollectionUtils#isEmpty(Map)
 */
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Object obj) {
    if (obj == null) {
        return true;
    }

    if (obj.getClass().isArray()) {
        return Array.getLength(obj) == 0;
    }
    if (obj instanceof CharSequence) {
        return ((CharSequence) obj).length() == 0;
    }
    if (obj instanceof Collection) {
        return ((Collection) obj).isEmpty();
    }
    if (obj instanceof Map) {
        return ((Map) obj).isEmpty();
    }

    // else
    return false;
}

From source file:mil.jpeojtrs.sca.util.PrimitiveArrayUtils.java

public static boolean[] convertToBooleanArray(final Object array) {
    if (array == null) {
        return null;
    }/*from  ww  w  .  j av a  2 s.c o  m*/
    if (array instanceof boolean[]) {
        return (boolean[]) array;
    }
    if (array instanceof Boolean[]) {
        return ArrayUtils.toPrimitive((Boolean[]) array);
    }
    final boolean[] newArray = new boolean[Array.getLength(array)];
    for (int i = 0; i < newArray.length; i++) {
        final Object obj = Array.get(array, i);
        if (obj instanceof Boolean) {
            newArray[i] = (Boolean) Array.get(array, i);
        } else {
            newArray[i] = ((Number) Array.get(array, i)).byteValue() != 0;
        }
    }
    return newArray;
}

From source file:com.dsj.core.beans.ObjectUtils.java

/**
 * Convert a primitive array to an object array of primitive wrapper
 * objects./*from w  w  w.  j a v a  2  s .com*/
 * 
 * @param primitiveArray
 *            the primitive array
 * @return the object array
 * @throws IllegalArgumentException
 *             if the parameter is not a primitive array
 */
public static Object[] toObjectArray(Object primitiveArray) {
    if (primitiveArray == null) {
        return new Object[0];
    }
    Class clazz = primitiveArray.getClass();
    if (!clazz.isArray() || !clazz.getComponentType().isPrimitive()) {
        throw new IllegalArgumentException("The specified parameter is not a primitive array");
    }
    int length = Array.getLength(primitiveArray);
    if (length == 0) {
        return new Object[0];
    }
    Class wrapperType = Array.get(primitiveArray, 0).getClass();
    Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
    for (int i = 0; i < length; i++) {
        newArray[i] = Array.get(primitiveArray, i);
    }
    return newArray;
}

From source file:es.caib.zkib.jxpath.util.ValueUtils.java

/**
 * Returns an iterator for the supplied collection. If the argument
 * is null, returns an empty iterator. If the argument is not
 * a collection, returns an iterator that produces just that one object.
 * @param collection to iterate/*from   w ww .  j  a v  a  2 s  .  c  o  m*/
 * @return Iterator
 */
public static Iterator iterate(Object collection) {
    if (collection == null) {
        return Collections.EMPTY_LIST.iterator();
    }
    if (collection.getClass().isArray()) {
        int length = Array.getLength(collection);
        if (length == 0) {
            return Collections.EMPTY_LIST.iterator();
        }
        ArrayList list = new ArrayList();
        for (int i = 0; i < length; i++) {
            list.add(Array.get(collection, i));
        }
        return list.iterator();
    }
    if (collection instanceof Collection) {
        return ((Collection) collection).iterator();
    }
    return Collections.singletonList(collection).iterator();
}

From source file:de.grobmeier.jjson.convert.JSONAnnotationEncoder.java

private void encodeArray(Object result, StringBuilder builder) throws JSONException {
    builder.append(ARRAY_LEFT);/*  w w  w .  ja v  a2s . c om*/
    int length = Array.getLength(result);
    for (int i = 0; i < length; i++) {
        if (i > 0) {
            builder.append(COMMA);
        }
        encode(Array.get(result, i), builder, null);
    }
    builder.append(ARRAY_RIGHT);
}

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.rhino.NativeArrayConverter.java

/**
 * {@inheritDoc}/*from w  ww . ja  v  a  2  s.  com*/
 */
@Override
public boolean canConvertValueForScript(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    boolean canConvert = expectedClass.isAssignableFrom(NativeArray.class);

    if (canConvert) {
        if (value instanceof Collection<?>) {
            final Collection<?> coll = (Collection<?>) value;
            for (final Object element : coll) {
                canConvert = canConvert && globalDelegate.canConvertValueForScript(element);

                if (!canConvert) {
                    break;
                }
            }
        } else if (value.getClass().isArray()) {
            final int length = Array.getLength(value);
            for (int idx = 0; idx < length && canConvert; idx++) {
                canConvert = canConvert && globalDelegate.canConvertValueForScript(Array.get(value, idx));
            }
        } else {
            canConvert = false;
        }
    }

    return canConvert;
}