Java String Ends With endsWith(final Object[] left, final Object[] right, final boolean equals)

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

Description

Tests whether the first array ends with the second array.

License

Open Source License

Parameter

Parameter Description
left The array to check (larger); may be <code>null</code>.
right The array that should be a subsequence (smaller); may be <code>null</code>.
equals Whether the two array are allowed to be equal.

Return

true if the second array is a subsequence of the array list, and they share end elements.

Declaration

public static final boolean endsWith(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://  ww w .j  a  v  a2  s  .  c  o  m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Tests whether the first array ends with the second array.
     *
     * @param left
     *            The array to check (larger); may be <code>null</code>.
     * @param right
     *            The array that should be a subsequence (smaller); may be <code>null</code>.
     * @param equals
     *            Whether the two array are allowed to be equal.
     * @return <code>true</code> if the second array is a subsequence of the array list, and they
     *         share end elements.
     */
    public static final boolean endsWith(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[l - i - 1], right[r - i - 1])) {
                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. endsWith(final byte[] big, final byte[] suffix)
  2. endsWith(final byte[] str1, int startIndex1, int endIndex1, final byte[] str2, int startIndex2, int endIndex2)
  3. endsWith(final CharSequence a, final CharSequence b)
  4. endsWith(final CharSequence str, final CharSequence suffix)
  5. endsWith(final CharSequence target, final CharSequence suffix)
  6. endsWith(final String path, final String suffix)
  7. endsWith(final String s, final String suffix, final boolean ignoreCase)
  8. endsWith(final String src, final String... suffixes)
  9. endsWith(final String str, final char suffix)