Java Array Region Match arrayRegionMatches(Object[] source, int sourceStart, Object[] target, int targetStart, int len)

Here you can find the source of arrayRegionMatches(Object[] source, int sourceStart, Object[] target, int targetStart, int len)

Description

Convenience utility to compare two Object[]s Ought to be in System.

License

Open Source License

Parameter

Parameter Description
len the length to compare. The start indices and start+len must be valid.

Declaration

final static boolean arrayRegionMatches(Object[] source, int sourceStart, Object[] target, int targetStart,
        int len) 

Method Source Code

//package com.java2s;
/*/*from   w  ww.  j  a v a  2 s .  com*/
 * 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.
     * @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;
    }

    /**
     * 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);
    }
}

Related

  1. arrayRegionMatches(char[] source, int sourceStart, char[] target, int targetStart, int len)
  2. arrayRegionMatches(Object[] source, int sourceStart, Object[] target, int targetStart, int len)
  3. arrayRegionsEqual(boolean[] a, int aoff, int alen, boolean[] b, int boff, int blen)