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 java.io.Serializable;
15 import java.util.Collection;
16 import java.util.Iterator;
18 import java.util.Spliterator;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.yangtools.concepts.Immutable;
25 * A {@link Set} containing a single value. For some reason neither Java nor Guava provide direct access to the retained
26 * element -- which is desirable in some situations, as is the case in {@link SharedSingletonMap#entrySet()}.
29 public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable {
30 private static final long serialVersionUID = 1L;
32 private static final SingletonSet<?> NULL_SINGLETON = new SingletonSet<Object>() {
33 private static final long serialVersionUID = 1L;
36 @SuppressWarnings("checkstyle:parameterName")
37 public boolean contains(final Object o) {
42 @SuppressWarnings("checkstyle:equalsHashCode")
43 public int hashCode() {
48 public @Nullable Object getElement() {
53 public @NonNull Spliterator<Object> spliterator() {
54 return SingletonSpliterators.immutableOfNull();
58 public @NonNull String toString() {
62 private Object readResolve() {
63 return NULL_SINGLETON;
67 @SuppressWarnings("unchecked")
68 public static <E> @NonNull SingletonSet<E> of(final @Nullable E element) {
69 return element == null ? (SingletonSet<E>) NULL_SINGLETON : new RegularSingletonSet<>(element);
72 public abstract @Nullable E getElement();
75 public final int size() {
80 public final boolean isEmpty() {
85 public final @NonNull Iterator<E> iterator() {
86 return Iterators.singletonIterator(getElement());
90 public abstract @NonNull Spliterator<E> spliterator();
93 public final @NonNull Object[] toArray() {
94 return new Object[] { getElement() };
97 @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
99 public final <T> @NonNull T[] toArray(final T[] a) {
101 a[0] = (T)getElement();
105 return (T[]) new Object[] {getElement()};
109 @SuppressWarnings("checkstyle:parameterName")
110 public final boolean add(final E e) {
111 throw new UnsupportedOperationException();
115 @SuppressWarnings("checkstyle:parameterName")
116 public final boolean remove(final Object o) {
117 throw new UnsupportedOperationException();
121 @SuppressWarnings("checkstyle:parameterName")
122 public final boolean containsAll(final Collection<?> c) {
130 return otherContains(c);
134 @SuppressWarnings("checkstyle:parameterName")
135 public final boolean addAll(final Collection<? extends E> c) {
136 throw new UnsupportedOperationException();
140 @SuppressWarnings("checkstyle:parameterName")
141 public final boolean retainAll(final Collection<?> c) {
142 throw new UnsupportedOperationException();
146 @SuppressWarnings("checkstyle:parameterName")
147 public final boolean removeAll(final Collection<?> c) {
148 throw new UnsupportedOperationException();
152 public final void clear() {
153 throw new UnsupportedOperationException();
157 public abstract int hashCode();
160 @SuppressWarnings("checkstyle:equalsHashCode")
161 public final boolean equals(final Object obj) {
165 if (!(obj instanceof Set)) {
169 final Set<?> s = (Set<?>)obj;
170 return s.size() == 1 && otherContains(s);
173 private boolean otherContains(final @NonNull Collection<?> other) {
175 return other.contains(getElement());
176 } catch (ClassCastException | NullPointerException e) {
182 private static final class RegularSingletonSet<E> extends SingletonSet<E> {
183 private static final long serialVersionUID = 1L;
185 private final E element;
187 RegularSingletonSet(final E element) {
188 this.element = requireNonNull(element);
192 @SuppressWarnings("checkstyle:parameterName")
193 public boolean contains(final @Nullable Object o) {
194 return element.equals(o);
198 public E getElement() {
203 @SuppressWarnings("checkstyle:equalsHashCode")
204 public int hashCode() {
205 return getElement().hashCode();
209 public String toString() {
210 return "[" + element + ']';
214 public Spliterator<E> spliterator() {
215 return SingletonSpliterators.immutableOf(element);