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 javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.concepts.Immutable;
22 * A {@link Set} containing a single value. For some reason neither Java nor Guava provide direct access to the retained
23 * element -- which is desirable in some situations, as is the case in {@link SharedSingletonMap#entrySet()}.
26 public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable {
27 private static final long serialVersionUID = 1L;
29 private static final SingletonSet<?> NULL_SINGLETON = new SingletonSet<Object>() {
30 private static final long serialVersionUID = 1L;
33 @SuppressWarnings("checkstyle:parameterName")
34 public boolean contains(final Object o) {
39 @SuppressWarnings("checkstyle:equalsHashCode")
40 public int hashCode() {
45 public Object getElement() {
50 public String toString() {
54 private Object readResolve() {
55 return NULL_SINGLETON;
59 @SuppressWarnings("unchecked")
60 public static <E> SingletonSet<E> of(@Nonnull final E element) {
61 if (element == null) {
62 return (SingletonSet<E>) NULL_SINGLETON;
64 return new RegularSingletonSet<>(element);
67 public abstract E getElement();
70 public final int size() {
75 public final boolean isEmpty() {
80 public final Iterator<E> iterator() {
81 return Iterators.singletonIterator(getElement());
86 public final Object[] toArray() {
87 return new Object[] { getElement() };
91 @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
93 public final <T> T[] toArray(@Nonnull final T[] a) {
95 a[0] = (T)getElement();
99 return (T[]) new Object[] {getElement()};
103 @SuppressWarnings("checkstyle:parameterName")
104 public final boolean add(final E e) {
105 throw new UnsupportedOperationException();
109 @SuppressWarnings("checkstyle:parameterName")
110 public final boolean remove(final Object o) {
111 throw new UnsupportedOperationException();
115 @SuppressWarnings("checkstyle:parameterName")
116 public final boolean containsAll(@Nonnull final Collection<?> c) {
124 return otherContains(c);
128 @SuppressWarnings("checkstyle:parameterName")
129 public final boolean addAll(@Nonnull final Collection<? extends E> c) {
130 throw new UnsupportedOperationException();
134 @SuppressWarnings("checkstyle:parameterName")
135 public final boolean retainAll(@Nonnull final Collection<?> c) {
136 throw new UnsupportedOperationException();
140 @SuppressWarnings("checkstyle:parameterName")
141 public final boolean removeAll(@Nonnull final Collection<?> c) {
142 throw new UnsupportedOperationException();
146 public final void clear() {
147 throw new UnsupportedOperationException();
151 public abstract int hashCode();
154 @SuppressWarnings("checkstyle:equalsHashCode")
155 public final boolean equals(final Object obj) {
159 if (!(obj instanceof Set)) {
163 final Set<?> s = (Set<?>)obj;
164 return s.size() == 1 && otherContains(s);
167 private boolean otherContains(final Collection<?> other) {
169 return other.contains(getElement());
170 } catch (ClassCastException | NullPointerException e) {
175 private static final class RegularSingletonSet<E> extends SingletonSet<E> {
176 private static final long serialVersionUID = 1L;
177 private final E element;
179 RegularSingletonSet(final E element) {
180 this.element = requireNonNull(element);
184 @SuppressWarnings("checkstyle:parameterName")
185 public boolean contains(final Object o) {
186 return element.equals(o);
190 public E getElement() {
195 @SuppressWarnings("checkstyle:equalsHashCode")
196 public int hashCode() {
197 return getElement().hashCode();
201 public String toString() {
202 return "[" + element + ']';