Bump odlparent to 10.0.0
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / SingletonSet.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.Iterators;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.io.Serializable;
16 import java.util.Collection;
17 import java.util.Iterator;
18 import java.util.Set;
19 import java.util.Spliterator;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.yangtools.concepts.Immutable;
24
25 /**
26  * A {@link Set} containing a single value. For some reason neither Java nor Guava provide direct access to the retained
27  * element -- which is desirable in some situations, as is the case in {@link SharedSingletonMap#entrySet()}.
28  */
29 @Beta
30 public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable {
31     private static final long serialVersionUID = 1L;
32
33     private static final SingletonSet<?> NULL_SINGLETON = new SingletonSet<>() {
34         private static final long serialVersionUID = 1L;
35
36         @Override
37         @SuppressWarnings("checkstyle:parameterName")
38         public boolean contains(final Object o) {
39             return o == null;
40         }
41
42         @Override
43         @SuppressWarnings("checkstyle:equalsHashCode")
44         public int hashCode() {
45             return 0;
46         }
47
48         @Override
49         public @Nullable Object getElement() {
50             return null;
51         }
52
53         @Override
54         public @NonNull Spliterator<Object> spliterator() {
55             return SingletonSpliterators.immutableOfNull();
56         }
57
58         @Override
59         public @NonNull String toString() {
60             return "[null]";
61         }
62
63         private Object readResolve() {
64             return NULL_SINGLETON;
65         }
66     };
67
68     @SuppressWarnings("unchecked")
69     public static <E> @NonNull SingletonSet<E> of(final @Nullable E element) {
70         return element == null ? (SingletonSet<E>) NULL_SINGLETON : new RegularSingletonSet<>(element);
71     }
72
73     /**
74      * Return the single element contained in this set.
75      *
76      * @return This set's element.
77      */
78     public abstract E getElement();
79
80     @Override
81     public final int size() {
82         return 1;
83     }
84
85     @Override
86     public final boolean isEmpty() {
87         return false;
88     }
89
90     @Override
91     public final @NonNull Iterator<E> iterator() {
92         return Iterators.singletonIterator(getElement());
93     }
94
95     @Override
96     public abstract @NonNull Spliterator<E> spliterator();
97
98     @Override
99     public final @NonNull Object[] toArray() {
100         return new Object[] { getElement() };
101     }
102
103     @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
104     @Override
105     public final <T> @NonNull T[] toArray(final T[] a) {
106         if (a.length > 0) {
107             a[0] = (T)getElement();
108             return a;
109         }
110
111         return (T[]) new Object[] {getElement()};
112     }
113
114     @Override
115     @SuppressWarnings("checkstyle:parameterName")
116     public final boolean add(final E e) {
117         throw new UnsupportedOperationException();
118     }
119
120     @Override
121     @SuppressWarnings("checkstyle:parameterName")
122     public final boolean remove(final Object o) {
123         throw new UnsupportedOperationException();
124     }
125
126     @Override
127     @SuppressWarnings("checkstyle:parameterName")
128     public final boolean containsAll(final Collection<?> c) {
129         return c.isEmpty() || c.size() == 1 && otherContains(c);
130     }
131
132     @Override
133     @SuppressWarnings("checkstyle:parameterName")
134     public final boolean addAll(final Collection<? extends E> c) {
135         throw new UnsupportedOperationException();
136     }
137
138     @Override
139     @SuppressWarnings("checkstyle:parameterName")
140     public final boolean retainAll(final Collection<?> c) {
141         throw new UnsupportedOperationException();
142     }
143
144     @Override
145     @SuppressWarnings("checkstyle:parameterName")
146     public final boolean removeAll(final Collection<?> c) {
147         throw new UnsupportedOperationException();
148     }
149
150     @Override
151     public final void clear() {
152         throw new UnsupportedOperationException();
153     }
154
155     @Override
156     public abstract int hashCode();
157
158     @Override
159     @SuppressWarnings("checkstyle:equalsHashCode")
160     public final boolean equals(final Object obj) {
161         if (obj == this) {
162             return true;
163         }
164         if (!(obj instanceof Set)) {
165             return false;
166         }
167
168         final Set<?> s = (Set<?>)obj;
169         return s.size() == 1 && otherContains(s);
170     }
171
172     @SuppressFBWarnings(value = "DCN_NULLPOINTER_EXCEPTION",
173         justification = "https://github.com/spotbugs/spotbugs/issues/1954")
174     private boolean otherContains(final @NonNull Collection<?> other) {
175         try {
176             return other.contains(getElement());
177         } catch (ClassCastException | NullPointerException e) {
178             return false;
179         }
180     }
181
182     @NonNullByDefault
183     private static final class RegularSingletonSet<E> extends SingletonSet<E> {
184         private static final long serialVersionUID = 1L;
185
186         private final E element;
187
188         RegularSingletonSet(final E element) {
189             this.element = requireNonNull(element);
190         }
191
192         @Override
193         @SuppressWarnings("checkstyle:parameterName")
194         public boolean contains(final @Nullable Object o) {
195             return element.equals(o);
196         }
197
198         @Override
199         public E getElement() {
200             return element;
201         }
202
203         @Override
204         @SuppressWarnings("checkstyle:equalsHashCode")
205         public int hashCode() {
206             return getElement().hashCode();
207         }
208
209         @Override
210         public String toString() {
211             return "[" + element + ']';
212         }
213
214         @Override
215         public Spliterator<E> spliterator() {
216             return SingletonSpliterators.immutableOf(element);
217         }
218     }
219 }