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 javax.annotation.Nonnull;
20 import org.opendaylight.yangtools.concepts.Immutable;
23 * A {@link Set} containing a single value. For some reason neither Java nor Guava provide direct access to the retained
24 * element -- which is desirable in some situations, as is the case in {@link SharedSingletonMap#entrySet()}.
27 public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable {
28 private static final long serialVersionUID = 1L;
30 private static final SingletonSet<?> NULL_SINGLETON = new SingletonSet<Object>() {
31 private static final long serialVersionUID = 1L;
34 @SuppressWarnings("checkstyle:parameterName")
35 public boolean contains(final Object o) {
40 @SuppressWarnings("checkstyle:equalsHashCode")
41 public int hashCode() {
46 public Object getElement() {
51 public Spliterator<Object> spliterator() {
52 return SingletonSpliterators.immutableOfNull();
56 public String toString() {
60 private Object readResolve() {
61 return NULL_SINGLETON;
65 @SuppressWarnings("unchecked")
66 public static <E> SingletonSet<E> of(@Nonnull final E element) {
67 if (element == null) {
68 return (SingletonSet<E>) NULL_SINGLETON;
70 return new RegularSingletonSet<>(element);
73 public abstract E getElement();
76 public final int size() {
81 public final boolean isEmpty() {
86 public final Iterator<E> iterator() {
87 return Iterators.singletonIterator(getElement());
91 public abstract Spliterator<E> spliterator();
95 public final Object[] toArray() {
96 return new Object[] { getElement() };
100 @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
102 public final <T> T[] toArray(@Nonnull final T[] a) {
104 a[0] = (T)getElement();
108 return (T[]) new Object[] {getElement()};
112 @SuppressWarnings("checkstyle:parameterName")
113 public final boolean add(final E e) {
114 throw new UnsupportedOperationException();
118 @SuppressWarnings("checkstyle:parameterName")
119 public final boolean remove(final Object o) {
120 throw new UnsupportedOperationException();
124 @SuppressWarnings("checkstyle:parameterName")
125 public final boolean containsAll(@Nonnull final Collection<?> c) {
133 return otherContains(c);
137 @SuppressWarnings("checkstyle:parameterName")
138 public final boolean addAll(@Nonnull final Collection<? extends E> c) {
139 throw new UnsupportedOperationException();
143 @SuppressWarnings("checkstyle:parameterName")
144 public final boolean retainAll(@Nonnull final Collection<?> c) {
145 throw new UnsupportedOperationException();
149 @SuppressWarnings("checkstyle:parameterName")
150 public final boolean removeAll(@Nonnull final Collection<?> c) {
151 throw new UnsupportedOperationException();
155 public final void clear() {
156 throw new UnsupportedOperationException();
160 public abstract int hashCode();
163 @SuppressWarnings("checkstyle:equalsHashCode")
164 public final boolean equals(final Object obj) {
168 if (!(obj instanceof Set)) {
172 final Set<?> s = (Set<?>)obj;
173 return s.size() == 1 && otherContains(s);
176 private boolean otherContains(final Collection<?> other) {
178 return other.contains(getElement());
179 } catch (ClassCastException | NullPointerException e) {
184 private static final class RegularSingletonSet<E> extends SingletonSet<E> {
185 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 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);