Exemplo n.º 1
0
 public void testCreateWithSize() {
   Multiset<String> multiset = HashMultiset.create(50);
   multiset.add("foo", 2);
   multiset.add("bar");
   assertEquals(3, multiset.size());
   assertEquals(2, multiset.count("foo"));
 }
  /** 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());
  }
  public void testToString() {
    Multiset<String> ms = TreeMultiset.create();
    ms.add("a", 3);
    ms.add("c", 1);
    ms.add("b", 2);

    assertEquals("[a x 3, b x 2, c]", ms.toString());
  }
 public void testCreateWithComparator() {
   Multiset<String> multiset = TreeMultiset.create(Collections.reverseOrder());
   multiset.add("foo", 2);
   multiset.add("bar");
   assertEquals(3, multiset.size());
   assertEquals(2, multiset.count("foo"));
   assertEquals("[foo x 2, bar]", multiset.toString());
 }
  @Test
  public void whenGetTopUsingMultiSet_thenCorrect() {
    final Multiset<String> names = HashMultiset.create();
    names.add("John");
    names.add("Adam", 5);
    names.add("Jane");
    names.add("Tom", 2);

    final Set<String> sorted = Multisets.copyHighestCountFirst(names).elementSet();
    final List<String> topTwo = Lists.newArrayList(sorted).subList(0, 2);
    assertEquals(2, topTwo.size());
    assertEquals("Adam", topTwo.get(0));
    assertEquals("Tom", topTwo.get(1));
  }
  @Test
  public void whenInsertDuplicatesInMultiSet_thenInserted() {
    final Multiset<String> names = HashMultiset.create();
    names.add("John");
    names.add("Adam", 3);
    names.add("John");

    assertEquals(2, names.count("John"));
    names.remove("John");
    assertEquals(1, names.count("John"));

    assertEquals(3, names.count("Adam"));
    names.remove("Adam", 2);
    assertEquals(1, names.count("Adam"));
  }
 public void testCreation_arrayOfArray() {
   String[] array = new String[] {"a"};
   Multiset<String[]> multiset = ImmutableMultiset.<String[]>of(array);
   Multiset<String[]> expected = HashMultiset.create();
   expected.add(array);
   assertEquals(expected, multiset);
 }
Exemplo n.º 8
0
 @GwtIncompatible // SerializableTester
 public void testSerializationIndirectSelfReference() {
   Multiset<MultisetHolder> multiset = HashMultiset.create();
   MultisetHolder holder = new MultisetHolder(multiset);
   multiset.add(holder, 2);
   Multiset<MultisetHolder> copy = SerializableTester.reserialize(multiset);
   assertEquals(2, copy.size());
   assertSame(copy, copy.iterator().next().member);
 }
Exemplo n.º 9
0
  /** An implementation of {@link Multiset#setCount(Object, int)}. */
  static <E> int setCountImpl(Multiset<E> self, E element, int count) {
    checkNonnegative(count, "count");

    int oldCount = self.count(element);

    int delta = count - oldCount;
    if (delta > 0) {
      self.add(element, delta);
    } else if (delta < 0) {
      self.remove(element, -delta);
    }

    return oldCount;
  }
Exemplo n.º 10
0
 /** An implementation of {@link Multiset#addAll}. */
 static <E> boolean addAllImpl(Multiset<E> self, Collection<? extends E> elements) {
   if (elements.isEmpty()) {
     return false;
   }
   if (elements instanceof Multiset) {
     Multiset<? extends E> that = cast(elements);
     for (Entry<? extends E> entry : that.entrySet()) {
       self.add(entry.getElement(), entry.getCount());
     }
   } else {
     Iterators.addAll(self, elements.iterator());
   }
   return true;
 }
Exemplo n.º 11
0
 public void testCreation_arrayOfArray() {
   Comparator<String[]> comparator =
       Ordering.natural()
           .lexicographical()
           .onResultOf(
               new Function<String[], Iterable<Comparable>>() {
                 @Override
                 public Iterable<Comparable> apply(String[] input) {
                   return Arrays.<Comparable>asList(input);
                 }
               });
   String[] array = new String[] {"a"};
   Multiset<String[]> multiset = ImmutableSortedMultiset.orderedBy(comparator).add(array).build();
   Multiset<String[]> expected = HashMultiset.create();
   expected.add(array);
   assertEquals(expected, multiset);
 }
Exemplo n.º 12
0
 @Override
 public int add(E element, int occurrences) {
   constraint.checkElement(element);
   return delegate.add(element, occurrences);
 }
Exemplo n.º 13
0
 @Override
 public boolean add(E element) {
   constraint.checkElement(element);
   return delegate.add(element);
 }
Exemplo n.º 14
0
 /**
  * Adds a number of occurrences of an element to this {@code ImmutableMultiset}.
  *
  * @param element the element to add
  * @param occurrences the number of occurrences of the element to add. May be zero, in which
  *     case no change will be made.
  * @return this {@code Builder} object
  * @throws NullPointerException if {@code element} is null
  * @throws IllegalArgumentException if {@code occurrences} is negative, or if this operation
  *     would result in more than {@link Integer#MAX_VALUE} occurrences of the element
  */
 public Builder<E> addCopies(E element, int occurrences) {
   contents.add(checkNotNull(element), occurrences);
   return this;
 }
Exemplo n.º 15
0
 /**
  * Adds {@code element} to the {@code ImmutableMultiset}.
  *
  * @param element the element to add
  * @return this {@code Builder} object
  * @throws NullPointerException if {@code element} is null
  */
 @Override
 public Builder<E> add(E element) {
   contents.add(checkNotNull(element));
   return this;
 }
Exemplo n.º 16
0
 @Override
 public int add(E element, int occurrences) {
   checkArgument(
       predicate.apply(element), "Element %s does not match predicate %s", element, predicate);
   return unfiltered.add(element, occurrences);
 }