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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.io.Serial;
16 import java.io.Serializable;
17 import java.util.Collection;
18 import java.util.Iterator;
20 import java.util.Spliterator;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.opendaylight.yangtools.concepts.Immutable;
27 * A {@link Set} containing a single value. For some reason neither Java nor Guava provide direct access to the retained
28 * element -- which is desirable in some situations, as is the case in {@link SharedSingletonMap#entrySet()}.
31 public abstract sealed class SingletonSet<E> implements Set<E>, Immutable, Serializable {
33 private static final long serialVersionUID = 1L;
35 @SuppressWarnings("unchecked")
36 public static <E> @NonNull SingletonSet<E> of(final @Nullable E element) {
37 return element == null ? (SingletonSet<E>) NullElement.INSTANCE : new Regular<>(element);
41 * Return the single element contained in this set.
43 * @return This set's element.
45 public abstract E getElement();
48 public final int size() {
53 public final boolean isEmpty() {
58 public final @NonNull Iterator<E> iterator() {
59 return Iterators.singletonIterator(getElement());
63 public abstract @NonNull Spliterator<E> spliterator();
66 public final @NonNull Object[] toArray() {
67 return new Object[] { getElement() };
70 @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
72 public final <T> @NonNull T[] toArray(final T[] a) {
74 a[0] = (T)getElement();
78 return (T[]) new Object[] {getElement()};
82 @SuppressWarnings("checkstyle:parameterName")
83 public final boolean add(final E e) {
84 throw new UnsupportedOperationException();
88 @SuppressWarnings("checkstyle:parameterName")
89 public final boolean remove(final Object o) {
90 throw new UnsupportedOperationException();
94 @SuppressWarnings("checkstyle:parameterName")
95 public final boolean containsAll(final Collection<?> c) {
96 return c.isEmpty() || c.size() == 1 && otherContains(c);
100 @SuppressWarnings("checkstyle:parameterName")
101 public final boolean addAll(final Collection<? extends E> c) {
102 throw new UnsupportedOperationException();
106 @SuppressWarnings("checkstyle:parameterName")
107 public final boolean retainAll(final Collection<?> c) {
108 throw new UnsupportedOperationException();
112 @SuppressWarnings("checkstyle:parameterName")
113 public final boolean removeAll(final Collection<?> c) {
114 throw new UnsupportedOperationException();
118 public final void clear() {
119 throw new UnsupportedOperationException();
123 public abstract int hashCode();
126 @SuppressWarnings("checkstyle:equalsHashCode")
127 public final boolean equals(final Object obj) {
128 return obj == this || obj instanceof Set<?> other && other.size() == 1 && otherContains(other);
132 final Object writeReplace() {
133 return new SSv1(getElement());
136 @SuppressFBWarnings(value = "DCN_NULLPOINTER_EXCEPTION",
137 justification = "https://github.com/spotbugs/spotbugs/issues/1954")
138 private boolean otherContains(final @NonNull Collection<?> other) {
140 return other.contains(getElement());
141 } catch (ClassCastException | NullPointerException e) {
146 private static final class NullElement<E> extends SingletonSet<E> {
148 private static final long serialVersionUID = 1L;
149 static final @NonNull NullElement<?> INSTANCE = new NullElement<>();
152 @SuppressWarnings("checkstyle:parameterName")
153 public boolean contains(final Object o) {
158 @SuppressWarnings("checkstyle:equalsHashCode")
159 public int hashCode() {
164 public E getElement() {
169 public Spliterator<E> spliterator() {
170 return SingletonSpliterators.immutableOfNull();
174 public String toString() {
180 private static final class Regular<E> extends SingletonSet<E> {
182 private static final long serialVersionUID = 1L;
184 private final @NonNull E element;
186 Regular(final E element) {
187 this.element = requireNonNull(element);
191 @SuppressWarnings("checkstyle:parameterName")
192 public boolean contains(final @Nullable Object o) {
193 return element.equals(o);
197 public @NonNull E getElement() {
202 @SuppressWarnings("checkstyle:equalsHashCode")
203 public int hashCode() {
204 return getElement().hashCode();
208 public String toString() {
209 return "[" + element + ']';
213 public Spliterator<E> spliterator() {
214 return SingletonSpliterators.immutableOf(element);