Migrate common/util to use JDT annotations
[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<Object>() {
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     public abstract @Nullable E getElement();
73
74     @Override
75     public final int size() {
76         return 1;
77     }
78
79     @Override
80     public final boolean isEmpty() {
81         return false;
82     }
83
84     @Override
85     public final @NonNull Iterator<E> iterator() {
86         return Iterators.singletonIterator(getElement());
87     }
88
89     @Override
90     public abstract @NonNull Spliterator<E> spliterator();
91
92     @Override
93     public final @NonNull Object[] toArray() {
94         return new Object[] { getElement() };
95     }
96
97     @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
98     @Override
99     public final <T> @NonNull T[] toArray(final T[] a) {
100         if (a.length > 0) {
101             a[0] = (T)getElement();
102             return a;
103         }
104
105         return (T[]) new Object[] {getElement()};
106     }
107
108     @Override
109     @SuppressWarnings("checkstyle:parameterName")
110     public final boolean add(final E e) {
111         throw new UnsupportedOperationException();
112     }
113
114     @Override
115     @SuppressWarnings("checkstyle:parameterName")
116     public final boolean remove(final Object o) {
117         throw new UnsupportedOperationException();
118     }
119
120     @Override
121     @SuppressWarnings("checkstyle:parameterName")
122     public final boolean containsAll(final Collection<?> c) {
123         if (c.isEmpty()) {
124             return true;
125         }
126         if (c.size() != 1) {
127             return false;
128         }
129
130         return otherContains(c);
131     }
132
133     @Override
134     @SuppressWarnings("checkstyle:parameterName")
135     public final boolean addAll(final Collection<? extends E> c) {
136         throw new UnsupportedOperationException();
137     }
138
139     @Override
140     @SuppressWarnings("checkstyle:parameterName")
141     public final boolean retainAll(final Collection<?> c) {
142         throw new UnsupportedOperationException();
143     }
144
145     @Override
146     @SuppressWarnings("checkstyle:parameterName")
147     public final boolean removeAll(final Collection<?> c) {
148         throw new UnsupportedOperationException();
149     }
150
151     @Override
152     public final void clear() {
153         throw new UnsupportedOperationException();
154     }
155
156     @Override
157     public abstract int hashCode();
158
159     @Override
160     @SuppressWarnings("checkstyle:equalsHashCode")
161     public final boolean equals(final Object obj) {
162         if (obj == this) {
163             return true;
164         }
165         if (!(obj instanceof Set)) {
166             return false;
167         }
168
169         final Set<?> s = (Set<?>)obj;
170         return s.size() == 1 && otherContains(s);
171     }
172
173     private boolean otherContains(final @NonNull Collection<?> other) {
174         try {
175             return other.contains(getElement());
176         } catch (ClassCastException | NullPointerException e) {
177             return false;
178         }
179     }
180
181     @NonNullByDefault
182     private static final class RegularSingletonSet<E> extends SingletonSet<E> {
183         private static final long serialVersionUID = 1L;
184
185         private final E element;
186
187         RegularSingletonSet(final E element) {
188             this.element = requireNonNull(element);
189         }
190
191         @Override
192         @SuppressWarnings("checkstyle:parameterName")
193         public boolean contains(final @Nullable Object o) {
194             return element.equals(o);
195         }
196
197         @Override
198         public E getElement() {
199             return element;
200         }
201
202         @Override
203         @SuppressWarnings("checkstyle:equalsHashCode")
204         public int hashCode() {
205             return getElement().hashCode();
206         }
207
208         @Override
209         public String toString() {
210             return "[" + element + ']';
211         }
212
213         @Override
214         public Spliterator<E> spliterator() {
215             return SingletonSpliterators.immutableOf(element);
216         }
217     }
218 }