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
17package com.google.common.collect;
18
19/**
20 * GWT emulation of {@link RegularImmutableBiMap}.
21 *
22 * @author Jared Levy
23 * @author Hayward Chan
24 */
25@SuppressWarnings("serial")
26class RegularImmutableBiMap<K, V> extends ImmutableBiMap<K, V> {
27
28  // This reference is used both by the GWT compiler to infer the elements
29  // of the lists that needs to be serialized.
30  private ImmutableBiMap<V, K> inverse;
31
32  RegularImmutableBiMap(ImmutableMap<K, V> delegate) {
33    super(delegate);
34
35    ImmutableMap.Builder<V, K> builder = ImmutableMap.builder();
36    for (Entry<K, V> entry : delegate.entrySet()) {
37      builder.put(entry.getValue(), entry.getKey());
38    }
39    ImmutableMap<V, K> backwardMap = builder.build();
40    this.inverse = new RegularImmutableBiMap<V, K>(backwardMap, this);
41  }
42
43  RegularImmutableBiMap(ImmutableMap<K, V> delegate,
44      ImmutableBiMap<V, K> inverse) {
45    super(delegate);
46    this.inverse = inverse;
47  }
48
49  @Override public ImmutableBiMap<V, K> inverse() {
50    return inverse;
51  }
52}
53