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.GwtCompatible; 20 21 /** 22 * Bimap with one or more mappings. 23 * 24 * @author Jared Levy 25 */ 26 @GwtCompatible(serializable = true, emulated = true) 27 @SuppressWarnings("serial") // uses writeReplace(), not default serialization 28 class RegularImmutableBiMap<K, V> extends ImmutableBiMap<K, V> { 29 final transient ImmutableMap<K, V> delegate; 30 final transient ImmutableBiMap<V, K> inverse; 31 32 RegularImmutableBiMap(ImmutableMap<K, V> delegate) { 33 this.delegate = 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 this.delegate = delegate; 46 this.inverse = inverse; 47 } 48 49 @Override ImmutableMap<K, V> delegate() { 50 return delegate; 51 } 52 53 @Override public ImmutableBiMap<V, K> inverse() { 54 return inverse; 55 } 56 57 @Override boolean isPartialView() { 58 return delegate.isPartialView() || inverse.delegate().isPartialView(); 59 } 60 } 61