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<E>(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());
82 public final Object[] toArray() {
83 return new Object[] { getElement() };
86 @SuppressWarnings("unchecked")
88 public final <T> T[] toArray(final T[] a) {
90 a[0] = (T)getElement();
94 return (T[]) new Object[] { (T) getElement() };
98 public final boolean add(final E e) {
99 throw new UnsupportedOperationException();
103 public final boolean remove(final Object o) {
104 throw new UnsupportedOperationException();
108 public final boolean containsAll(final Collection<?> c) {
116 return otherContains(c);
120 public final boolean addAll(final Collection<? extends E> c) {
121 throw new UnsupportedOperationException();
125 public final boolean retainAll(final Collection<?> c) {
126 throw new UnsupportedOperationException();
130 public final boolean removeAll(final Collection<?> c) {
131 throw new UnsupportedOperationException();
135 public final void clear() {
136 throw new UnsupportedOperationException();
140 public abstract int hashCode();
143 public final boolean equals(final Object obj) {
147 if (!(obj instanceof Set)) {
151 final Set<?> s = (Set<?>)obj;
152 return s.size() == 1 && otherContains(s);
155 private boolean otherContains(final Collection<?> other) {
157 return other.contains(getElement());
158 } catch (ClassCastException | NullPointerException e) {
163 private static final class RegularSingletonSet<E> extends SingletonSet<E> {
164 private static final long serialVersionUID = 1L;
165 private final E element;
167 RegularSingletonSet(final E element) {
168 this.element = Preconditions.checkNotNull(element);
172 public boolean contains(final Object o) {
173 return element.equals(o);
177 public E getElement() {
182 public int hashCode() {
183 return getElement().hashCode();
187 public String toString() {
188 return "[" + element + ']';