/** Test a TreeMultiset with a comparator that can return 0 when comparing unequal values. */
  public void testDegenerateComparator() throws Exception {
    TreeMultiset<String> ms = TreeMultiset.create(DEGENERATE_COMPARATOR);

    ms.add("foo");
    ms.add("a");
    ms.add("bar");
    ms.add("b");
    ms.add("c");

    assertEquals(2, ms.count("bar"));
    assertEquals(3, ms.count("b"));

    Multiset<String> ms2 = TreeMultiset.create(DEGENERATE_COMPARATOR);

    ms2.add("cat", 2);
    ms2.add("x", 3);

    assertEquals(ms, ms2);
    assertEquals(ms2, ms);

    SortedSet<String> elementSet = ms.elementSet();
    assertEquals("a", elementSet.first());
    assertEquals("foo", elementSet.last());
    assertEquals(DEGENERATE_COMPARATOR, elementSet.comparator());
  }
 @SuppressWarnings("unchecked")
 public PriorityBlockingDeque(SortedSet<? extends E> c) {
   this.lock = new ReentrantLock();
   this.notEmpty = lock.newCondition();
   this.comparator = (Comparator<? super E>) c.comparator();
   addAll(c);
 }
Пример #3
0
 public static <E> Comparator<? super E> m2894a(SortedSet<E> sortedSet) {
   Comparator<? super E> comparator = sortedSet.comparator();
   if (comparator == null) {
     return az.m2824b();
   }
   return comparator;
 }
Пример #4
0
  public static <E extends Object> ImmutableSortedSet<E> copyOfSorted(SortedSet<E> var0) {
    Comparator var1 = var0.comparator();
    if (var1 == null) {
      var1 = NATURAL_ORDER;
    }

    return copyOfInternal(var1, var0, (boolean) 1);
  }
Пример #5
0
 /**
  * Returns an immutable sorted set containing the elements of a sorted set, sorted by the same
  * {@code Comparator}. That behavior differs from {@link #copyOf(Iterable)}, which always uses the
  * natural ordering of the elements.
  *
  * <p><b>Note:</b> Despite what the method name suggests, if {@code sortedSet} is an {@code
  * ImmutableSortedSet}, it may be returned instead of a copy.
  *
  * @throws NullPointerException if any of {@code elements} is null
  */
 @SuppressWarnings("unchecked")
 public static <E> ImmutableSortedSet<E> copyOfSorted(SortedSet<E> sortedSet) {
   Comparator<? super E> comparator = sortedSet.comparator();
   if (comparator == null) {
     comparator = NATURAL_ORDER;
   }
   return copyOfInternal(comparator, sortedSet, true);
 }
Пример #6
0
 /**
  * Returns {@code true} if {@code elements} is a {@code SortedSet} that uses {@code comparator} to
  * order its elements. Note that equivalent comparators may still return {@code false}, if {@code
  * equals} doesn't consider them equal. If one comparator is {@code null} and the other is {@link
  * Comparators#naturalOrder()}, this method returns {@code true}.
  */
 private static boolean hasSameComparator(Object elements, Comparator<?> comparator) {
   if (elements instanceof SortedSet) {
     SortedSet<?> sortedSet = (SortedSet<?>) elements;
     Comparator<?> comparator2 = sortedSet.comparator();
     return Objects.equal(comparator2, comparator)
         || (comparator == null && comparator2 == Comparators.naturalOrder())
         || (comparator2 == null && comparator == Comparators.naturalOrder());
   }
   return false;
 }
Пример #7
0
  private SortedSet<TreeFacetField> cloneHierarchy(SortedSet<TreeFacetField> orig) {
    SortedSet<TreeFacetField> cloned = null;

    if (orig != null) {
      cloned = new TreeSet<>(orig.comparator());

      for (TreeFacetField tff : orig) {
        cloned.add(tff.clone());
      }
    }

    return cloned;
  }
Пример #8
0
 /**
  * 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
  */
 @Override
 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 OMVRBTree) {
     SortedSet<? extends E> set = (SortedSet<? extends E>) c;
     OMVRBTree<E, Object> map = (OMVRBTree<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.addAllForOTreeSet(set, PRESENT);
       return true;
     }
   }
   return super.addAll(c);
 }
  public void testElementSetSortedSetMethods() {
    TreeMultiset<String> ms = TreeMultiset.create();
    ms.add("c", 1);
    ms.add("a", 3);
    ms.add("b", 2);
    SortedSet<String> elementSet = ms.elementSet();

    assertEquals("a", elementSet.first());
    assertEquals("c", elementSet.last());
    assertEquals(Ordering.natural(), elementSet.comparator());

    ASSERT.that(elementSet.headSet("b")).has().exactly("a").inOrder();
    ASSERT.that(elementSet.tailSet("b")).has().exactly("b", "c").inOrder();
    ASSERT.that(elementSet.subSet("a", "c")).has().exactly("a", "b").inOrder();
  }
Пример #10
0
 /**
  * Returns {@code true} if {@code elements} is a sorted collection using an ordering equivalent to
  * {@code comparator}.
  */
 public static boolean hasSameComparator(Comparator<?> comparator, Iterable<?> elements) {
   checkNotNull(comparator);
   checkNotNull(elements);
   Comparator<?> comparator2;
   if (elements instanceof SortedSet) {
     SortedSet<?> sortedSet = (SortedSet<?>) elements;
     comparator2 = sortedSet.comparator();
     if (comparator2 == null) {
       comparator2 = (Comparator) Ordering.natural();
     }
   } else if (elements instanceof SortedIterable) {
     comparator2 = ((SortedIterable<?>) elements).comparator();
   } else {
     comparator2 = null;
   }
   return comparator.equals(comparator2);
 }
Пример #11
0
 /** @tests java.util.TreeSet#TreeSet(java.util.SortedSet) */
 public void test_ConstructorLjava_util_SortedSet() {
   // Test for method java.util.TreeSet(java.util.SortedSet)
   ReversedIntegerComparator comp = new ReversedIntegerComparator();
   SortedSet myTreeSet = db.createTreeSet("test", comp, null);
   for (int i = 0; i < objArray.length; i++) myTreeSet.add(objArray[i]);
   SortedSet anotherTreeSet = db.getTreeSet("test");
   anotherTreeSet.addAll(myTreeSet);
   assertTrue("TreeSet is not correct size", anotherTreeSet.size() == objArray.length);
   for (int counter = 0; counter < objArray.length; counter++)
     assertTrue(
         "TreeSet does not contain correct elements", anotherTreeSet.contains(objArray[counter]));
   assertEquals(
       "TreeSet does not answer correct comparator",
       anotherTreeSet.comparator().getClass(),
       comp.getClass());
   assertEquals(
       "TreeSet does not use comparator", anotherTreeSet.first(), objArray[objArray.length - 1]);
 }
  public void testNullAcceptingComparator() throws Exception {
    Comparator<String> comparator = Ordering.<String>natural().nullsFirst();
    TreeMultiset<String> ms = TreeMultiset.create(comparator);

    ms.add("b");
    ms.add(null);
    ms.add("a");
    ms.add("b");
    ms.add(null, 2);

    ASSERT.that(ms).has().exactly(null, null, null, "a", "b", "b").inOrder();
    assertEquals(3, ms.count(null));

    SortedSet<String> elementSet = ms.elementSet();
    assertEquals(null, elementSet.first());
    assertEquals("b", elementSet.last());
    assertEquals(comparator, elementSet.comparator());
  }
Пример #13
0
  /**
   * 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;
  }
 @SuppressWarnings("unchecked")
 public PriorityBlockingDeque(Collection<? extends E> c) {
   this.lock = new ReentrantLock();
   this.notEmpty = lock.newCondition();
   if (c instanceof SortedSet<?>) {
     SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
     this.comparator = (Comparator<? super E>) ss.comparator();
     addAll(ss);
   } else if (c instanceof PriorityDeque<?>) {
     PriorityDeque<? extends E> pq = (PriorityDeque<? extends E>) c;
     this.comparator = (Comparator<? super E>) pq.comparator();
     initFromPriorityDeque(pq);
   } else if (c instanceof PriorityBlockingDeque<?>) {
     PriorityBlockingDeque<? extends E> pq = (PriorityBlockingDeque<? extends E>) c;
     this.comparator = (Comparator<? super E>) pq.comparator();
     initFromPriorityBlockingDeque(pq);
   } else {
     this.comparator = null;
     addAll(c);
   }
 }
  public void testCustomComparator() throws Exception {
    Comparator<String> comparator =
        new Comparator<String>() {
          @Override
          public int compare(String o1, String o2) {
            return o2.compareTo(o1);
          }
        };
    TreeMultiset<String> ms = TreeMultiset.create(comparator);

    ms.add("b");
    ms.add("c");
    ms.add("a");
    ms.add("b");
    ms.add("d");

    ASSERT.that(ms).has().exactly("d", "c", "b", "b", "a").inOrder();

    SortedSet<String> elementSet = ms.elementSet();
    assertEquals("d", elementSet.first());
    assertEquals("a", elementSet.last());
    assertEquals(comparator, elementSet.comparator());
  }
Пример #16
0
 /**
  * Utility method to verify that the given SortedSet is equal to and hashes identically to a set
  * constructed with the elements in the given iterable. Also verifies that the comparator is the
  * same as the given comparator.
  */
 private static <E> void verifySortedSetContents(
     SortedSet<E> set, Iterable<E> iterable, @Nullable Comparator<E> comparator) {
   assertSame(comparator, set.comparator());
   verifySetContents(set, iterable);
 }
Пример #17
0
 /**
  * Creates a new tree set copying a given sorted set (and its {@link Comparator}).
  *
  * @param s a {@link SortedSet} to be copied into the new tree set.
  */
 public ObjectRBTreeSet(final SortedSet<K> s) {
   this(s.comparator());
   addAll(s);
 }
Пример #18
0
  private <T> void verify(SortedSet<T> immutableSet, SortedSet<T> mutableSet, T value1, T value2) {
    try {
      immutableSet.add(value1);
      Assert.fail();
    } catch (UnsupportedOperationException e) {
    }

    try {
      immutableSet.addAll(java.util.Collections.singleton(value1));
      Assert.fail();
    } catch (UnsupportedOperationException e) {
    }

    try {
      immutableSet.clear();

      Assert.assertTrue(mutableSet.isEmpty());
    } catch (UnsupportedOperationException e) {
      Assert.assertFalse(mutableSet.isEmpty());
    }

    Assert.assertEquals(immutableSet.contains(value1), mutableSet.contains(value1));
    Assert.assertEquals(immutableSet.contains(value2), mutableSet.contains(value2));

    Assert.assertEquals(
        immutableSet.containsAll(java.util.Collections.emptySet()),
        mutableSet.containsAll(java.util.Collections.emptySet()));
    Assert.assertEquals(
        immutableSet.containsAll(java.util.Collections.singleton(0)),
        mutableSet.containsAll(java.util.Collections.singleton(0)));
    Assert.assertEquals(
        immutableSet.containsAll(java.util.Collections.singleton(1)),
        mutableSet.containsAll(java.util.Collections.singleton(1)));

    Iterator<T> iterator = immutableSet.iterator();

    if (!mutableSet.isEmpty()) {
      Assert.assertTrue(iterator.hasNext());

      Assert.assertEquals(iterator.next(), mutableSet.iterator().next());
    }

    Assert.assertFalse(iterator.hasNext());

    try {
      iterator.next();
      Assert.fail();
    } catch (NoSuchElementException e) {
    }

    Assert.assertEquals(immutableSet.isEmpty(), mutableSet.isEmpty());

    try {
      immutableSet.remove(value1);
    } catch (UnsupportedOperationException e) {
    }

    try {
      immutableSet.removeAll(java.util.Collections.singleton(value1));
    } catch (UnsupportedOperationException e) {
    }

    try {
      immutableSet.retainAll(java.util.Collections.singleton(value1));
    } catch (UnsupportedOperationException e) {
    }

    Assert.assertEquals(immutableSet.size(), mutableSet.size());

    Assert.assertArrayEquals(immutableSet.toArray(), mutableSet.toArray());

    Assert.assertSame(immutableSet.comparator(), mutableSet.comparator());

    if (!mutableSet.isEmpty()) {
      Assert.assertEquals(immutableSet.first(), mutableSet.first());
      Assert.assertEquals(immutableSet.last(), mutableSet.last());
    } else {
      try {
        immutableSet.first();
        Assert.fail();
      } catch (NoSuchElementException e) {
      }

      try {
        immutableSet.last();
        Assert.fail();
      } catch (NoSuchElementException e) {
      }
    }

    Assert.assertEquals(immutableSet.headSet(value1), mutableSet.headSet(value1));
    Assert.assertEquals(immutableSet.headSet(value2), mutableSet.headSet(value2));

    Assert.assertEquals(immutableSet.subSet(value1, value2), mutableSet.subSet(value1, value2));

    Assert.assertEquals(immutableSet.tailSet(value1), mutableSet.tailSet(value1));
    Assert.assertEquals(immutableSet.tailSet(value2), mutableSet.tailSet(value2));
  }
Пример #19
0
 /**
  * Creates a new tree set copying a given sorted set (and its {@link Comparator}).
  *
  * @param s a {@link SortedSet} to be copied into the new tree set.
  */
 public LongAVLTreeSet(final SortedSet<Long> s) {
   this(s.comparator());
   addAll(s);
 }
Пример #20
0
 /** @tests java.util.TreeSet#comparator() */
 public void test_comparator() {
   // Test for method java.util.Comparator java.util.TreeSet.comparator()
   ReversedIntegerComparator comp = new ReversedIntegerComparator();
   SortedSet myTreeSet = db.createTreeSet("test", comp, null);
   assertTrue("Answered incorrect comparator", myTreeSet.comparator() == comp);
 }
Пример #21
0
 /**
  * Constructs a new tree 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 is null
  */
 public OMVRBTreeSet(SortedSet<E> s) {
   this(s.comparator());
   addAll(s);
 }
 public static ImmutableSortedSet copyOfSorted(SortedSet sortedset) {
   Comparator comparator1 = sortedset.comparator();
   if (comparator1 == null) comparator1 = NATURAL_ORDER;
   return copyOfInternal(comparator1, sortedset);
 }
Пример #23
0
 /**
  * {@inheritDoc}
  *
  * @see java.util.SortedSet#comparator()
  */
 public Comparator<? super E> comparator() {
   return m_set.comparator();
 }