Merge branch 'master' of ../controller
[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         return c.isEmpty() || c.size() == 1 && otherContains(c);
124     }
125
126     @Override
127     @SuppressWarnings("checkstyle:parameterName")
128     public final boolean addAll(final Collection<? extends E> c) {
129         throw new UnsupportedOperationException();
130     }
131
132     @Override
133     @SuppressWarnings("checkstyle:parameterName")
134     public final boolean retainAll(final Collection<?> c) {
135         throw new UnsupportedOperationException();
136     }
137
138     @Override
139     @SuppressWarnings("checkstyle:parameterName")
140     public final boolean removeAll(final Collection<?> c) {
141         throw new UnsupportedOperationException();
142     }
143
144     @Override
145     public final void clear() {
146         throw new UnsupportedOperationException();
147     }
148
149     @Override
150     public abstract int hashCode();
151
152     @Override
153     @SuppressWarnings("checkstyle:equalsHashCode")
154     public final boolean equals(final Object obj) {
155         if (obj == this) {
156             return true;
157         }
158         if (!(obj instanceof Set)) {
159             return false;
160         }
161
162         final Set<?> s = (Set<?>)obj;
163         return s.size() == 1 && otherContains(s);
164     }
165
166     private boolean otherContains(final @NonNull Collection<?> other) {
167         try {
168             return other.contains(getElement());
169         } catch (ClassCastException | NullPointerException e) {
170             return false;
171         }
172     }
173
174     @NonNullByDefault
175     private static final class RegularSingletonSet<E> extends SingletonSet<E> {
176         private static final long serialVersionUID = 1L;
177
178         private final E element;
179
180         RegularSingletonSet(final E element) {
181             this.element = requireNonNull(element);
182         }
183
184         @Override
185         @SuppressWarnings("checkstyle:parameterName")
186         public boolean contains(final @Nullable Object o) {
187             return element.equals(o);
188         }
189
190         @Override
191         public E getElement() {
192             return element;
193         }
194
195         @Override
196         @SuppressWarnings("checkstyle:equalsHashCode")
197         public int hashCode() {
198             return getElement().hashCode();
199         }
200
201         @Override
202         public String toString() {
203             return "[" + element + ']';
204         }
205
206         @Override
207         public Spliterator<E> spliterator() {
208             return SingletonSpliterators.immutableOf(element);
209         }
210     }
211 }