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

Class Class, % Method, % Line, %
SingletonImmutableSet 100% (1/1) 63.6% (7/11) 60% (12/20)


1 /* 2  * Copyright (C) 2007 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 import com.google.common.base.Preconditions; 21 import com.google.errorprone.annotations.concurrent.LazyInit; 22 import javax.annotation.CheckForNull; 23 import org.checkerframework.checker.nullness.qual.Nullable; 24  25 /** 26  * Implementation of {@link ImmutableSet} with exactly one element. 27  * 28  * @author Kevin Bourrillion 29  * @author Nick Kralevich 30  */ 31 @GwtCompatible(serializable = true, emulated = true) 32 @SuppressWarnings("serial") // uses writeReplace(), not default serialization 33 @ElementTypesAreNonnullByDefault 34 final class SingletonImmutableSet<E> extends ImmutableSet<E> { 35  36  final transient E element; 37  // This is transient because it will be recalculated on the first 38  // call to hashCode(). 39  // 40  // A race condition is avoided since threads will either see that the value 41  // is zero and recalculate it themselves, or two threads will see it at 42  // the same time, and both recalculate it. If the cachedHashCode is 0, 43  // it will always be recalculated, unfortunately. 44  @LazyInit private transient int cachedHashCode; 45  46  SingletonImmutableSet(E element) { 47  this.element = Preconditions.checkNotNull(element); 48  } 49  50  SingletonImmutableSet(E element, int hashCode) { 51  // Guaranteed to be non-null by the presence of the pre-computed hash code. 52  this.element = element; 53  cachedHashCode = hashCode; 54  } 55  56  @Override 57  public int size() { 58  return 1; 59  } 60  61  @Override 62  public boolean contains(@CheckForNull Object target) { 63  return element.equals(target); 64  } 65  66  @Override 67  public UnmodifiableIterator<E> iterator() { 68  return Iterators.singletonIterator(element); 69  } 70  71  @Override 72  ImmutableList<E> createAsList() { 73  return ImmutableList.of(element); 74  } 75  76  @Override 77  boolean isPartialView() { 78  return false; 79  } 80  81  @Override 82  int copyIntoArray(@Nullable Object[] dst, int offset) { 83  dst[offset] = element; 84  return offset + 1; 85  } 86  87  @Override 88  public final int hashCode() { 89  // Racy single-check. 90  int code = cachedHashCode; 91  if (code == 0) { 92  cachedHashCode = code = element.hashCode(); 93  } 94  return code; 95  } 96  97  @Override 98  boolean isHashCodeFast() { 99  return cachedHashCode != 0; 100  } 101  102  @Override 103  public String toString() { 104  return '[' + element.toString() + ']'; 105  } 106 }