View Javadoc
1   /*
2    * Copyright (C) 2016 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.annotations.GwtIncompatible;
23  import com.google.common.collect.Multiset.Entry;
24  import com.google.common.collect.Multisets;
25  import com.google.common.collect.testing.Helpers;
26  import com.google.common.collect.testing.features.CollectionFeature;
27  import java.lang.reflect.Method;
28  import java.util.Arrays;
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.List;
32  
33  /**
34   * Tests for {@code Multiset#forEachEntry}.
35   *
36   * @author Louis Wasserman
37   */
38  @GwtCompatible(emulated = true)
39  public class MultisetForEachEntryTester<E> extends AbstractMultisetTester<E> {
40    public void testForEachEntry() {
41      List<Entry<E>> expected = new ArrayList<>(getMultiset().entrySet());
42      List<Entry<E>> actual = new ArrayList<>();
43      getMultiset()
44          .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count)));
45      Helpers.assertEqualIgnoringOrder(expected, actual);
46    }
47  
48    @CollectionFeature.Require(KNOWN_ORDER)
49    public void testForEachEntryOrdered() {
50      List<Entry<E>> expected = new ArrayList<>(getMultiset().entrySet());
51      List<Entry<E>> actual = new ArrayList<>();
52      getMultiset()
53          .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count)));
54      assertEquals(expected, actual);
55    }
56  
57    public void testForEachEntryDuplicates() {
58      initThreeCopies();
59      List<Entry<E>> expected = Collections.singletonList(Multisets.immutableEntry(e0(), 3));
60      List<Entry<E>> actual = new ArrayList<>();
61      getMultiset()
62          .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count)));
63      assertEquals(expected, actual);
64    }
65  
66    /**
67     * Returns {@link Method} instances for the remove tests that assume multisets
68     * support duplicates so that the test of {@code Multisets.forSet()} can
69     * suppress them.
70     */
71    @GwtIncompatible // reflection
72    public static List<Method> getForEachEntryDuplicateInitializingMethods() {
73      return Arrays.asList(
74          Helpers.getMethod(MultisetForEachEntryTester.class, "testForEachEntryDuplicates"));
75    }
76  }