2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util;
10 import static java.util.Objects.requireNonNull;
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;
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;
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()}.
30 public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable {
31 private static final long serialVersionUID = 1L;
33 private static final SingletonSet<?> NULL_SINGLETON = new SingletonSet<>() {
34 private static final long serialVersionUID = 1L;
37 @SuppressWarnings("checkstyle:parameterName")
38 public boolean contains(final Object o) {
43 @SuppressWarnings("checkstyle:equalsHashCode")
44 public int hashCode() {
49 public @Nullable Object getElement() {
54 public @NonNull Spliterator<Object> spliterator() {
55 return SingletonSpliterators.immutableOfNull();
59 public @NonNull String toString() {
63 private Object readResolve() {
64 return NULL_SINGLETON;
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);
74 * Return the single element contained in this set.
76 * @return This set's element.
78 public abstract E getElement();
81 public final int size() {
86 public final boolean isEmpty() {
91 public final @NonNull Iterator<E> iterator() {
92 return Iterators.singletonIterator(getElement());
96 public abstract @NonNull Spliterator<E> spliterator();
99 public final @NonNull Object[] toArray() {
100 return new Object[] { getElement() };
103 @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
105 public final <T> @NonNull T[] toArray(final T[] a) {
107 a[0] = (T)getElement();
111 return (T[]) new Object[] {getElement()};
115 @SuppressWarnings("checkstyle:parameterName")
116 public final boolean add(final E e) {
117 throw new UnsupportedOperationException();
121 @SuppressWarnings("checkstyle:parameterName")
122 public final boolean remove(final Object o) {
123 throw new UnsupportedOperationException();
127 @SuppressWarnings("checkstyle:parameterName")
128 public final boolean containsAll(final Collection<?> c) {
129 return c.isEmpty() || c.size() == 1 && otherContains(c);
133 @SuppressWarnings("checkstyle:parameterName")
134 public final boolean addAll(final Collection<? extends E> c) {
135 throw new UnsupportedOperationException();
139 @SuppressWarnings("checkstyle:parameterName")
140 public final boolean retainAll(final Collection<?> c) {
141 throw new UnsupportedOperationException();
145 @SuppressWarnings("checkstyle:parameterName")
146 public final boolean removeAll(final Collection<?> c) {
147 throw new UnsupportedOperationException();
151 public final void clear() {
152 throw new UnsupportedOperationException();
156 public abstract int hashCode();
159 @SuppressWarnings("checkstyle:equalsHashCode")
160 public final boolean equals(final Object obj) {
164 if (!(obj instanceof Set)) {
168 final Set<?> s = (Set<?>)obj;
169 return s.size() == 1 && otherContains(s);
172 @SuppressFBWarnings(value = "DCN_NULLPOINTER_EXCEPTION",
173 justification = "https://github.com/spotbugs/spotbugs/issues/1954")
174 private boolean otherContains(final @NonNull Collection<?> other) {
176 return other.contains(getElement());
177 } catch (ClassCastException | NullPointerException e) {
183 private static final class RegularSingletonSet<E> extends SingletonSet<E> {
184 private static final long serialVersionUID = 1L;
186 private final E element;
188 RegularSingletonSet(final E element) {
189 this.element = requireNonNull(element);
193 @SuppressWarnings("checkstyle:parameterName")
194 public boolean contains(final @Nullable Object o) {
195 return element.equals(o);
199 public E getElement() {
204 @SuppressWarnings("checkstyle:equalsHashCode")
205 public int hashCode() {
206 return getElement().hashCode();
210 public String toString() {
211 return "[" + element + ']';
215 public Spliterator<E> spliterator() {
216 return SingletonSpliterators.immutableOf(element);