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 public int hashCode() {
43 public Object getElement() {
48 public String toString() {
52 private Object readResolve() {
53 return NULL_SINGLETON;
57 @SuppressWarnings("unchecked")
58 public static <E> SingletonSet<E> of(@Nonnull final E element) {
59 if (element == null) {
60 return (SingletonSet<E>) NULL_SINGLETON;
62 return new RegularSingletonSet<>(element);
65 public abstract E getElement();
68 public final int size() {
73 public final boolean isEmpty() {
78 public final Iterator<E> iterator() {
79 return Iterators.singletonIterator(getElement());
84 public final Object[] toArray() {
85 return new Object[] { getElement() };
89 @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
91 public final <T> T[] toArray(@Nonnull final T[] a) {
93 a[0] = (T)getElement();
97 return (T[]) new Object[] {getElement()};
101 @SuppressWarnings("checkstyle:parameterName")
102 public final boolean add(final E e) {
103 throw new UnsupportedOperationException();
107 @SuppressWarnings("checkstyle:parameterName")
108 public final boolean remove(final Object o) {
109 throw new UnsupportedOperationException();
113 @SuppressWarnings("checkstyle:parameterName")
114 public final boolean containsAll(@Nonnull final Collection<?> c) {
122 return otherContains(c);
126 @SuppressWarnings("checkstyle:parameterName")
127 public final boolean addAll(@Nonnull final Collection<? extends E> c) {
128 throw new UnsupportedOperationException();
132 @SuppressWarnings("checkstyle:parameterName")
133 public final boolean retainAll(@Nonnull final Collection<?> c) {
134 throw new UnsupportedOperationException();
138 @SuppressWarnings("checkstyle:parameterName")
139 public final boolean removeAll(@Nonnull final Collection<?> c) {
140 throw new UnsupportedOperationException();
144 public final void clear() {
145 throw new UnsupportedOperationException();
149 public abstract int hashCode();
152 public final boolean equals(final Object obj) {
156 if (!(obj instanceof Set)) {
160 final Set<?> s = (Set<?>)obj;
161 return s.size() == 1 && otherContains(s);
164 private boolean otherContains(final Collection<?> other) {
166 return other.contains(getElement());
167 } catch (ClassCastException | NullPointerException e) {
172 private static final class RegularSingletonSet<E> extends SingletonSet<E> {
173 private static final long serialVersionUID = 1L;
174 private final E element;
176 RegularSingletonSet(final E element) {
177 this.element = Preconditions.checkNotNull(element);
181 @SuppressWarnings("checkstyle:parameterName")
182 public boolean contains(final Object o) {
183 return element.equals(o);
187 public E getElement() {
192 public int hashCode() {
193 return getElement().hashCode();
197 public String toString() {
198 return "[" + element + ']';