Add ImmutableMapTemplate
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / SharedSingletonMap.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 com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.CacheLoader;
16 import com.google.common.cache.LoadingCache;
17 import java.io.Serializable;
18 import java.util.AbstractMap.SimpleImmutableEntry;
19 import java.util.Iterator;
20 import java.util.Map;
21 import org.eclipse.jdt.annotation.NonNull;
22
23 /**
24  * Implementation of the {@link Map} interface which stores a single mapping. The key set is shared among all instances
25  * which contain the same key. This implementation does not support null keys or values.
26  *
27  * @param <K> the type of keys maintained by this map
28  * @param <V> the type of mapped values
29  */
30 @Beta
31 public abstract class SharedSingletonMap<K, V> implements Serializable, UnmodifiableMapPhase<K, V> {
32     static final class Ordered<K, V> extends SharedSingletonMap<K, V> {
33         private static final long serialVersionUID = 1L;
34
35         Ordered(final K key, final V value) {
36             super(key, value);
37         }
38
39         Ordered(final SingletonSet<K> keySet, final V value) {
40             super(keySet, value);
41         }
42
43         @Override
44         public ModifiableMapPhase<K, V> toModifiableMap() {
45             return MutableOffsetMap.orderedCopyOf(this);
46         }
47     }
48
49     static final class Unordered<K, V> extends SharedSingletonMap<K, V> {
50         private static final long serialVersionUID = 1L;
51
52         Unordered(final K key, final V value) {
53             super(key, value);
54         }
55
56         Unordered(final SingletonSet<K> keySet, final V value) {
57             super(keySet, value);
58         }
59
60         @Override
61         public ModifiableMapPhase<K, V> toModifiableMap() {
62             return MutableOffsetMap.unorderedCopyOf(this);
63         }
64     }
65
66     private static final long serialVersionUID = 1L;
67     private static final LoadingCache<Object, SingletonSet<Object>> CACHE = CacheBuilder.newBuilder().weakValues()
68             .build(new CacheLoader<Object, SingletonSet<Object>>() {
69                 @Override
70                 public SingletonSet<Object> load(final Object key) {
71                     return SingletonSet.of(key);
72                 }
73             });
74
75     private final @NonNull SingletonSet<K> keySet;
76     private final @NonNull V value;
77     private int hashCode;
78
79     SharedSingletonMap(final SingletonSet<K> keySet, final V value) {
80         this.keySet = requireNonNull(keySet);
81         this.value = requireNonNull(value);
82     }
83
84     SharedSingletonMap(final K key, final V value) {
85         this(cachedSet(key), value);
86     }
87
88     /**
89      * Create a {@link SharedSingletonMap} of specified {@code key} and {@code value}, which retains insertion order
90      * when transformed via {@link #toModifiableMap()}.
91      *
92      * @param key key
93      * @param value value
94      * @return A SharedSingletonMap
95      * @throws NullPointerException if any of the arguments is null
96      */
97     public static <K, V> @NonNull SharedSingletonMap<K, V> orderedOf(final K key, final V value) {
98         return new Ordered<>(key, value);
99     }
100
101     /**
102      * Create a {@link SharedSingletonMap} of specified {@code key} and {@code value}, which does not retain insertion
103      * order when transformed via {@link #toModifiableMap()}.
104      *
105      * @param key key
106      * @param value value
107      * @return A SharedSingletonMap
108      * @throws NullPointerException if any of the arguments is null
109      */
110     public static <K, V> @NonNull SharedSingletonMap<K, V> unorderedOf(final K key, final V value) {
111         return new Unordered<>(key, value);
112     }
113
114     /**
115      * Create a {@link SharedSingletonMap} of specified {@code key} and {@code value}, which retains insertion order
116      * when transformed via {@link #toModifiableMap()}.
117      *
118      * @param map input map
119      * @return A SharedSingletonMap
120      * @throws NullPointerException if {@code map} is null
121      * @throws IllegalArgumentException if {@code map} does not have exactly one entry
122      */
123     public static <K, V> @NonNull SharedSingletonMap<K, V> orderedCopyOf(final Map<K, V> map) {
124         final Entry<K, V> e = singleEntry(map);
125         return new Ordered<>(e.getKey(), e.getValue());
126     }
127
128     /**
129      * Create a {@link SharedSingletonMap} from specified single-element map, which does not retain insertion order when
130      * transformed via {@link #toModifiableMap()}.
131      *
132      * @param map input map
133      * @return A SharedSingletonMap
134      * @throws NullPointerException if {@code map} is null
135      * @throws IllegalArgumentException if {@code map} does not have exactly one entry
136      */
137     public static <K, V> @NonNull SharedSingletonMap<K, V> unorderedCopyOf(final Map<K, V> map) {
138         final Entry<K, V> e = singleEntry(map);
139         return new Unordered<>(e.getKey(), e.getValue());
140     }
141
142     @Override
143     public final SingletonSet<Entry<K, V>> entrySet() {
144         return SingletonSet.of(new SimpleImmutableEntry<>(keySet.getElement(), value));
145     }
146
147     @Override
148     public final SingletonSet<K> keySet() {
149         return keySet;
150     }
151
152     @Override
153     public final SingletonSet<V> values() {
154         return SingletonSet.of(value);
155     }
156
157     @Override
158     public final boolean containsKey(final Object key) {
159         return keySet.contains(key);
160     }
161
162     @Override
163     @SuppressWarnings("checkstyle:hiddenField")
164     public final boolean containsValue(final Object value) {
165         return this.value.equals(value);
166     }
167
168     @Override
169     public final V get(final Object key) {
170         return keySet.contains(key) ? value : null;
171     }
172
173     @Override
174     public final int size() {
175         return 1;
176     }
177
178     @Override
179     public final boolean isEmpty() {
180         return false;
181     }
182
183     @Override
184     @SuppressWarnings("checkstyle:hiddenField")
185     public final V put(final K key, final V value) {
186         throw new UnsupportedOperationException();
187     }
188
189     @Override
190     public final V remove(final Object key) {
191         throw new UnsupportedOperationException();
192     }
193
194     @Override
195     @SuppressWarnings("checkstyle:parameterName")
196     public final void putAll(final Map<? extends K, ? extends V> m) {
197         throw new UnsupportedOperationException();
198     }
199
200     @Override
201     public final void clear() {
202         throw new UnsupportedOperationException();
203     }
204
205     @Override
206     public final int hashCode() {
207         if (hashCode == 0) {
208             hashCode = keySet.getElement().hashCode() ^ value.hashCode();
209         }
210         return hashCode;
211     }
212
213     @Override
214     public final boolean equals(final Object obj) {
215         if (this == obj) {
216             return true;
217         }
218         if (!(obj instanceof Map)) {
219             return false;
220         }
221
222         final Map<?, ?> m = (Map<?, ?>)obj;
223         return m.size() == 1 && value.equals(m.get(keySet.getElement()));
224     }
225
226     @Override
227     public final String toString() {
228         return "{" + keySet.getElement() + '=' + value + '}';
229     }
230
231     @SuppressWarnings("unchecked")
232     static <K> @NonNull SingletonSet<K> cachedSet(final K key) {
233         return (SingletonSet<K>) CACHE.getUnchecked(key);
234     }
235
236     private static <K, V> Entry<K, V> singleEntry(final Map<K, V> map) {
237         final Iterator<Entry<K, V>> it = map.entrySet().iterator();
238         checkArgument(it.hasNext(), "Input map is empty");
239         final Entry<K, V> ret = it.next();
240         checkArgument(!it.hasNext(), "Input map has more than one entry");
241         return ret;
242     }
243 }