• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.GwtIncompatible;
21 import com.google.common.annotations.J2ktIncompatible;
22 import com.google.common.base.Preconditions;
23 import javax.annotation.CheckForNull;
24 import org.checkerframework.checker.nullness.qual.Nullable;
25 
26 /**
27  * Implementation of {@link ImmutableSet} with exactly one element.
28  *
29  * @author Kevin Bourrillion
30  * @author Nick Kralevich
31  */
32 @GwtCompatible(serializable = true, emulated = true)
33 @SuppressWarnings("serial") // uses writeReplace(), not default serialization
34 @ElementTypesAreNonnullByDefault
35 final class SingletonImmutableSet<E> extends ImmutableSet<E> {
36   // We deliberately avoid caching the asList and hashCode here, to ensure that with
37   // compressed oops, a SingletonImmutableSet packs all the way down to the optimal 16 bytes.
38 
39   final transient E element;
40 
SingletonImmutableSet(E element)41   SingletonImmutableSet(E element) {
42     this.element = Preconditions.checkNotNull(element);
43   }
44 
45   @Override
size()46   public int size() {
47     return 1;
48   }
49 
50   @Override
contains(@heckForNull Object target)51   public boolean contains(@CheckForNull Object target) {
52     return element.equals(target);
53   }
54 
55   @Override
iterator()56   public UnmodifiableIterator<E> iterator() {
57     return Iterators.singletonIterator(element);
58   }
59 
60   @Override
asList()61   public ImmutableList<E> asList() {
62     return ImmutableList.of(element);
63   }
64 
65   @Override
isPartialView()66   boolean isPartialView() {
67     return false;
68   }
69 
70   @Override
copyIntoArray(@ullable Object[] dst, int offset)71   int copyIntoArray(@Nullable Object[] dst, int offset) {
72     dst[offset] = element;
73     return offset + 1;
74   }
75 
76   @Override
hashCode()77   public final int hashCode() {
78     return element.hashCode();
79   }
80 
81   @Override
toString()82   public String toString() {
83     return '[' + element.toString() + ']';
84   }
85 
86   // redeclare to help optimizers with b/310253115
87   @SuppressWarnings("RedundantOverride")
88   @Override
89   @J2ktIncompatible // serialization
90   @GwtIncompatible // serialization
writeReplace()91   Object writeReplace() {
92     return super.writeReplace();
93   }
94 }
95