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 com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Iterators;
13 import java.io.Serializable;
14 import java.util.Collection;
15 import java.util.Iterator;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.yangtools.concepts.Immutable;
21 * A {@link Set} containing a single value. For some reason neither Java nor Guava provide direct access to the retained
22 * element -- which is desirable in some situations, as is the case in {@link SharedSingletonMap#entrySet()}.
25 public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable {
26 private static final long serialVersionUID = 1L;
28 private static final SingletonSet<?> NULL_SINGLETON = new SingletonSet<Object>() {
29 private static final long serialVersionUID = 1L;
32 @SuppressWarnings("checkstyle:parameterName")
33 public boolean contains(final Object o) {
38 @SuppressWarnings("checkstyle:equalsHashCode")
39 public int hashCode() {
44 public Object getElement() {
49 public String toString() {
53 private Object readResolve() {
54 return NULL_SINGLETON;
58 @SuppressWarnings("unchecked")
59 public static <E> SingletonSet<E> of(@Nonnull final E element) {
60 if (element == null) {
61 return (SingletonSet<E>) NULL_SINGLETON;
63 return new RegularSingletonSet<>(element);
66 public abstract E getElement();
69 public final int size() {
74 public final boolean isEmpty() {
79 public final Iterator<E> iterator() {
80 return Iterators.singletonIterator(getElement());
85 public final Object[] toArray() {
86 return new Object[] { getElement() };
90 @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
92 public final <T> T[] toArray(@Nonnull final T[] a) {
94 a[0] = (T)getElement();
98 return (T[]) new Object[] {getElement()};
102 @SuppressWarnings("checkstyle:parameterName")
103 public final boolean add(final E e) {
104 throw new UnsupportedOperationException();
108 @SuppressWarnings("checkstyle:parameterName")
109 public final boolean remove(final Object o) {
110 throw new UnsupportedOperationException();
114 @SuppressWarnings("checkstyle:parameterName")
115 public final boolean containsAll(@Nonnull final Collection<?> c) {
123 return otherContains(c);
127 @SuppressWarnings("checkstyle:parameterName")
128 public final boolean addAll(@Nonnull final Collection<? extends E> c) {
129 throw new UnsupportedOperationException();
133 @SuppressWarnings("checkstyle:parameterName")
134 public final boolean retainAll(@Nonnull final Collection<?> c) {
135 throw new UnsupportedOperationException();
139 @SuppressWarnings("checkstyle:parameterName")
140 public final boolean removeAll(@Nonnull 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 Collection<?> other) {
168 return other.contains(getElement());
169 } catch (ClassCastException | NullPointerException e) {
174 private static final class RegularSingletonSet<E> extends SingletonSet<E> {
175 private static final long serialVersionUID = 1L;
176 private final E element;
178 RegularSingletonSet(final E element) {
179 this.element = Preconditions.checkNotNull(element);
183 @SuppressWarnings("checkstyle:parameterName")
184 public boolean contains(final Object o) {
185 return element.equals(o);
189 public E getElement() {
194 @SuppressWarnings("checkstyle:equalsHashCode")
195 public int hashCode() {
196 return getElement().hashCode();
200 public String toString() {
201 return "[" + element + ']';