Migrate common/util to use JDT annotations
[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.Map;
20 import org.eclipse.jdt.annotation.NonNull;
21
22 /**
23  * Implementation of the {@link Map} interface which stores a single mapping. The key set is shared among all instances
24  * which contain the same key. This implementation does not support null keys or values.
25  *
26  * @param <K> the type of keys maintained by this map
27  * @param <V> the type of mapped values
28  */
29 @Beta
30 public abstract class SharedSingletonMap<K, V> implements Serializable, UnmodifiableMapPhase<K, V> {
31     private static final class Ordered<K, V> extends SharedSingletonMap<K, V> {
32         private static final long serialVersionUID = 1L;
33
34         Ordered(final K key, final V value) {
35             super(key, value);
36         }
37
38         @Override
39         public @NonNull ModifiableMapPhase<K, V> toModifiableMap() {
40             return MutableOffsetMap.orderedCopyOf(this);
41         }
42     }
43
44     private static final class Unordered<K, V> extends SharedSingletonMap<K, V> {
45         private static final long serialVersionUID = 1L;
46
47         Unordered(final K key, final V value) {
48             super(key, value);
49         }
50
51         @Override
52         public @NonNull ModifiableMapPhase<K, V> toModifiableMap() {
53             return MutableOffsetMap.unorderedCopyOf(this);
54         }
55     }
56
57     private static final long serialVersionUID = 1L;
58     private static final LoadingCache<Object, SingletonSet<Object>> CACHE = CacheBuilder.newBuilder().weakValues()
59             .build(new CacheLoader<Object, SingletonSet<Object>>() {
60                 @Override
61                 public SingletonSet<Object> load(final Object key) {
62                     return SingletonSet.of(key);
63                 }
64             });
65     private final SingletonSet<K> keySet;
66     private final V value;
67     private int hashCode;
68
69     @SuppressWarnings("unchecked")
70     SharedSingletonMap(final K key, final V value) {
71         this.keySet = (SingletonSet<K>) CACHE.getUnchecked(key);
72         this.value = requireNonNull(value);
73     }
74
75     public static <K, V> SharedSingletonMap<K, V> orderedOf(final K key, final V value) {
76         return new Ordered<>(key, value);
77     }
78
79     public static <K, V> SharedSingletonMap<K, V> unorderedOf(final K key, final V value) {
80         return new Unordered<>(key, value);
81     }
82
83     public static <K, V> SharedSingletonMap<K, V> orderedCopyOf(final Map<K, V> map) {
84         checkArgument(map.size() == 1);
85
86         final Entry<K, V> e = map.entrySet().iterator().next();
87         return new Ordered<>(e.getKey(), e.getValue());
88     }
89
90     public static <K, V> SharedSingletonMap<K, V> unorderedCopyOf(final Map<K, V> map) {
91         checkArgument(map.size() == 1);
92
93         final Entry<K, V> e = map.entrySet().iterator().next();
94         return new Unordered<>(e.getKey(), e.getValue());
95     }
96
97     @Override
98     public final @NonNull SingletonSet<Entry<K, V>> entrySet() {
99         return SingletonSet.of(new SimpleImmutableEntry<>(keySet.getElement(), value));
100     }
101
102     @Override
103     public final @NonNull SingletonSet<K> keySet() {
104         return keySet;
105     }
106
107     @Override
108     public final @NonNull SingletonSet<V> values() {
109         return SingletonSet.of(value);
110     }
111
112     @Override
113     public final boolean containsKey(final Object key) {
114         return keySet.contains(key);
115     }
116
117     @Override
118     @SuppressWarnings("checkstyle:hiddenField")
119     public final boolean containsValue(final Object value) {
120         return this.value.equals(value);
121     }
122
123     @Override
124     public final V get(final Object key) {
125         return keySet.contains(key) ? value : null;
126     }
127
128     @Override
129     public final int size() {
130         return 1;
131     }
132
133     @Override
134     public final boolean isEmpty() {
135         return false;
136     }
137
138     @Override
139     @SuppressWarnings("checkstyle:hiddenField")
140     public final V put(final K key, final V value) {
141         throw new UnsupportedOperationException();
142     }
143
144     @Override
145     public final V remove(final Object key) {
146         throw new UnsupportedOperationException();
147     }
148
149     @Override
150     @SuppressWarnings("checkstyle:parameterName")
151     public final void putAll(final Map<? extends K, ? extends V> m) {
152         throw new UnsupportedOperationException();
153     }
154
155     @Override
156     public final void clear() {
157         throw new UnsupportedOperationException();
158     }
159
160     @Override
161     public final int hashCode() {
162         if (hashCode == 0) {
163             hashCode = keySet.getElement().hashCode() ^ value.hashCode();
164         }
165         return hashCode;
166     }
167
168     @Override
169     public final boolean equals(final Object obj) {
170         if (this == obj) {
171             return true;
172         }
173         if (!(obj instanceof Map)) {
174             return false;
175         }
176
177         final Map<?, ?> m = (Map<?, ?>)obj;
178         return m.size() == 1 && value.equals(m.get(keySet.getElement()));
179     }
180
181     @Override
182     public final String toString() {
183         return "{" + keySet.getElement() + '=' + value + '}';
184     }
185 }