Java List Compare canonicalCompare(List o1, List o2, Comparator elemComparator)

Here you can find the source of canonicalCompare(List o1, List o2, Comparator elemComparator)

Description

Compares two List s with respect to canonical ordering.

License

Open Source License

Parameter

Parameter Description
o1 the first list
o2 the second list
elemComparator the comparator for comparing the single elements

Return

the result of the comparison

Declaration

public static <U> int canonicalCompare(List<? extends U> o1, List<? extends U> o2,
        Comparator<? super U> elemComparator) 

Method Source Code


//package com.java2s;
/* Copyright (C) 2013 TU Dortmund
 * This file is part of AutomataLib, http://www.automatalib.net/.
 * //from w  w  w  .j a va2 s  .c  o  m
 * AutomataLib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 3.0 as published by the Free Software Foundation.
 * 
 * AutomataLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with AutomataLib; if not, see
 * http://www.gnu.de/documents/lgpl.en.html.
 */

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Compares two {@link List}s with respect to canonical ordering.
     * <p>
     * In canonical ordering, a sequence <tt>o1</tt> is less than a sequence <tt>o2</tt> if <tt>o1</tt> is shorter
     * than <tt>o2</tt>, or if they have the same length and <tt>o1</tt> is lexicographically smaller than <tt>o2</tt>. 
     * @param o1 the first list
     * @param o2 the second list
     * @param elemComparator the comparator for comparing the single elements
     * @return the result of the comparison
     */
    public static <U> int canonicalCompare(List<? extends U> o1, List<? extends U> o2,
            Comparator<? super U> elemComparator) {
        int siz1 = o1.size(), siz2 = o2.size();

        if (siz1 != siz2) {
            return siz1 - siz2;
        }

        return lexCompare(o1, o2, elemComparator);
    }

    /**
     * Compares two {@link List}s of {@link Comparable} elements with respect to canonical ordering.
     * <p>
     * In canonical ordering, a sequence <tt>o1</tt> is less than a sequence <tt>o2</tt> if <tt>o1</tt> is shorter
     * than <tt>o2</tt>, or if they have the same length and <tt>o1</tt> is lexicographically smaller than <tt>o2</tt>.
     * @param o1 the first list
     * @param o2 the second list
     * @return the result of the comparison
     */
    public static <U extends Comparable<? super U>> int canonicalCompare(List<? extends U> o1,
            List<? extends U> o2) {
        int siz1 = o1.size(), siz2 = o2.size();
        if (siz1 != siz2) {
            return siz1 - siz2;
        }

        return lexCompare(o1, o2);
    }

    /**
     * Lexicographically compares two {@link Iterable}s. Comparison of the
     * elements is done using the specified comparator.
     * 
     * @param o1 the first iterable.
     * @param o2 the second iterable.
     * @param elemComparator the comparator.
     * @return <code>&lt; 0</code> iff o1 is lexicographically smaller,
     * <code>0</code> if o1 equals o2 and <code>&gt; 0</code> otherwise.
     */
    public static <U> int lexCompare(Iterable<? extends U> o1, Iterable<? extends U> o2,
            Comparator<U> elemComparator) {
        Iterator<? extends U> it1 = o1.iterator(), it2 = o2.iterator();

        while (it1.hasNext() && it2.hasNext()) {
            int cmp = elemComparator.compare(it1.next(), it2.next());
            if (cmp != 0)
                return cmp;
        }

        if (it1.hasNext())
            return 1;
        else if (it2.hasNext())
            return -1;
        return 0;
    }

    /**
     * Lexicographically compares two {@link Iterable}s, whose element types
     * are comparable.
     * @param o1
     * @param o2
     * @return
     */
    public static <U extends Comparable<? super U>> int lexCompare(Iterable<? extends U> o1,
            Iterable<? extends U> o2) {
        Iterator<? extends U> it1 = o1.iterator(), it2 = o2.iterator();

        while (it1.hasNext() && it2.hasNext()) {
            int cmp = it1.next().compareTo(it2.next());
            if (cmp != 0)
                return cmp;
        }

        if (it1.hasNext())
            return 1;
        else if (it2.hasNext())
            return -1;
        return 0;
    }
}

Related

  1. compare(List lhs, Collection rhs, List standard)
  2. compare(List> beanList1, List> beanList2, final int[] sortKeys, final boolean[] sortAscending, final boolean nullAlwaysFirst, final int naOrder)
  3. compare(List list1, List list2)
  4. compare(List a, List b)