Exemplo n.º 1
0
 Object readResolve() {
   LinkedHashMultiset<Object> multiset = LinkedHashMultiset.create(elements.length);
   for (int i = 0; i < elements.length; i++) {
     multiset.add(elements[i], counts[i]);
   }
   return ImmutableMultiset.copyOf(multiset);
 }
  @BeforeExperiment
  void setUp() {
    hashMultiset = HashMultiset.create(size);
    linkedHashMultiset = LinkedHashMultiset.create(size);
    treeMultiset = TreeMultiset.create();

    Random random = new Random();

    int sizeRemaining = size;

    // TODO(kevinb): generate better test contents for multisets
    for (int i = 0; sizeRemaining > 0; i++) {
      // The JVM will return interned values for small ints.
      Integer value = random.nextInt(1000) + 128;
      int count = Math.min(random.nextInt(10) + 1, sizeRemaining);
      sizeRemaining -= count;
      hashMultiset.add(value, count);
      linkedHashMultiset.add(value, count);
      treeMultiset.add(value, count);
    }

    // TODO(kevinb): convert to assert once benchmark tests enable asserts by default
    Preconditions.checkState(hashMultiset.size() == size);
  }
Exemplo n.º 3
0
  /**
   * Returns an immutable multiset containing the given elements.
   *
   * <p>The multiset is ordered by the first occurrence of each element. For example, {@code
   * ImmutableMultiset.copyOf(Arrays.asList(2, 3, 1, 3))} yields a multiset with elements in the
   * order {@code 2, 3, 3, 1}.
   *
   * <p>Despite the method name, this method attempts to avoid actually copying the data when it is
   * safe to do so. The exact circumstances under which a copy will or will not be performed are
   * undocumented and subject to change.
   *
   * <p><b>Note:</b> Despite what the method name suggests, if {@code elements} is an {@code
   * ImmutableMultiset}, no copy will actually be performed, and the given multiset itself will be
   * returned.
   *
   * @throws NullPointerException if any of {@code elements} is null
   */
  public static <E> ImmutableMultiset<E> copyOf(Iterable<? extends E> elements) {
    if (elements instanceof ImmutableMultiset) {
      @SuppressWarnings("unchecked") // all supported methods are covariant
      ImmutableMultiset<E> result = (ImmutableMultiset<E>) elements;
      if (!result.isPartialView()) {
        return result;
      }
    }

    Multiset<? extends E> multiset =
        (elements instanceof Multiset)
            ? Multisets.cast(elements)
            : LinkedHashMultiset.create(elements);

    return copyOfInternal(multiset);
  }
  public void testBuilderAddAllHandlesNullsCorrectly() {
    ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder();
    try {
      builder.addAll((Collection<String>) null);
      fail("expected NullPointerException");
    } catch (NullPointerException expected) {
    }

    builder = ImmutableMultiset.builder();
    List<String> listWithNulls = asList("a", null, "b");
    try {
      builder.addAll(listWithNulls);
      fail("expected NullPointerException");
    } catch (NullPointerException expected) {
    }

    builder = ImmutableMultiset.builder();
    Multiset<String> multisetWithNull = LinkedHashMultiset.create(asList("a", null, "b"));
    try {
      builder.addAll(multisetWithNull);
      fail("expected NullPointerException");
    } catch (NullPointerException expected) {
    }
  }
Exemplo n.º 5
0
 /**
  * Creates a new builder. The returned builder is equivalent to the builder generated by {@link
  * ImmutableMultiset#builder}.
  */
 public Builder() {
   this(LinkedHashMultiset.<E>create());
 }
Exemplo n.º 6
0
 /**
  * Returns an immutable multiset containing the given elements.
  *
  * <p>The multiset is ordered by the first occurrence of each element. For example, {@code
  * ImmutableMultiset.copyOf(Arrays.asList(2, 3, 1, 3).iterator())} yields a multiset with elements
  * in the order {@code 2, 3, 3, 1}.
  *
  * @throws NullPointerException if any of {@code elements} is null
  */
 public static <E> ImmutableMultiset<E> copyOf(Iterator<? extends E> elements) {
   Multiset<E> multiset = LinkedHashMultiset.create();
   Iterators.addAll(multiset, elements);
   return copyOfInternal(multiset);
 }