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 public boolean contains(final Object o) {
37 public int hashCode() {
42 public Object getElement() {
47 public String toString() {
51 private Object readResolve() {
52 return NULL_SINGLETON;
56 @SuppressWarnings("unchecked")
57 public static <E> SingletonSet<E> of(@Nonnull final E element) {
58 if (element == null) {
59 return (SingletonSet<E>) NULL_SINGLETON;
61 return new RegularSingletonSet<>(element);
64 public abstract E getElement();
67 public final int size() {
72 public final boolean isEmpty() {
77 public final Iterator<E> iterator() {
78 return Iterators.singletonIterator(getElement());
83 public final Object[] toArray() {
84 return new Object[] { getElement() };
88 @SuppressWarnings("unchecked")
90 public final <T> T[] toArray(@Nonnull final T[] a) {
92 a[0] = (T)getElement();
96 return (T[]) new Object[] {getElement()};
100 public final boolean add(final E e) {
101 throw new UnsupportedOperationException();
105 public final boolean remove(final Object o) {
106 throw new UnsupportedOperationException();
110 public final boolean containsAll(@Nonnull final Collection<?> c) {
118 return otherContains(c);
122 public final boolean addAll(@Nonnull final Collection<? extends E> c) {
123 throw new UnsupportedOperationException();
127 public final boolean retainAll(@Nonnull final Collection<?> c) {
128 throw new UnsupportedOperationException();
132 public final boolean removeAll(@Nonnull final Collection<?> c) {
133 throw new UnsupportedOperationException();
137 public final void clear() {
138 throw new UnsupportedOperationException();
142 public abstract int hashCode();
145 public final boolean equals(final Object obj) {
149 if (!(obj instanceof Set)) {
153 final Set<?> s = (Set<?>)obj;
154 return s.size() == 1 && otherContains(s);
157 private boolean otherContains(final Collection<?> other) {
159 return other.contains(getElement());
160 } catch (ClassCastException | NullPointerException e) {
165 private static final class RegularSingletonSet<E> extends SingletonSet<E> {
166 private static final long serialVersionUID = 1L;
167 private final E element;
169 RegularSingletonSet(final E element) {
170 this.element = Preconditions.checkNotNull(element);
174 public boolean contains(final Object o) {
175 return element.equals(o);
179 public E getElement() {
184 public int hashCode() {
185 return getElement().hashCode();
189 public String toString() {
190 return "[" + element + ']';