• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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;
18 
19 import com.google.common.annotations.GwtIncompatible;
20 import java.util.Arrays;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map;
24 
25 @GwtIncompatible
26 public class ImmutableMapFloodingTest extends AbstractHashFloodingTest<Map<Object, Object>> {
ImmutableMapFloodingTest()27   public ImmutableMapFloodingTest() {
28     super(
29         Arrays.asList(ConstructionPathway.values()),
30         n -> n * Math.log(n),
31         ImmutableList.of(QueryOp.MAP_GET));
32   }
33 
34   /** All the ways to create an ImmutableMap. */
35   enum ConstructionPathway implements Construction<Map<Object, Object>> {
36     COPY_OF_MAP {
37       @Override
create(List<?> keys)38       public Map<Object, Object> create(List<?> keys) {
39         Map<Object, Object> sourceMap = new LinkedHashMap<>();
40         for (Object k : keys) {
41           if (sourceMap.put(k, "dummy value") != null) {
42             throw new UnsupportedOperationException("duplicate key");
43           }
44         }
45         return ImmutableMap.copyOf(sourceMap);
46       }
47     },
48     COPY_OF_ENTRIES {
49       @Override
create(List<?> keys)50       public Map<Object, Object> create(List<?> keys) {
51         return ImmutableMap.copyOf(
52             Lists.transform(keys, k -> Maps.immutableEntry(k, "dummy value")));
53       }
54     },
55     BUILDER_PUT_ONE_BY_ONE {
56       @Override
create(List<?> keys)57       public Map<Object, Object> create(List<?> keys) {
58         ImmutableMap.Builder<Object, Object> builder = ImmutableMap.builder();
59         for (Object k : keys) {
60           builder.put(k, "dummy value");
61         }
62         return builder.buildOrThrow();
63       }
64     },
65     BUILDER_PUT_ENTRIES_ONE_BY_ONE {
66       @Override
create(List<?> keys)67       public Map<Object, Object> create(List<?> keys) {
68         ImmutableMap.Builder<Object, Object> builder = ImmutableMap.builder();
69         for (Object k : keys) {
70           builder.put(Maps.immutableEntry(k, "dummy value"));
71         }
72         return builder.buildOrThrow();
73       }
74     },
75     BUILDER_PUT_ALL_MAP {
76       @Override
create(List<?> keys)77       public Map<Object, Object> create(List<?> keys) {
78         Map<Object, Object> sourceMap = new LinkedHashMap<>();
79         for (Object k : keys) {
80           if (sourceMap.put(k, "dummy value") != null) {
81             throw new UnsupportedOperationException("duplicate key");
82           }
83         }
84         return ImmutableMap.builder().putAll(sourceMap).buildOrThrow();
85       }
86     },
87     BUILDER_PUT_ALL_ENTRIES {
88       @Override
create(List<?> keys)89       public Map<Object, Object> create(List<?> keys) {
90         return ImmutableMap.builder()
91             .putAll(Lists.transform(keys, k -> Maps.immutableEntry(k, "dummy value")))
92             .buildOrThrow();
93       }
94     },
95     FORCE_JDK {
96       @Override
create(List<?> keys)97       public Map<Object, Object> create(List<?> keys) {
98         ImmutableMap.Builder<Object, Object> builder = ImmutableMap.builder();
99         for (Object k : keys) {
100           builder.put(k, "dummy value");
101         }
102         return builder.buildJdkBacked();
103       }
104     };
105   }
106 }
107