Example usage for org.apache.commons.lang3 ArrayUtils isSameLength

List of usage examples for org.apache.commons.lang3 ArrayUtils isSameLength

Introduction

In this page you can find the example usage for org.apache.commons.lang3 ArrayUtils isSameLength.

Prototype

public static boolean isSameLength(final boolean[] array1, final boolean[] array2) 

Source Link

Document

Checks whether two arrays are the same length, treating null arrays as length 0 .

Usage

From source file:hu.bme.mit.sette.common.model.snippet.Snippet.java

/**
 * Parses the methods which should be considered in coverage.
 *
 * @param annotation/*ww  w  .  j  a va  2s.co m*/
 *            the {@link SetteIncludeCoverage} annotation
 * @param v
 *            a {@link MethodValidator}
 * @param classLoader
 *            the class loader for loading snippet project classes
 */
private void parseIncludedMethods(final SetteIncludeCoverage annotation, final MethodValidator v,
        final ClassLoader classLoader) {
    if (annotation == null) {
        return;
    }

    Class<?>[] includedClasses = annotation.classes();
    String[] includedMethodStrings = annotation.methods();
    boolean shouldParse = true; // only parse if no validation error

    // check the arrays: not empty, no null element, same lengths
    if (ArrayUtils.isEmpty(includedClasses)) {
        v.addException("The included class list must not be empty");
        shouldParse = false;
    }

    if (ArrayUtils.contains(includedClasses, null)) {
        v.addException("The included class list " + "must not contain null elements");
        shouldParse = false;
    }

    if (ArrayUtils.isEmpty(includedMethodStrings)) {
        v.addException("The included method list must not be empty");
        shouldParse = false;
    }

    if (ArrayUtils.contains(includedMethodStrings, null)) {
        v.addException("The included method list " + "must not contain null elements");
        shouldParse = false;
    }

    if (!ArrayUtils.isSameLength(includedClasses, includedMethodStrings)) {
        v.addException("The included class list and method list " + "must have the same length");
        shouldParse = false;
    }

    if (shouldParse) {
        // check and add methods
        for (int i = 0; i < includedClasses.length; i++) {
            Class<?> includedClass = includedClasses[i];
            String includedMethodString = includedMethodStrings[i].trim();

            if (includedMethodString.equals("*")) {
                // add all non-synthetic constructors
                for (Constructor<?> c : includedClass.getDeclaredConstructors()) {
                    if (!c.isSynthetic()) {
                        addIncludedConstructor(c, v);
                    }
                }
                // add all non-synthetic methods
                for (Method m : includedClass.getDeclaredMethods()) {
                    if (!m.isSynthetic()) {
                        addIncludedMethod(m, v);
                    }
                }
            } else {
                parseIncludedMethod(includedClass, includedMethodString, v, classLoader);
            }
        }
    }
}

From source file:fr.landel.utils.io.FileUtils.java

private static boolean isIdentical(final File file1, final File file2) {
    boolean result = true;

    try (BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(file1));
            BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream(file2))) {

        final byte[] buffer1 = new byte[BUFFER_SIZE];
        final byte[] buffer2 = new byte[BUFFER_SIZE];

        while (result && bis1.read(buffer1, 0, BUFFER_SIZE) > 0 && bis2.read(buffer2, 0, BUFFER_SIZE) > 0) {
            if (!ArrayUtils.isSameLength(buffer1, buffer2) || !Arrays.equals(buffer1, buffer2)) {
                result = false;/*from w ww  .ja v  a 2  s .c  o m*/
            }
        }
    } catch (IOException e) {
        result = false;
    }

    return result;
}

From source file:objenome.util.ClassUtils.java

/**
 * <p>Checks if an array of Classes can be assigned to another array of Classes.</p>
 *
 * <p>This method calls {@link #isAssignable(Class, Class) isAssignable} for each
 * Class pair in the input arrays. It can be used to check if a set of arguments
 * (the first parameter) are suitably compatible with a set of method parameter types
 * (the second parameter).</p>/*from w ww .  jav  a2  s  .c  o m*/
 *
 * <p>Unlike the {@link Class#isAssignableFrom(Class)} method, this
 * method takes into account widenings of primitive classes and
 * {@code null}s.</p>
 *
 * <p>Primitive widenings allow an int to be assigned to a {@code long},
 * {@code float} or {@code double}. This method returns the correct
 * result for these cases.</p>
 *
 * <p>{@code Null} may be assigned to any reference type. This method will
 * return {@code true} if {@code null} is passed in and the toClass is
 * non-primitive.</p>
 *
 * <p>Specifically, this method tests whether the type represented by the
 * specified {@code Class} parameter can be converted to the type
 * represented by this {@code Class} object via an identity conversion
 * widening primitive or widening reference conversion. See
 * <em><a href="http://docs.oracle.com/javase/specs/">The Java Language Specification</a></em>,
 * sections 5.1.1, 5.1.2 and 5.1.4 for details.</p>
 *
 * @param classArray  the array of Classes to check, may be {@code null}
 * @param toClassArray  the array of Classes to try to assign into, may be {@code null}
 * @param autoboxing  whether to use implicit autoboxing/unboxing between primitives and wrappers
 * @return {@code true} if assignment possible
 */
public static boolean isAssignable(Class<?>[] classArray, Class<?>[] toClassArray, boolean autoboxing) {
    if (!ArrayUtils.isSameLength(classArray, toClassArray)) {
        return false;
    }
    if (classArray == null) {
        classArray = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    if (toClassArray == null) {
        toClassArray = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    for (int i = 0; i < classArray.length; i++) {
        if (!isAssignable(classArray[i], toClassArray[i], autoboxing)) {
            return false;
        }
    }
    return true;
}

From source file:org.power.commons.lang.util.ClassUtils.java

/**
 *  <code>fromClasses</code> ??? <code>classes</code>
 * <p>/*from w w w .  j av a 2  s. c o m*/
 * ??? <code>object1, object2, ...</code> ????
 * <code>class1, class2,
 * ...</code> 
 * </p>
 * <p>
 *  <code>fromClasses</code> ? <code>fromClass</code> 
 * <code>classes</code> ? <code>clazz</code> 
 * <ol>
 * <li> <code>clazz</code>  <code>null</code>  <code>false</code>
 * </li>
 * <li>? <code>fromClass</code>  <code>null</code> 
 * <code>clazz</code> ?? <code>true</code>  <code>null</code>
 * ?</li>
 * <li> <code>Class.isAssignableFrom</code> ? <code>clazz</code>
 * ?? <code>fromClass</code> ??? <code>true</code></li>
 * <li> <code>clazz</code> ?? <a
 * href="http://java.sun.com/docs/books/jls/">The Java Language
 * Specification</a> sections 5.1.1, 5.1.2, 5.1.4Widening Primitive
 * Conversion? <code>fromClass</code> ???? 
 * <code>clazz</code>  <code>long</code> ?? <code>byte</code>?
 * <code>short</code>?<code>int</code>?<code>long</code>?<code>char</code>
 * ? <code>java.lang.Byte</code>?<code>java.lang.Short</code>?
 * <code>java.lang.Integer</code>? <code>java.lang.Long</code> 
 * <code>java.lang.Character</code> ? <code>true</code></li>
 * <li>?? <code>false</code></li>
 * </ol>
 * </p>
 *
 * @param classes      <code>null</code>  <code>false</code>
 * @param fromClasses ? <code>null</code> ????
 * @return ? <code>true</code>
 */
public static boolean isAssignable(Class<?>[] classes, Class<?>[] fromClasses) {
    if (!ArrayUtils.isSameLength(fromClasses, classes)) {
        return false;
    }

    if (fromClasses == null) {
        fromClasses = org.power.commons.lang.BasicConstant.EMPTY_CLASS_ARRAY;
    }

    if (classes == null) {
        classes = org.power.commons.lang.BasicConstant.EMPTY_CLASS_ARRAY;
    }

    for (int i = 0; i < fromClasses.length; i++) {
        if (isAssignable(classes[i], fromClasses[i]) == false) {
            return false;
        }
    }

    return true;
}

From source file:yoyo.framework.standard.shared.commons.lang.ArrayUtilsTest.java

@Test
public void test() {
    assertThat(ArrayUtils.EMPTY_BOOLEAN_ARRAY.length, is(0));
    assertThat(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY, is(new Boolean[] {}));
    boolean[] testee = ArrayUtils.add(null, true);
    assertThat(ArrayUtils.clone(testee), is(testee));
    assertThat(ArrayUtils.contains(testee, true), is(true));
    assertThat(ArrayUtils.contains(testee, false), is(false));
    assertThat(ArrayUtils.getLength(testee), is(1));
    assertThat(ArrayUtils.hashCode(testee), is(not(nullValue())));
    assertThat(ArrayUtils.indexOf(testee, true), is(not(ArrayUtils.INDEX_NOT_FOUND)));
    assertThat(ArrayUtils.indexOf(testee, false), is(ArrayUtils.INDEX_NOT_FOUND));
    assertThat(ArrayUtils.isEmpty(testee), is(false));
    assertThat(ArrayUtils.isEmpty(ArrayUtils.EMPTY_BOOLEAN_ARRAY), is(true));
    assertThat(ArrayUtils.isEquals(testee, new boolean[] { true }), is(true));
    assertThat(ArrayUtils.isNotEmpty(testee), is(true));
    assertThat(ArrayUtils.isSameLength(testee, new boolean[] { true }), is(true));
    assertThat(ArrayUtils.isSameType(testee, new boolean[] { true }), is(true));
    assertThat(ArrayUtils.lastIndexOf(testee, true), is(not(ArrayUtils.INDEX_NOT_FOUND)));
    assertThat(ArrayUtils.lastIndexOf(testee, false), is(ArrayUtils.INDEX_NOT_FOUND));
    boolean[] booleanNull = null;
    assertThat(ArrayUtils.nullToEmpty(booleanNull), is(ArrayUtils.EMPTY_BOOLEAN_ARRAY));
    assertThat(ArrayUtils.remove(testee, 0), is(ArrayUtils.EMPTY_BOOLEAN_ARRAY));
    assertThat(ArrayUtils.removeAll(testee, 0), is(ArrayUtils.EMPTY_BOOLEAN_ARRAY));
    assertThat(ArrayUtils.removeElement(testee, true), is(ArrayUtils.EMPTY_BOOLEAN_ARRAY));
    assertThat(ArrayUtils.removeElement(testee, false), is(testee));
    assertThat(ArrayUtils.removeElements(testee, true, false), is(ArrayUtils.EMPTY_BOOLEAN_ARRAY));
}