Coverage Summary for Class: RegularImmutableSet (com.google.common.collect)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| RegularImmutableSet | 100% (1/1) | 35.7% (5/14) | 56.2% (18/32) |
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.annotations.VisibleForTesting; 21 import java.util.Spliterator; 22 import java.util.Spliterators; 23 import javax.annotation.CheckForNull; 24 import org.checkerframework.checker.nullness.qual.Nullable; 25 26 /** 27 * Implementation of {@link ImmutableSet} with two or more elements. 28 * 29 * @author Kevin Bourrillion 30 */ 31 @GwtCompatible(serializable = true, emulated = true) 32 @SuppressWarnings("serial") // uses writeReplace(), not default serialization 33 @ElementTypesAreNonnullByDefault 34 final class RegularImmutableSet<E> extends ImmutableSet<E> { 35 private static final Object[] EMPTY_ARRAY = new Object[0]; 36 static final RegularImmutableSet<Object> EMPTY = 37 new RegularImmutableSet<>(EMPTY_ARRAY, 0, EMPTY_ARRAY, 0); 38 39 private final transient Object[] elements; 40 private final transient int hashCode; 41 // the same values as `elements` in hashed positions (plus nulls) 42 @VisibleForTesting final transient @Nullable Object[] table; 43 // 'and' with an int to get a valid table index. 44 private final transient int mask; 45 46 RegularImmutableSet(Object[] elements, int hashCode, @Nullable Object[] table, int mask) { 47 this.elements = elements; 48 this.hashCode = hashCode; 49 this.table = table; 50 this.mask = mask; 51 } 52 53 @Override 54 public boolean contains(@CheckForNull Object target) { 55 @Nullable Object[] table = this.table; 56 if (target == null || table.length == 0) { 57 return false; 58 } 59 for (int i = Hashing.smearedHash(target); ; i++) { 60 i &= mask; 61 Object candidate = table[i]; 62 if (candidate == null) { 63 return false; 64 } else if (candidate.equals(target)) { 65 return true; 66 } 67 } 68 } 69 70 @Override 71 public int size() { 72 return elements.length; 73 } 74 75 @Override 76 public UnmodifiableIterator<E> iterator() { 77 return (UnmodifiableIterator<E>) Iterators.forArray(elements); 78 } 79 80 @Override 81 public Spliterator<E> spliterator() { 82 return Spliterators.spliterator(elements, SPLITERATOR_CHARACTERISTICS); 83 } 84 85 @Override 86 Object[] internalArray() { 87 return elements; 88 } 89 90 @Override 91 int internalArrayStart() { 92 return 0; 93 } 94 95 @Override 96 int internalArrayEnd() { 97 return elements.length; 98 } 99 100 @Override 101 int copyIntoArray(@Nullable Object[] dst, int offset) { 102 System.arraycopy(elements, 0, dst, offset, elements.length); 103 return offset + elements.length; 104 } 105 106 @Override 107 ImmutableList<E> createAsList() { 108 return (table.length == 0) 109 ? ImmutableList.<E>of() 110 : new RegularImmutableAsList<E>(this, elements); 111 } 112 113 @Override 114 boolean isPartialView() { 115 return false; 116 } 117 118 @Override 119 public int hashCode() { 120 return hashCode; 121 } 122 123 @Override 124 boolean isHashCodeFast() { 125 return true; 126 } 127 }