Example usage for java.util SortedSet comparator

List of usage examples for java.util SortedSet comparator

Introduction

In this page you can find the example usage for java.util SortedSet comparator.

Prototype

Comparator<? super E> comparator();

Source Link

Document

Returns the comparator used to order the elements in this set, or null if this set uses the Comparable natural ordering of its elements.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SortedSet<String> set = new TreeSet<String>();
    set.add("b");
    set.add("c");
    set.add("a");

    System.out.println(set.comparator());
}

From source file:Main.java

public static <T> boolean isNaturalSortedSet(Iterable<? extends T> it) {
    if (it instanceof SortedSet) {
        SortedSet<? extends T> sortedSet = (SortedSet<? extends T>) it;
        Comparator<?> comparator = sortedSet.comparator();
        return (comparator == null) || comparator.equals(Ordering.natural());
    }/*from   w w w  .j av  a  2 s. co m*/
    return false;
}

From source file:Main.java

public static <T> boolean isNaturalSortedSet(@Nonnull Iterable<? extends T> it) {
    if (it instanceof SortedSet) {
        SortedSet<? extends T> sortedSet = (SortedSet<? extends T>) it;
        Comparator<?> comparator = sortedSet.comparator();
        return (comparator == null) || comparator.equals(Ordering.natural());
    }//  w w  w  .j a v a 2 s.co m
    return false;
}

From source file:Main.java

public static <T> boolean isSortedSet(Comparator<? extends T> elementComparator, Iterable<? extends T> it) {
    if (it instanceof SortedSet) {
        SortedSet<? extends T> sortedSet = (SortedSet<? extends T>) it;
        Comparator<?> comparator = sortedSet.comparator();
        if (comparator == null) {
            return elementComparator.equals(Ordering.natural());
        }/* w  ww. j  a  v a2  s . c  om*/
        return elementComparator.equals(comparator);
    }
    return false;
}

From source file:Main.java

public static <T> boolean isSortedSet(@Nonnull Comparator<? extends T> elementComparator,
        @Nonnull Iterable<? extends T> it) {
    if (it instanceof SortedSet) {
        SortedSet<? extends T> sortedSet = (SortedSet<? extends T>) it;
        Comparator<?> comparator = sortedSet.comparator();
        if (comparator == null) {
            return elementComparator.equals(Ordering.natural());
        }/*  w w w  . j a va 2  s.  c o m*/
        return elementComparator.equals(comparator);
    }
    return false;
}

From source file:Main.java

@Nonnull
private static <T> SortedSet<? extends T> toSortedSet(@Nonnull Comparator<? super T> elementComparator,
        @Nonnull Collection<? extends T> collection) {
    if (collection instanceof SortedSet) {
        SortedSet<? extends T> sortedSet = (SortedSet<? extends T>) collection;
        Comparator<?> comparator = sortedSet.comparator();
        if (comparator != null && comparator.equals(elementComparator)) {
            return sortedSet;
        }//from ww  w. j av a 2s .c  o  m
    }
    return ImmutableSortedSet.copyOf(elementComparator, collection);
}

From source file:Main.java

private static <T> SortedSet<? extends T> toSortedSet(Comparator<? super T> elementComparator,
        Collection<? extends T> collection) {
    if (collection instanceof SortedSet) {
        SortedSet<? extends T> sortedSet = (SortedSet<? extends T>) collection;
        Comparator<?> comparator = sortedSet.comparator();
        if (comparator != null && comparator.equals(elementComparator)) {
            return sortedSet;
        }//from   w  w w . j a  v  a  2 s  . co  m
    }
    return ImmutableSortedSet.copyOf(elementComparator, collection);
}

From source file:Main.java

public static boolean containsAll(Collection a, Collection b) {
    // fast paths
    if (a == b)/*from   w ww  .  jav  a  2s.  c  o  m*/
        return true;
    if (b.size() == 0)
        return true;
    if (a.size() < b.size())
        return false;

    if (a instanceof SortedSet && b instanceof SortedSet) {
        SortedSet aa = (SortedSet) a;
        SortedSet bb = (SortedSet) b;
        Comparator bbc = bb.comparator();
        Comparator aac = aa.comparator();
        if (bbc == null && aac == null) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Comparable ao = (Comparable) ai.next(); // these are ok, since the sizes are != 0
            Comparable bo = (Comparable) bi.next();
            while (true) {
                int rel = ao.compareTo(bo);
                if (rel == 0) {
                    if (!bi.hasNext())
                        return true;
                    if (!ai.hasNext())
                        return false;
                    bo = (Comparable) bi.next();
                    ao = (Comparable) ai.next();
                } else if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = (Comparable) ai.next();
                } else {
                    return false;
                }
            }
        } else if (bbc.equals(aac)) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Object ao = ai.next(); // these are ok, since the sizes are != 0
            Object bo = bi.next();
            while (true) {
                int rel = aac.compare(ao, bo);
                if (rel == 0) {
                    if (!bi.hasNext())
                        return true;
                    if (!ai.hasNext())
                        return false;
                    bo = bi.next();
                    ao = ai.next();
                } else if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = ai.next();
                } else {
                    return false;
                }
            }
        }
    }
    return a.containsAll(b);
}

From source file:Main.java

/**
 * @param a//from   www . j ava2s .  c o m
 * @param b
 * @return
 */
public static boolean containsSome(Collection a, Collection b) {
    // fast paths
    if (a.size() == 0 || b.size() == 0)
        return false;
    if (a == b)
        return true; // must test after size test.

    if (a instanceof SortedSet && b instanceof SortedSet) {
        SortedSet aa = (SortedSet) a;
        SortedSet bb = (SortedSet) b;
        Comparator bbc = bb.comparator();
        Comparator aac = aa.comparator();
        if (bbc == null && aac == null) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Comparable ao = (Comparable) ai.next(); // these are ok, since the sizes are != 0
            Comparable bo = (Comparable) bi.next();
            while (true) {
                int rel = ao.compareTo(bo);
                if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = (Comparable) ai.next();
                } else if (rel > 0) {
                    if (!bi.hasNext())
                        return false;
                    bo = (Comparable) bi.next();
                } else {
                    return true;
                }
            }
        } else if (bbc.equals(a)) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Object ao = ai.next(); // these are ok, since the sizes are != 0
            Object bo = bi.next();
            while (true) {
                int rel = aac.compare(ao, bo);
                if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = ai.next();
                } else if (rel > 0) {
                    if (!bi.hasNext())
                        return false;
                    bo = bi.next();
                } else {
                    return true;
                }
            }
        }
    }
    for (Iterator it = a.iterator(); it.hasNext();) {
        if (b.contains(it.next()))
            return true;
    }
    return false;
}

From source file:edu.utah.further.core.api.collections.SortedSetComparator.java

/**
 * Compare two {@link Double}s. If {@link #sortingOrder} is
 * {@link SortingOrder#ASCENDING}, this is their natural ordering. If
 * {@link #sortingOrder} is {@link SortingOrder#DESCENDING}, the order is reversed.
 * /*from w  w  w  .j  a va  2 s. com*/
 * @param o1
 *            left operand
 * @param o2
 *            right operand
 * @return result of comparison of <code>o1</code> and <code>o2</code>
 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
 */
@Override
public int compare(final SortedSet<E> o1, final SortedSet<E> o2) {
    ValidationUtil.validateIsTrue(o1.comparator() == null, "First set must have natural ordering");
    ValidationUtil.validateIsTrue(o2.comparator() == null, "Second set must have natural ordering");

    final CompareToBuilder builder = new CompareToBuilder();

    // Compare the first corresponding min(o1.size(),o2.size()) element pairs
    final Iterator<E> iterator2 = o2.iterator();
    for (final E element1 : o1) {
        if (!iterator2.hasNext()) {
            // o2.size() < o1.size()
            break;
        }
        // Pair exists, add to comparison
        builder.append(element1, iterator2.next());
    }

    // If we're still tied, compare by set sizes
    return builder.append(o1.size(), o2.size()).toComparison();
}