Java Array Equal arrayEquals(Object[] source, Object target)

Here you can find the source of arrayEquals(Object[] source, Object target)

Description

Convenience utility to compare two Object[]s.

License

Open Source License

Declaration

public final static boolean arrayEquals(Object[] source, Object target) 

Method Source Code

//package com.java2s;
// License & terms of use: http://www.unicode.org/copyright.html#License

public class Main {
    /**//from   w  w  w.  ja  v  a2 s . c  o m
     * Convenience utility to compare two Object[]s.
     * Ought to be in System
     */
    public final static boolean arrayEquals(Object[] source, Object target) {
        if (source == null)
            return (target == null);
        if (!(target instanceof Object[]))
            return false;
        Object[] targ = (Object[]) target;
        return (source.length == targ.length && arrayRegionMatches(source,
                0, targ, 0, source.length));
    }

    /**
     * Convenience utility to compare two int[]s
     * Ought to be in System
     */
    public final static boolean arrayEquals(int[] source, Object target) {
        if (source == null)
            return (target == null);
        if (!(target instanceof int[]))
            return false;
        int[] targ = (int[]) target;
        return (source.length == targ.length && arrayRegionMatches(source,
                0, targ, 0, source.length));
    }

    /**
     * Convenience utility to compare two double[]s
     * Ought to be in System
     */
    public final static boolean arrayEquals(double[] source, Object target) {
        if (source == null)
            return (target == null);
        if (!(target instanceof double[]))
            return false;
        double[] targ = (double[]) target;
        return (source.length == targ.length && arrayRegionMatches(source,
                0, targ, 0, source.length));
    }

    public final static boolean arrayEquals(byte[] source, Object target) {
        if (source == null)
            return (target == null);
        if (!(target instanceof byte[]))
            return false;
        byte[] targ = (byte[]) target;
        return (source.length == targ.length && arrayRegionMatches(source,
                0, targ, 0, source.length));
    }

    /**
     * Convenience utility to compare two Object[]s
     * Ought to be in System
     */
    public final static boolean arrayEquals(Object source, Object target) {
        if (source == null)
            return (target == null);
        // for some reason, the correct arrayEquals is not being called
        // so do it by hand for now.
        if (source instanceof Object[])
            return (arrayEquals((Object[]) source, target));
        if (source instanceof int[])
            return (arrayEquals((int[]) source, target));
        if (source instanceof double[])
            return (arrayEquals((double[]) source, target));
        if (source instanceof byte[])
            return (arrayEquals((byte[]) source, target));
        return source.equals(target);
    }

    /**
     * Convenience utility to compare two Object[]s
     * Ought to be in System.
     * @param len the length to compare.
     * The start indices and start+len must be valid.
     */
    public final static boolean arrayRegionMatches(Object[] source,
            int sourceStart, Object[] target, int targetStart, int len) {
        int sourceEnd = sourceStart + len;
        int delta = targetStart - sourceStart;
        for (int i = sourceStart; i < sourceEnd; i++) {
            if (!arrayEquals(source[i], target[i + delta]))
                return false;
        }
        return true;
    }

    /**
     * Convenience utility to compare two Object[]s
     * Ought to be in System.
     * @param len the length to compare.
     * The start indices and start+len must be valid.
     */
    public final static boolean arrayRegionMatches(char[] source,
            int sourceStart, char[] target, int targetStart, int len) {
        int sourceEnd = sourceStart + len;
        int delta = targetStart - sourceStart;
        for (int i = sourceStart; i < sourceEnd; i++) {
            if (source[i] != target[i + delta])
                return false;
        }
        return true;
    }

    /**
     * Convenience utility to compare two int[]s.
     * @param len the length to compare.
     * The start indices and start+len must be valid.
     * Ought to be in System
     */
    public final static boolean arrayRegionMatches(int[] source,
            int sourceStart, int[] target, int targetStart, int len) {
        int sourceEnd = sourceStart + len;
        int delta = targetStart - sourceStart;
        for (int i = sourceStart; i < sourceEnd; i++) {
            if (source[i] != target[i + delta])
                return false;
        }
        return true;
    }

    /**
     * Convenience utility to compare two arrays of doubles.
     * @param len the length to compare.
     * The start indices and start+len must be valid.
     * Ought to be in System
     */
    public final static boolean arrayRegionMatches(double[] source,
            int sourceStart, double[] target, int targetStart, int len) {
        int sourceEnd = sourceStart + len;
        int delta = targetStart - sourceStart;
        for (int i = sourceStart; i < sourceEnd; i++) {
            if (source[i] != target[i + delta])
                return false;
        }
        return true;
    }

    public final static boolean arrayRegionMatches(byte[] source,
            int sourceStart, byte[] target, int targetStart, int len) {
        int sourceEnd = sourceStart + len;
        int delta = targetStart - sourceStart;
        for (int i = sourceStart; i < sourceEnd; i++) {
            if (source[i] != target[i + delta])
                return false;
        }
        return true;
    }
}

Related

  1. arrayEquals(Object[] a, Object[] b)
  2. arrayEquals(Object[] a, Object[] b)
  3. arrayEquals(Object[] arr1, Object[] arr2)
  4. arrayEquals(Object[] array1, Object[] array2)
  5. arrayEquals(Object[] source, Object target)
  6. arrayEquals(String[] array1, String[] array2)
  7. arrayEquals(String[] targetMethodParamTypes, String[] paramTypes)
  8. arrayEqualsExceptNull(Object[] a1, Object[] a2)
  9. arrayEqualsSubset(final byte[] array, final int... elements)