Java Code Examples for java.util.SortedSet#comparator()

The following examples show how to use java.util.SortedSet#comparator() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: codebuff   File: Range.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns {@code true} if every element in {@code values} is {@linkplain #contains contained} in
 * this range.
 */
public boolean containsAll(Iterable<? extends C> values) {
  if (Iterables.isEmpty(values)) {
    return true;
  }

  // this optimizes testing equality of two range-backed sets
  if (values instanceof SortedSet) {
    SortedSet<? extends C> set = cast(values);
    Comparator<?> comparator = set.comparator();
    if (Ordering.natural().equals(comparator) || comparator == null) {
      return contains(set.first()) && contains(set.last());
    }
  }

  for (C value : values) {
    if (!contains(value)) {
      return false;
    }
  }
  return true;
}
 
Example 2
/**
 * Adds all of the elements in the specified collection to this set.
 *
 * @param c collection containing elements to be added to this set
 * @return {@code true} if this set changed as a result of the call
 * @throws ClassCastException   if the elements provided cannot be compared
 *                              with the elements currently in the set
 * @throws NullPointerException if the specified collection is null or
 *                              if any element is null and this set uses natural ordering, or
 *                              its comparator does not permit null elements
 */
public boolean addAll(Collection<? extends E> c) {
    // Use linear-time version if applicable
    if (m.size() == 0 && c.size() > 0 &&
            c instanceof SortedSet &&
            m instanceof IndexedTreeMap) {
        SortedSet<? extends E> set = (SortedSet<? extends E>) c;
        IndexedTreeMap<E, Object> map = (IndexedTreeMap<E, Object>) m;
        Comparator<? super E> cc = (Comparator<? super E>) set.comparator();
        Comparator<? super E> mc = map.comparator();
        if (cc == mc || (cc != null && cc.equals(mc))) {
            map.addAllForTreeSet(set, PRESENT);
            return true;
        }
    }
    return super.addAll(c);
}
 
Example 3
public static final boolean isDescending(SortedSet<?> set) {
    if (null == set.comparator()) {
        // natural order
        return false;
    }

    if (Collections.reverseOrder() == set.comparator()) {
        // reverse natural order.
        return true;
    }

    if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {
        // it's a Collections.reverseOrder(Comparator).
        return true;
    }

    throw new IllegalStateException("can't determine ordering for " + set);
}
 
Example 4
public static final boolean isDescending(SortedSet<?> set) {
    if (null == set.comparator()) {
        // natural order
        return false;
    }

    if (Collections.reverseOrder() == set.comparator()) {
        // reverse natural order.
        return true;
    }

    if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {
        // it's a Collections.reverseOrder(Comparator).
        return true;
    }

    throw new IllegalStateException("can't determine ordering for " + set);
}
 
Example 5
Source Project: codebuff   File: Range.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns {@code true} if every element in {@code values} is {@linkplain #contains contained} in
 * this range.
 */


public boolean containsAll(Iterable<? extends C> values) {
  if (Iterables.isEmpty(values)) {
    return true;
  }

  // this optimizes testing equality of two range-backed sets
  if (values instanceof SortedSet) {
    SortedSet<? extends C> set = cast(values);
    Comparator<?> comparator = set.comparator();
    if (Ordering.natural().equals(comparator) || comparator == null) {
      return contains(set.first()) && contains(set.last());
    }
  }
  for (C value : values) {
    if (!contains(value)) {
      return false;
    }
  }
  return true;
}
 
Example 6
Source Project: Bytecoder   File: PriorityBlockingQueue.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@code PriorityBlockingQueue} containing the elements
 * in the specified collection.  If the specified collection is a
 * {@link SortedSet} or a {@link PriorityQueue}, this
 * priority queue will be ordered according to the same ordering.
 * Otherwise, this priority queue will be ordered according to the
 * {@linkplain Comparable natural ordering} of its elements.
 *
 * @param  c the collection whose elements are to be placed
 *         into this priority queue
 * @throws ClassCastException if elements of the specified collection
 *         cannot be compared to one another according to the priority
 *         queue's ordering
 * @throws NullPointerException if the specified collection or any
 *         of its elements are null
 */
public PriorityBlockingQueue(Collection<? extends E> c) {
    boolean heapify = true; // true if not known to be in heap order
    boolean screen = true;  // true if must screen for nulls
    if (c instanceof SortedSet<?>) {
        SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
        this.comparator = (Comparator<? super E>) ss.comparator();
        heapify = false;
    }
    else if (c instanceof PriorityBlockingQueue<?>) {
        PriorityBlockingQueue<? extends E> pq =
            (PriorityBlockingQueue<? extends E>) c;
        this.comparator = (Comparator<? super E>) pq.comparator();
        screen = false;
        if (pq.getClass() == PriorityBlockingQueue.class) // exact match
            heapify = false;
    }
    Object[] es = c.toArray();
    int n = es.length;
    // If c.toArray incorrectly doesn't return Object[], copy it.
    if (es.getClass() != Object[].class)
        es = Arrays.copyOf(es, n, Object[].class);
    if (screen && (n == 1 || this.comparator != null)) {
        for (Object e : es)
            if (e == null)
                throw new NullPointerException();
    }
    this.queue = ensureNonEmpty(es);
    this.size = n;
    if (heapify)
        heapify();
}
 
Example 7
/**
 * Creates a {@code PriorityBlockingQueue} containing the elements
 * in the specified collection.  If the specified collection is a
 * {@link SortedSet} or a {@link PriorityQueue}, this
 * priority queue will be ordered according to the same ordering.
 * Otherwise, this priority queue will be ordered according to the
 * {@linkplain Comparable natural ordering} of its elements.
 *
 * @param  c the collection whose elements are to be placed
 *         into this priority queue
 * @throws ClassCastException if elements of the specified collection
 *         cannot be compared to one another according to the priority
 *         queue's ordering
 * @throws NullPointerException if the specified collection or any
 *         of its elements are null
 */
public PriorityBlockingQueue(Collection<? extends E> c) {
    this.lock = new ReentrantLock();
    this.notEmpty = lock.newCondition();
    boolean heapify = true; // true if not known to be in heap order
    boolean screen = true;  // true if must screen for nulls
    if (c instanceof SortedSet<?>) {
        SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
        this.comparator = (Comparator<? super E>) ss.comparator();
        heapify = false;
    }
    else if (c instanceof PriorityBlockingQueue<?>) {
        PriorityBlockingQueue<? extends E> pq =
            (PriorityBlockingQueue<? extends E>) c;
        this.comparator = (Comparator<? super E>) pq.comparator();
        screen = false;
        if (pq.getClass() == PriorityBlockingQueue.class) // exact match
            heapify = false;
    }
    Object[] a = c.toArray();
    int n = a.length;
    // If c.toArray incorrectly doesn't return Object[], copy it.
    if (a.getClass() != Object[].class)
        a = Arrays.copyOf(a, n, Object[].class);
    if (screen && (n == 1 || this.comparator != null)) {
        for (int i = 0; i < n; ++i)
            if (a[i] == null)
                throw new NullPointerException();
    }
    this.queue = a;
    this.size = n;
    if (heapify)
        heapify();
}
 
Example 8
@SuppressWarnings("unchecked")
// if sortedSet.comparator() is null, the set must be naturally ordered
public static <E> Comparator<? super E> comparator(SortedSet<E> sortedSet) {
  Comparator<? super E> result = sortedSet.comparator();
  if (result == null) {
    result = (Comparator<? super E>) Ordering.natural();
  }
  return result;
}
 
Example 9
void assertSortedSetCharacteristics(SortedSet<Integer> s, int keyCharacteristics) {
    assertSetCharacteristics(s, keyCharacteristics);

    if (s.comparator() != null) {
        assertNotNullComparator(s);
    }
    else {
        assertNullComparator(s);
    }
}
 
Example 10
/**
 * Creates a {@code PriorityBlockingQueue} containing the elements
 * in the specified collection.  If the specified collection is a
 * {@link SortedSet} or a {@link PriorityQueue}, this
 * priority queue will be ordered according to the same ordering.
 * Otherwise, this priority queue will be ordered according to the
 * {@linkplain Comparable natural ordering} of its elements.
 *
 * @param  c the collection whose elements are to be placed
 *         into this priority queue
 * @throws ClassCastException if elements of the specified collection
 *         cannot be compared to one another according to the priority
 *         queue's ordering
 * @throws NullPointerException if the specified collection or any
 *         of its elements are null
 */
public PriorityBlockingQueue(Collection<? extends E> c) {
    this.lock = new ReentrantLock();
    this.notEmpty = lock.newCondition();
    boolean heapify = true; // true if not known to be in heap order
    boolean screen = true;  // true if must screen for nulls
    if (c instanceof SortedSet<?>) {
        SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
        this.comparator = (Comparator<? super E>) ss.comparator();
        heapify = false;
    }
    else if (c instanceof PriorityBlockingQueue<?>) {
        PriorityBlockingQueue<? extends E> pq =
            (PriorityBlockingQueue<? extends E>) c;
        this.comparator = (Comparator<? super E>) pq.comparator();
        screen = false;
        if (pq.getClass() == PriorityBlockingQueue.class) // exact match
            heapify = false;
    }
    Object[] a = c.toArray();
    int n = a.length;
    // If c.toArray incorrectly doesn't return Object[], copy it.
    if (a.getClass() != Object[].class)
        a = Arrays.copyOf(a, n, Object[].class);
    if (screen && (n == 1 || this.comparator != null)) {
        for (int i = 0; i < n; ++i)
            if (a[i] == null)
                throw new NullPointerException();
    }
    this.queue = a;
    this.size = n;
    if (heapify)
        heapify();
}
 
Example 11
Source Project: buck   File: SortedSets.java    License: Apache License 2.0 5 votes vote down vote up
public MergedSortedSetView(SortedSet<T> a, SortedSet<T> b) {
  Preconditions.checkArgument(
      areSameComparators(a.comparator(), b.comparator()),
      "Cannot merge SortedSets with different comparators, got: %s, %s",
      a.comparator(),
      b.comparator());
  this.a = a;
  this.b = b;

  this.comparator = a.comparator() == null ? Ordering.natural() : a.comparator();
}
 
Example 12
Source Project: j2objc   File: PriorityBlockingQueue.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@code PriorityBlockingQueue} containing the elements
 * in the specified collection.  If the specified collection is a
 * {@link SortedSet} or a {@link PriorityQueue}, this
 * priority queue will be ordered according to the same ordering.
 * Otherwise, this priority queue will be ordered according to the
 * {@linkplain Comparable natural ordering} of its elements.
 *
 * @param  c the collection whose elements are to be placed
 *         into this priority queue
 * @throws ClassCastException if elements of the specified collection
 *         cannot be compared to one another according to the priority
 *         queue's ordering
 * @throws NullPointerException if the specified collection or any
 *         of its elements are null
 */
public PriorityBlockingQueue(Collection<? extends E> c) {
    this.lock = new ReentrantLock();
    this.notEmpty = lock.newCondition();
    boolean heapify = true; // true if not known to be in heap order
    boolean screen = true;  // true if must screen for nulls
    if (c instanceof SortedSet<?>) {
        SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
        this.comparator = (Comparator<? super E>) ss.comparator();
        heapify = false;
    }
    else if (c instanceof PriorityBlockingQueue<?>) {
        PriorityBlockingQueue<? extends E> pq =
            (PriorityBlockingQueue<? extends E>) c;
        this.comparator = (Comparator<? super E>) pq.comparator();
        screen = false;
        if (pq.getClass() == PriorityBlockingQueue.class) // exact match
            heapify = false;
    }
    Object[] a = c.toArray();
    int n = a.length;
    // If c.toArray incorrectly doesn't return Object[], copy it.
    if (a.getClass() != Object[].class)
        a = Arrays.copyOf(a, n, Object[].class);
    if (screen && (n == 1 || this.comparator != null)) {
        for (int i = 0; i < n; ++i)
            if (a[i] == null)
                throw new NullPointerException();
    }
    this.queue = a;
    this.size = n;
    if (heapify)
        heapify();
}
 
Example 13
/**
 * Creates a {@code PriorityBlockingQueue} containing the elements
 * in the specified collection.  If the specified collection is a
 * {@link SortedSet} or a {@link PriorityQueue}, this
 * priority queue will be ordered according to the same ordering.
 * Otherwise, this priority queue will be ordered according to the
 * {@linkplain Comparable natural ordering} of its elements.
 *
 * @param  c the collection whose elements are to be placed
 *         into this priority queue
 * @throws ClassCastException if elements of the specified collection
 *         cannot be compared to one another according to the priority
 *         queue's ordering
 * @throws NullPointerException if the specified collection or any
 *         of its elements are null
 */
public PriorityBlockingQueue(Collection<? extends E> c) {
    this.lock = new ReentrantLock();
    this.notEmpty = lock.newCondition();
    boolean heapify = true; // true if not known to be in heap order
    boolean screen = true;  // true if must screen for nulls
    if (c instanceof SortedSet<?>) {
        SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
        this.comparator = (Comparator<? super E>) ss.comparator();
        heapify = false;
    }
    else if (c instanceof PriorityBlockingQueue<?>) {
        PriorityBlockingQueue<? extends E> pq =
            (PriorityBlockingQueue<? extends E>) c;
        this.comparator = (Comparator<? super E>) pq.comparator();
        screen = false;
        if (pq.getClass() == PriorityBlockingQueue.class) // exact match
            heapify = false;
    }
    Object[] a = c.toArray();
    int n = a.length;
    // If c.toArray incorrectly doesn't return Object[], copy it.
    if (a.getClass() != Object[].class)
        a = Arrays.copyOf(a, n, Object[].class);
    if (screen && (n == 1 || this.comparator != null)) {
        for (int i = 0; i < n; ++i)
            if (a[i] == null)
                throw new NullPointerException();
    }
    this.queue = a;
    this.size = n;
    if (heapify)
        heapify();
}
 
Example 14
Source Project: datawave   File: FileSortedSet.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create an sorted set out of another sorted set. If persist is true, then the set will be directly persisted using the set's iterator which avoid pulling
 * all of its entries into memory at once.
 *
 * @param set
 * @param handler
 */
public FileSortedSet(SortedSet<E> set, TypedSortedSetFileHandler handler, FileSortedSetFactory factory, boolean persist) throws IOException {
    this.handler = handler;
    this.factory = factory;
    if (!persist) {
        this.set = new TreeSet<>(set);
        this.persisted = false;
    } else {
        this.set = new TreeSet<>(set.comparator());
        persist(set, handler);
        persisted = true;
    }
}
 
Example 15
Source Project: JDKSourceCode1.8   File: PriorityBlockingQueue.java    License: MIT License 5 votes vote down vote up
/**
 * Creates a {@code PriorityBlockingQueue} containing the elements
 * in the specified collection.  If the specified collection is a
 * {@link SortedSet} or a {@link PriorityQueue}, this
 * priority queue will be ordered according to the same ordering.
 * Otherwise, this priority queue will be ordered according to the
 * {@linkplain Comparable natural ordering} of its elements.
 *
 * @param  c the collection whose elements are to be placed
 *         into this priority queue
 * @throws ClassCastException if elements of the specified collection
 *         cannot be compared to one another according to the priority
 *         queue's ordering
 * @throws NullPointerException if the specified collection or any
 *         of its elements are null
 */
public PriorityBlockingQueue(Collection<? extends E> c) {
    this.lock = new ReentrantLock();
    this.notEmpty = lock.newCondition();
    boolean heapify = true; // true if not known to be in heap order
    boolean screen = true;  // true if must screen for nulls
    if (c instanceof SortedSet<?>) {
        SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
        this.comparator = (Comparator<? super E>) ss.comparator();
        heapify = false;
    }
    else if (c instanceof PriorityBlockingQueue<?>) {
        PriorityBlockingQueue<? extends E> pq =
            (PriorityBlockingQueue<? extends E>) c;
        this.comparator = (Comparator<? super E>) pq.comparator();
        screen = false;
        if (pq.getClass() == PriorityBlockingQueue.class) // exact match
            heapify = false;
    }
    Object[] a = c.toArray();
    int n = a.length;
    // If c.toArray incorrectly doesn't return Object[], copy it.
    if (a.getClass() != Object[].class)
        a = Arrays.copyOf(a, n, Object[].class);
    if (screen && (n == 1 || this.comparator != null)) {
        for (int i = 0; i < n; ++i)
            if (a[i] == null)
                throw new NullPointerException();
    }
    this.queue = a;
    this.size = n;
    if (heapify)
        heapify();
}
 
Example 16
@SuppressWarnings("unchecked")
// if sortedSet.comparator() is null, the set must be naturally ordered
public static <E> Comparator<? super E> comparator(SortedSet<E> sortedSet) {
  Comparator<? super E> result = sortedSet.comparator();
  if (result == null) {
    result = (Comparator<? super E>) Ordering.natural();
  }
  return result;
}
 
Example 17
Source Project: Bytecoder   File: ConcurrentSkipListSet.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a new set containing the same elements and using the
 * same ordering as the specified sorted set.
 *
 * @param s sorted set whose elements will comprise the new set
 * @throws NullPointerException if the specified sorted set or any
 *         of its elements are null
 */
public ConcurrentSkipListSet(SortedSet<E> s) {
    m = new ConcurrentSkipListMap<E,Object>(s.comparator());
    addAll(s);
}
 
Example 18
/**
 * Constructs a new set containing the same elements and using the
 * same ordering as the specified sorted set.
 *
 * @param s sorted set whose elements will comprise the new set
 * @throws NullPointerException if the specified sorted set or any
 *         of its elements are null
 */
public ConcurrentSkipListSet(SortedSet<E> s) {
    m = new ConcurrentSkipListMap<E,Object>(s.comparator());
    addAll(s);
}
 
Example 19
/**
 * Constructs a new set containing the same elements and using the
 * same ordering as the specified sorted set.
 *
 * @param s sorted set whose elements will comprise the new set
 * @throws NullPointerException if the specified sorted set or any
 *         of its elements are null
 */
public ConcurrentSkipListSet(SortedSet<E> s) {
    m = new ConcurrentSkipListMap<E,Object>(s.comparator());
    addAll(s);
}
 
Example 20
/**
 * Constructs a new set containing the same elements and using the
 * same ordering as the specified sorted set.
 *
 * @param s sorted set whose elements will comprise the new set
 * @throws NullPointerException if the specified sorted set or any
 *         of its elements are null
 */
public ConcurrentSkipListSet(SortedSet<E> s) {
    m = new ConcurrentSkipListMap<E,Object>(s.comparator());
    addAll(s);
}