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) {
123 return c.isEmpty() || c.size() == 1 && otherContains(c);
127 @SuppressWarnings("checkstyle:parameterName")
128 public final boolean addAll(final Collection<? extends E> c) {
129 throw new UnsupportedOperationException();
133 @SuppressWarnings("checkstyle:parameterName")
134 public final boolean retainAll(final Collection<?> c) {
135 throw new UnsupportedOperationException();
139 @SuppressWarnings("checkstyle:parameterName")
140 public final boolean removeAll(final Collection<?> c) {
141 throw new UnsupportedOperationException();
145 public final void clear() {
146 throw new UnsupportedOperationException();
150 public abstract int hashCode();
153 @SuppressWarnings("checkstyle:equalsHashCode")
154 public final boolean equals(final Object obj) {
158 if (!(obj instanceof Set)) {
162 final Set<?> s = (Set<?>)obj;
163 return s.size() == 1 && otherContains(s);
166 private boolean otherContains(final @NonNull Collection<?> other) {
168 return other.contains(getElement());
169 } catch (ClassCastException | NullPointerException e) {
175 private static final class RegularSingletonSet<E> extends SingletonSet<E> {
176 private static final long serialVersionUID = 1L;
178 private final E element;
180 RegularSingletonSet(final E element) {
181 this.element = requireNonNull(element);
185 @SuppressWarnings("checkstyle:parameterName")
186 public boolean contains(final @Nullable Object o) {
187 return element.equals(o);
191 public E getElement() {
196 @SuppressWarnings("checkstyle:equalsHashCode")
197 public int hashCode() {
198 return getElement().hashCode();
202 public String toString() {
203 return "[" + element + ']';
207 public Spliterator<E> spliterator() {
208 return SingletonSpliterators.immutableOf(element);