1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
2090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Copyright (C) 2008 Google Inc.
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License.
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Bimap with one or more mappings.
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@GwtCompatible(serializable = true)
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@SuppressWarnings("serial") // uses writeReplace(), not default serialization
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonclass RegularImmutableBiMap<K, V> extends ImmutableBiMap<K, V> {
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  final transient ImmutableMap<K, V> delegate;
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  final transient ImmutableBiMap<V, K> inverse;
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  RegularImmutableBiMap(ImmutableMap<K, V> delegate) {
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    this.delegate = delegate;
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableMap.Builder<V, K> builder = ImmutableMap.builder();
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    for (Entry<K, V> entry : delegate.entrySet()) {
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      builder.put(entry.getValue(), entry.getKey());
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableMap<V, K> backwardMap = builder.build();
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    this.inverse = new RegularImmutableBiMap<V, K>(backwardMap, this);
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  RegularImmutableBiMap(ImmutableMap<K, V> delegate,
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      ImmutableBiMap<V, K> inverse) {
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    this.delegate = delegate;
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    this.inverse = inverse;
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override ImmutableMap<K, V> delegate() {
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate;
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableBiMap<V, K> inverse() {
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return inverse;
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
57