• 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.testing.google;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.collect.ImmutableBiMap;
23 import com.google.common.collect.Maps;
24 import com.google.common.collect.testing.SampleElements;
25 import com.google.common.collect.testing.TestMapEntrySetGenerator;
26 import com.google.common.collect.testing.TestStringSetGenerator;
27 
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.Set;
31 
32 /**
33  * Generators of various {@link com.google.common.collect.BiMap}s and derived
34  * collections.
35  *
36  * @author Jared Levy
37  * @author Hayward Chan
38  */
39 @GwtCompatible
40 public class BiMapGenerators {
41 
42   public static class ImmutableBiMapKeySetGenerator
43       extends TestStringSetGenerator {
create(String[] elements)44     @Override protected Set<String> create(String[] elements) {
45       Map<String, Integer> map = Maps.newLinkedHashMap();
46       for (int i = 0; i < elements.length; i++) {
47         map.put(elements[i], i);
48       }
49       return ImmutableBiMap.copyOf(map).keySet();
50     }
51   }
52 
53   public static class ImmutableBiMapValuesGenerator
54       extends TestStringSetGenerator {
create(String[] elements)55     @Override protected Set<String> create(String[] elements) {
56       Map<Integer, String> map = Maps.newLinkedHashMap();
57       for (int i = 0; i < elements.length; i++) {
58         map.put(i, elements[i]);
59       }
60       return ImmutableBiMap.copyOf(map).values();
61     }
62   }
63 
64   public static class ImmutableBiMapInverseEntrySetGenerator
65       extends TestMapEntrySetGenerator<String, String> {
66 
ImmutableBiMapInverseEntrySetGenerator()67     public ImmutableBiMapInverseEntrySetGenerator() {
68       super(new SampleElements.Strings(), new SampleElements.Strings());
69     }
createFromEntries( Entry<String, String>[] entries)70     @Override public Set<Entry<String, String>> createFromEntries(
71         Entry<String, String>[] entries) {
72       Map<String, String> map = Maps.newLinkedHashMap();
73       for (Entry<String, String> entry : entries) {
74         checkNotNull(entry);
75         map.put(entry.getValue(), entry.getKey());
76       }
77       return ImmutableBiMap.copyOf(map).inverse().entrySet();
78     }
79   }
80 
81   public static class ImmutableBiMapInverseKeySetGenerator
82       extends TestStringSetGenerator {
create(String[] elements)83     @Override protected Set<String> create(String[] elements) {
84       Map<Integer, String> map = Maps.newLinkedHashMap();
85       for (int i = 0; i < elements.length; i++) {
86         map.put(i, elements[i]);
87       }
88       return ImmutableBiMap.copyOf(map).inverse().keySet();
89     }
90   }
91 
92   public static class ImmutableBiMapInverseValuesGenerator
93       extends TestStringSetGenerator {
create(String[] elements)94     @Override protected Set<String> create(String[] elements) {
95       Map<String, Integer> map = Maps.newLinkedHashMap();
96       for (int i = 0; i < elements.length; i++) {
97         map.put(elements[i], i);
98       }
99       return ImmutableBiMap.copyOf(map).inverse().values();
100     }
101   }
102 
103   public static class ImmutableBiMapEntrySetGenerator
104       extends TestMapEntrySetGenerator<String, String> {
105 
ImmutableBiMapEntrySetGenerator()106     public ImmutableBiMapEntrySetGenerator() {
107       super(new SampleElements.Strings(), new SampleElements.Strings());
108     }
createFromEntries( Entry<String, String>[] entries)109     @Override public Set<Entry<String, String>> createFromEntries(
110         Entry<String, String>[] entries) {
111       Map<String, String> map = Maps.newLinkedHashMap();
112       for (Entry<String, String> entry : entries) {
113         checkNotNull(entry);
114         map.put(entry.getKey(), entry.getValue());
115       }
116       return ImmutableBiMap.copyOf(map).entrySet();
117     }
118   }
119 }
120