Java List Ends with endsWith(List left, List right, boolean equals)

Here you can find the source of endsWith(List left, List right, boolean equals)

Description

ends With

License

Open Source License

Declaration

public static boolean endsWith(List left, List right, boolean equals) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2010 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   ww w . j  av a 2  s.co  m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.util.List;

public class Main {
    public static boolean endsWith(List left, List right, boolean equals) {
        if (left == null || right == null) {
            return false;
        } else {
            int l = left.size();
            int r = right.size();

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

            for (int i = 0; i < r; i++) {
                if (!equals(left.get(l - i - 1), right.get(r - i - 1))) {
                    return false;
                }
            }

            return true;
        }
    }

    public static boolean endsWith(Object[] left, Object[] right, boolean equals) {
        if (left == null || right == null) {
            return false;
        } else {
            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;
        }
    }

    public static boolean equals(boolean left, boolean right) {
        return left == right;
    }

    public static boolean equals(int left, int right) {
        return left == right;
    }

    public static boolean equals(Object left, 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(List left, List right, boolean equals)
  2. endsWith(List source, List prefix)
  3. endsWith(List hierarchy, String... items)
  4. endsWith(List lst, List suffix)