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

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

Method Source Code

//package com.java2s;
/*// w w w .ja va 2s  .c  o m
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

public class Main {
    /**
     * Convenience utility to compare two Object[]s.
     * Ought to be in System
     */
    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
     */
    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
     */
    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));
    }

    /**
     * Convenience utility to compare two Object[]s
     * Ought to be in System
     */
    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((int[]) 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.
     */
    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 int[]s.
     * @param len the length to compare.
     * The start indices and start+len must be valid.
     * Ought to be in System
     */
    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
     */
    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;
    }
}

Related

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