Coverage Summary for Class: SingletonImmutableBiMap (com.google.common.collect)

Class Class, % Method, % Line, %
SingletonImmutableBiMap 100% (1/1) 36.4% (4/11) 34.6% (9/26)


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 static com.google.common.base.Preconditions.checkNotNull; 20 import static com.google.common.collect.CollectPreconditions.checkEntryNotNull; 21  22 import com.google.common.annotations.GwtCompatible; 23 import com.google.errorprone.annotations.concurrent.LazyInit; 24 import com.google.j2objc.annotations.RetainedWith; 25 import java.util.function.BiConsumer; 26 import org.checkerframework.checker.nullness.qual.Nullable; 27  28 /** 29  * Implementation of {@link ImmutableMap} with exactly one entry. 30  * 31  * @author Jesse Wilson 32  * @author Kevin Bourrillion 33  */ 34 @GwtCompatible(serializable = true, emulated = true) 35 @SuppressWarnings("serial") // uses writeReplace(), not default serialization 36 final class SingletonImmutableBiMap<K, V> extends ImmutableBiMap<K, V> { 37  38  final transient K singleKey; 39  final transient V singleValue; 40  41  SingletonImmutableBiMap(K singleKey, V singleValue) { 42  checkEntryNotNull(singleKey, singleValue); 43  this.singleKey = singleKey; 44  this.singleValue = singleValue; 45  this.inverse = null; 46  } 47  48  private SingletonImmutableBiMap(K singleKey, V singleValue, ImmutableBiMap<V, K> inverse) { 49  this.singleKey = singleKey; 50  this.singleValue = singleValue; 51  this.inverse = inverse; 52  } 53  54  @Override 55  public V get(@Nullable Object key) { 56  return singleKey.equals(key) ? singleValue : null; 57  } 58  59  @Override 60  public int size() { 61  return 1; 62  } 63  64  @Override 65  public void forEach(BiConsumer<? super K, ? super V> action) { 66  checkNotNull(action).accept(singleKey, singleValue); 67  } 68  69  @Override 70  public boolean containsKey(@Nullable Object key) { 71  return singleKey.equals(key); 72  } 73  74  @Override 75  public boolean containsValue(@Nullable Object value) { 76  return singleValue.equals(value); 77  } 78  79  @Override 80  boolean isPartialView() { 81  return false; 82  } 83  84  @Override 85  ImmutableSet<Entry<K, V>> createEntrySet() { 86  return ImmutableSet.of(Maps.immutableEntry(singleKey, singleValue)); 87  } 88  89  @Override 90  ImmutableSet<K> createKeySet() { 91  return ImmutableSet.of(singleKey); 92  } 93  94  private final transient @Nullable ImmutableBiMap<V, K> inverse; 95  @LazyInit @RetainedWith private transient @Nullable ImmutableBiMap<V, K> lazyInverse; 96  97  @Override 98  public ImmutableBiMap<V, K> inverse() { 99  if (inverse != null) { 100  return inverse; 101  } else { 102  // racy single-check idiom 103  ImmutableBiMap<V, K> result = lazyInverse; 104  if (result == null) { 105  return lazyInverse = new SingletonImmutableBiMap<>(singleValue, singleKey, this); 106  } else { 107  return result; 108  } 109  } 110  } 111 }