Java String Starts Wtih startsWith(final Object[] left, final Object[] right, final boolean equals)

Here you can find the source of startsWith(final Object[] left, final Object[] right, final boolean equals)

Description

Checks whether the second array is a subsequence of the first array, and that they share common starting elements.

License

Open Source License

Parameter

Parameter Description
left The first array to compare (large); may be <code>null</code>.
right The second array to compare (small); may be <code>null</code>.
equals Whether it is allowed for the two arrays to be equivalent.

Return

true if the first arrays starts with the second list; false otherwise.

Declaration

public static final boolean startsWith(final Object[] left, final Object[] right, final boolean equals) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  w w.  j av a2s . c o m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Checks whether the second array is a subsequence of the first array, and that they share
     * common starting elements.
     *
     * @param left
     *            The first array to compare (large); may be <code>null</code>.
     * @param right
     *            The second array to compare (small); may be <code>null</code>.
     * @param equals
     *            Whether it is allowed for the two arrays to be equivalent.
     * @return <code>true</code> if the first arrays starts with the second list; <code>false</code>
     *         otherwise.
     */
    public static final boolean startsWith(final Object[] left, final Object[] right, final boolean equals) {
        if (left == null || right == null) {
            return false;
        }

        int l = left.length;
        int r = right.length;

        if (r > l || !equals && r == l) {
            return false;
        }

        for (int i = 0; i < r; i++) {
            if (!equals(left[i], right[i])) {
                return false;
            }
        }

        return true;
    }

    /**
     * Checks whether the two objects are <code>null</code> -- allowing for <code>null</code>.
     *
     * @param left
     *            The left object to compare; may be <code>null</code>.
     * @param right
     *            The right object to compare; may be <code>null</code>.
     * @return <code>true</code> if the two objects are equivalent; <code>false</code> otherwise.
     */
    public static final boolean equals(final Object left, final Object right) {
        return left == null ? right == null : ((right != null) && left.equals(right));
    }

    /**
     * Tests whether two arrays of objects are equal to each other. The arrays must not be
     * <code>null</code>, but their elements may be <code>null</code>.
     *
     * @param leftArray
     *            The left array to compare; may be <code>null</code>, and may be empty and may
     *            contain <code>null</code> elements.
     * @param rightArray
     *            The right array to compare; may be <code>null</code>, and may be empty and may
     *            contain <code>null</code> elements.
     * @return <code>true</code> if the arrays are equal length and the elements at the same
     *         position are equal; <code>false</code> otherwise.
     */
    public static final boolean equals(final Object[] leftArray, final Object[] rightArray) {
        if (leftArray == rightArray) {
            return true;
        }

        if (leftArray == null) {
            return (rightArray == null);
        } else if (rightArray == null) {
            return false;
        }

        if (leftArray.length != rightArray.length) {
            return false;
        }

        for (int i = 0; i < leftArray.length; i++) {
            final Object left = leftArray[i];
            final Object right = rightArray[i];
            final boolean equal = (left == null) ? (right == null) : (left.equals(right));
            if (!equal) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. startsWith(CharSequence str, CharSequence prefix)
  2. startsWith(final boolean caseSensitive, final char[] text, final char[] prefix)
  3. startsWith(final CharSequence str, final CharSequence prefix)
  4. startsWith(final CharSequence target, final CharSequence prefix)
  5. startsWith(final E[] array, final E[] prefix)
  6. startsWith(final String fullMethodName, final String prefix)
  7. startsWith(final String s, final String prefix, final boolean ignoreCase)
  8. startsWith(final String s, final String[] prefixes)
  9. startsWith(final String searchString, final String crit)