View Javadoc
1   /*
2    * Copyright (C) 2012 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 com.google.common.annotations.GwtCompatible;
20  import com.google.common.collect.BiMap;
21  import com.google.common.collect.testing.DerivedGenerator;
22  import com.google.common.collect.testing.Helpers;
23  import com.google.common.collect.testing.OneSizeTestContainerGenerator;
24  import com.google.common.collect.testing.SampleElements;
25  import com.google.common.collect.testing.TestMapGenerator;
26  import com.google.common.collect.testing.TestSetGenerator;
27  import com.google.common.collect.testing.TestSubjectGenerator;
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Map.Entry;
33  import java.util.Set;
34  
35  /**
36   * Derived suite generators for Guava collection interfaces, split out of the suite builders so that
37   * they are available to GWT.
38   *
39   * @author Louis Wasserman
40   */
41  @GwtCompatible
42  public final class DerivedGoogleCollectionGenerators {
43    public static class MapGenerator<K, V> implements TestMapGenerator<K, V>, DerivedGenerator {
44  
45      private final OneSizeTestContainerGenerator<BiMap<K, V>, Entry<K, V>> generator;
46  
47      public MapGenerator(
48          OneSizeTestContainerGenerator<BiMap<K, V>, Entry<K, V>> oneSizeTestContainerGenerator) {
49        this.generator = oneSizeTestContainerGenerator;
50      }
51  
52      @Override
53      public SampleElements<Map.Entry<K, V>> samples() {
54        return generator.samples();
55      }
56  
57      @Override
58      public Map<K, V> create(Object... elements) {
59        return generator.create(elements);
60      }
61  
62      @Override
63      public Map.Entry<K, V>[] createArray(int length) {
64        return generator.createArray(length);
65      }
66  
67      @Override
68      public Iterable<Map.Entry<K, V>> order(List<Map.Entry<K, V>> insertionOrder) {
69        return generator.order(insertionOrder);
70      }
71  
72      @SuppressWarnings("unchecked")
73      @Override
74      public K[] createKeyArray(int length) {
75        return (K[]) new Object[length];
76      }
77  
78      @SuppressWarnings("unchecked")
79      @Override
80      public V[] createValueArray(int length) {
81        return (V[]) new Object[length];
82      }
83  
84      public TestSubjectGenerator<?> getInnerGenerator() {
85        return generator;
86      }
87    }
88  
89    public static class InverseBiMapGenerator<K, V>
90        implements TestBiMapGenerator<V, K>, DerivedGenerator {
91  
92      private final OneSizeTestContainerGenerator<BiMap<K, V>, Entry<K, V>> generator;
93  
94      public InverseBiMapGenerator(
95          OneSizeTestContainerGenerator<BiMap<K, V>, Entry<K, V>> oneSizeTestContainerGenerator) {
96        this.generator = oneSizeTestContainerGenerator;
97      }
98  
99      @Override
100     public SampleElements<Map.Entry<V, K>> samples() {
101       SampleElements<Entry<K, V>> samples = generator.samples();
102       return new SampleElements<>(
103           reverse(samples.e0()),
104           reverse(samples.e1()),
105           reverse(samples.e2()),
106           reverse(samples.e3()),
107           reverse(samples.e4()));
108     }
109 
110     private Map.Entry<V, K> reverse(Map.Entry<K, V> entry) {
111       return Helpers.mapEntry(entry.getValue(), entry.getKey());
112     }
113 
114     @SuppressWarnings("unchecked")
115     @Override
116     public BiMap<V, K> create(Object... elements) {
117       Entry<?, ?>[] entries = new Entry<?, ?>[elements.length];
118       for (int i = 0; i < elements.length; i++) {
119         entries[i] = reverse((Entry<K, V>) elements[i]);
120       }
121       return generator.create((Object[]) entries).inverse();
122     }
123 
124     @SuppressWarnings("unchecked")
125     @Override
126     public Map.Entry<V, K>[] createArray(int length) {
127       return new Entry[length];
128     }
129 
130     @Override
131     public Iterable<Entry<V, K>> order(List<Entry<V, K>> insertionOrder) {
132       return insertionOrder;
133     }
134 
135     @SuppressWarnings("unchecked")
136     @Override
137     public V[] createKeyArray(int length) {
138       return (V[]) new Object[length];
139     }
140 
141     @SuppressWarnings("unchecked")
142     @Override
143     public K[] createValueArray(int length) {
144       return (K[]) new Object[length];
145     }
146 
147     public TestSubjectGenerator<?> getInnerGenerator() {
148       return generator;
149     }
150   }
151 
152   public static class BiMapValueSetGenerator<K, V>
153       implements TestSetGenerator<V>, DerivedGenerator {
154     private final OneSizeTestContainerGenerator<BiMap<K, V>, Map.Entry<K, V>> mapGenerator;
155     private final SampleElements<V> samples;
156 
157     public BiMapValueSetGenerator(
158         OneSizeTestContainerGenerator<BiMap<K, V>, Entry<K, V>> mapGenerator) {
159       this.mapGenerator = mapGenerator;
160       final SampleElements<Map.Entry<K, V>> mapSamples = this.mapGenerator.samples();
161       this.samples =
162           new SampleElements<V>(
163               mapSamples.e0().getValue(),
164               mapSamples.e1().getValue(),
165               mapSamples.e2().getValue(),
166               mapSamples.e3().getValue(),
167               mapSamples.e4().getValue());
168     }
169 
170     @Override
171     public SampleElements<V> samples() {
172       return samples;
173     }
174 
175     @Override
176     public Set<V> create(Object... elements) {
177       @SuppressWarnings("unchecked")
178       V[] valuesArray = (V[]) elements;
179 
180       // Start with a suitably shaped collection of entries
181       Collection<Map.Entry<K, V>> originalEntries = mapGenerator.getSampleElements(elements.length);
182 
183       // Create a copy of that, with the desired value for each value
184       Collection<Map.Entry<K, V>> entries = new ArrayList<>(elements.length);
185       int i = 0;
186       for (Map.Entry<K, V> entry : originalEntries) {
187         entries.add(Helpers.mapEntry(entry.getKey(), valuesArray[i++]));
188       }
189 
190       return mapGenerator.create(entries.toArray()).values();
191     }
192 
193     @Override
194     public V[] createArray(int length) {
195       final V[] vs =
196           ((TestBiMapGenerator<K, V>) mapGenerator.getInnerGenerator()).createValueArray(length);
197       return vs;
198     }
199 
200     @Override
201     public Iterable<V> order(List<V> insertionOrder) {
202       return insertionOrder;
203     }
204 
205     public TestSubjectGenerator<?> getInnerGenerator() {
206       return mapGenerator;
207     }
208   }
209 
210   private DerivedGoogleCollectionGenerators() {}
211 }