Java List Compare compareTermList(final List tl0, final List tl1)

Here you can find the source of compareTermList(final List tl0, final List tl1)

Description

Compare two term lists by comparing their subterms in order.

License

Artistic License

Parameter

Parameter Description
Term The subterm type, must derive from IDLTerm .
tl0 The first term list
tl1 The second term list <p/>

Return

-1 , if tl0 < tl1 , 0 , if tl0.equals(tl1) and 1 , if tl0 > tl1 .

Declaration

public static <Term extends Comparable<? super Term>> int compareTermList(final List<Term> tl0,
        final List<? extends Term> tl1) 

Method Source Code

//package com.java2s;
/**/* ww  w  .j  a v  a2 s.co  m*/
 * (c) 2009-2014 Otto-Friedrich-University Bamberg
 *
 * $Id$
 *
 * Use, modification and restribution of this file are covered by the
 * terms of the Artistic License 2.0.
 *
 * You should have received a copy of the license terms in a file named
 * "LICENSE" together with this software package.
 *
 * Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT
 * HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
 * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE
 * EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO
 * COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT
 * OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 **/

import java.util.List;

public class Main {
    /**
     * Compare two term lists by comparing their subterms in order. <p />
     * The comparison of the first non-equal subterm decides. If the first {@literal min(tl0.size(), tl1.size())} of
     * both term lists are equal, the term length is used to decide.
     *
     * @param <Term> The subterm type, must derive from {@link IDLTerm}.
     * @param tl0    The first term list
     * @param tl1    The second term list
     * <p/>
     * @return {@literal -1}, if {@literal tl0 &lt; tl1}, {@literal 0}, if {@literal tl0.equals(tl1)} and {@literal 1},
     *            if {@literal tl0 &gt; tl1}.
     */
    public static <Term extends Comparable<? super Term>> int compareTermList(final List<Term> tl0,
            final List<? extends Term> tl1) {
        int minSize = Math.min(tl0.size(), tl1.size());
        int compare = 0;
        /*
         * compareTermList for common initial subterms
         */
        for (int i = 0; (compare == 0) && (i < minSize); ++i) {
            compare = tl0.get(i).compareTo(tl1.get(i));
        }
        /*
         * common subterms match, term length decides
         */
        if (compare == 0) {
            compare = tl1.size() - tl0.size();
        }

        if (compare < 0) {
            return -1;
        } else if (compare > 0) {
            return 1;
        } else {
            return 0;
        }
    }
}

Related

  1. compareListsAndNull(List arg1, List arg2)
  2. comparer(Comparator comparator, T object, T... withList)
  3. compareStringList(List list1, List list2)
  4. compareStringLists(List elements1, List elements2)
  5. compareSubstring(List pieces, String s)
  6. compareTo(List list, int i, int j)
  7. compareTwoListOfStrings(List first, List second)
  8. compareVectors(List vectorA, List vectorB)
  9. equalsToAny(Object comparable, Object... compareList)