51f65dfac6abd060a8e3ae4fdb188b8acddfe389
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / SingletonSet.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.util;
9
10 import static java.util.Objects.requireNonNull;
11
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;
17 import java.util.Set;
18 import java.util.Spliterator;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.yangtools.concepts.Immutable;
23
24 /**
25  * A {@link Set} containing a single value. For some reason neither Java nor Guava provide direct access to the retained
26  * element -- which is desirable in some situations, as is the case in {@link SharedSingletonMap#entrySet()}.
27  */
28 @Beta
29 public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable {
30     private static final long serialVersionUID = 1L;
31
32     private static final SingletonSet<?> NULL_SINGLETON = new SingletonSet<>() {
33         private static final long serialVersionUID = 1L;
34
35         @Override
36         @SuppressWarnings("checkstyle:parameterName")
37         public boolean contains(final Object o) {
38             return o == null;
39         }
40
41         @Override
42         @SuppressWarnings("checkstyle:equalsHashCode")
43         public int hashCode() {
44             return 0;
45         }
46
47         @Override
48         public @Nullable Object getElement() {
49             return null;
50         }
51
52         @Override
53         public @NonNull Spliterator<Object> spliterator() {
54             return SingletonSpliterators.immutableOfNull();
55         }
56
57         @Override
58         public @NonNull String toString() {
59             return "[null]";
60         }
61
62         private Object readResolve() {
63             return NULL_SINGLETON;
64         }
65     };
66
67     @SuppressWarnings("unchecked")
68     public static <E> @NonNull SingletonSet<E> of(final @Nullable E element) {
69         return element == null ? (SingletonSet<E>) NULL_SINGLETON : new RegularSingletonSet<>(element);
70     }
71
72     /**
73      * Return the single element contained in this set.
74      *
75      * @return This set's element.
76      */
77     public abstract E getElement();
78
79     @Override
80     public final int size() {
81         return 1;
82     }
83
84     @Override
85     public final boolean isEmpty() {
86         return false;
87     }
88
89     @Override
90     public final @NonNull Iterator<E> iterator() {
91         return Iterators.singletonIterator(getElement());
92     }
93
94     @Override
95     public abstract @NonNull Spliterator<E> spliterator();
96
97     @Override
98     public final @NonNull Object[] toArray() {
99         return new Object[] { getElement() };
100     }
101
102     @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
103     @Override
104     public final <T> @NonNull T[] toArray(final T[] a) {
105         if (a.length > 0) {
106             a[0] = (T)getElement();
107             return a;
108         }
109
110         return (T[]) new Object[] {getElement()};
111     }
112
113     @Override
114     @SuppressWarnings("checkstyle:parameterName")
115     public final boolean add(final E e) {
116         throw new UnsupportedOperationException();
117     }
118
119     @Override
120     @SuppressWarnings("checkstyle:parameterName")
121     public final boolean remove(final Object o) {
122         throw new UnsupportedOperationException();
123     }
124
125     @Override
126     @SuppressWarnings("checkstyle:parameterName")
127     public final boolean containsAll(final Collection<?> c) {
128         return c.isEmpty() || c.size() == 1 && otherContains(c);
129     }
130
131     @Override
132     @SuppressWarnings("checkstyle:parameterName")
133     public final boolean addAll(final Collection<? extends E> c) {
134         throw new UnsupportedOperationException();
135     }
136
137     @Override
138     @SuppressWarnings("checkstyle:parameterName")
139     public final boolean retainAll(final Collection<?> c) {
140         throw new UnsupportedOperationException();
141     }
142
143     @Override
144     @SuppressWarnings("checkstyle:parameterName")
145     public final boolean removeAll(final Collection<?> c) {
146         throw new UnsupportedOperationException();
147     }
148
149     @Override
150     public final void clear() {
151         throw new UnsupportedOperationException();
152     }
153
154     @Override
155     public abstract int hashCode();
156
157     @Override
158     @SuppressWarnings("checkstyle:equalsHashCode")
159     public final boolean equals(final Object obj) {
160         if (obj == this) {
161             return true;
162         }
163         if (!(obj instanceof Set)) {
164             return false;
165         }
166
167         final Set<?> s = (Set<?>)obj;
168         return s.size() == 1 && otherContains(s);
169     }
170
171     private boolean otherContains(final @NonNull Collection<?> other) {
172         try {
173             return other.contains(getElement());
174         } catch (ClassCastException | NullPointerException e) {
175             return false;
176         }
177     }
178
179     @NonNullByDefault
180     private static final class RegularSingletonSet<E> extends SingletonSet<E> {
181         private static final long serialVersionUID = 1L;
182
183         private final E element;
184
185         RegularSingletonSet(final E element) {
186             this.element = requireNonNull(element);
187         }
188
189         @Override
190         @SuppressWarnings("checkstyle:parameterName")
191         public boolean contains(final @Nullable Object o) {
192             return element.equals(o);
193         }
194
195         @Override
196         public E getElement() {
197             return element;
198         }
199
200         @Override
201         @SuppressWarnings("checkstyle:equalsHashCode")
202         public int hashCode() {
203             return getElement().hashCode();
204         }
205
206         @Override
207         public String toString() {
208             return "[" + element + ']';
209         }
210
211         @Override
212         public Spliterator<E> spliterator() {
213             return SingletonSpliterators.immutableOf(element);
214         }
215     }
216 }