8e36661c3ac61b146d64985076cb417d73cad798
[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 javax.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         @Nonnull
39         @Override
40         public ModifiableMapPhase<K, V> toModifiableMap() {
41             return MutableOffsetMap.orderedCopyOf(this);
42         }
43     }
44
45     private static final class Unordered<K, V> extends SharedSingletonMap<K, V> {
46         private static final long serialVersionUID = 1L;
47
48         Unordered(final K key, final V value) {
49             super(key, value);
50         }
51
52         @Nonnull
53         @Override
54         public ModifiableMapPhase<K, V> toModifiableMap() {
55             return MutableOffsetMap.unorderedCopyOf(this);
56         }
57     }
58
59     private static final long serialVersionUID = 1L;
60     private static final LoadingCache<Object, SingletonSet<Object>> CACHE = CacheBuilder.newBuilder().weakValues()
61             .build(new CacheLoader<Object, SingletonSet<Object>>() {
62                 @Override
63                 public SingletonSet<Object> load(@Nonnull final Object key) {
64                     return SingletonSet.of(key);
65                 }
66             });
67     private final SingletonSet<K> keySet;
68     private final V value;
69     private int hashCode;
70
71     @SuppressWarnings("unchecked")
72     SharedSingletonMap(final K key, final V value) {
73         this.keySet = (SingletonSet<K>) CACHE.getUnchecked(key);
74         this.value = requireNonNull(value);
75     }
76
77     public static <K, V> SharedSingletonMap<K, V> orderedOf(final K key, final V value) {
78         return new Ordered<>(key, value);
79     }
80
81     public static <K, V> SharedSingletonMap<K, V> unorderedOf(final K key, final V value) {
82         return new Unordered<>(key, value);
83     }
84
85     public static <K, V> SharedSingletonMap<K, V> orderedCopyOf(final Map<K, V> map) {
86         checkArgument(map.size() == 1);
87
88         final Entry<K, V> e = map.entrySet().iterator().next();
89         return new Ordered<>(e.getKey(), e.getValue());
90     }
91
92     public static <K, V> SharedSingletonMap<K, V> unorderedCopyOf(final Map<K, V> map) {
93         checkArgument(map.size() == 1);
94
95         final Entry<K, V> e = map.entrySet().iterator().next();
96         return new Unordered<>(e.getKey(), e.getValue());
97     }
98
99     @Nonnull
100     @Override
101     public final SingletonSet<Entry<K, V>> entrySet() {
102         return SingletonSet.of(new SimpleImmutableEntry<>(keySet.getElement(), value));
103     }
104
105     @Nonnull
106     @Override
107     public final SingletonSet<K> keySet() {
108         return keySet;
109     }
110
111     @Nonnull
112     @Override
113     public final SingletonSet<V> values() {
114         return SingletonSet.of(value);
115     }
116
117     @Override
118     public final boolean containsKey(final Object key) {
119         return keySet.contains(key);
120     }
121
122     @Override
123     @SuppressWarnings("checkstyle:hiddenField")
124     public final boolean containsValue(final Object value) {
125         return this.value.equals(value);
126     }
127
128     @Override
129     public final V get(final Object key) {
130         return keySet.contains(key) ? value : null;
131     }
132
133     @Override
134     public final int size() {
135         return 1;
136     }
137
138     @Override
139     public final boolean isEmpty() {
140         return false;
141     }
142
143     @Override
144     @SuppressWarnings("checkstyle:hiddenField")
145     public final V put(final K key, final V value) {
146         throw new UnsupportedOperationException();
147     }
148
149     @Override
150     public final V remove(final Object key) {
151         throw new UnsupportedOperationException();
152     }
153
154     @Override
155     @SuppressWarnings("checkstyle:parameterName")
156     public final void putAll(@Nonnull final Map<? extends K, ? extends V> m) {
157         throw new UnsupportedOperationException();
158     }
159
160     @Override
161     public final void clear() {
162         throw new UnsupportedOperationException();
163     }
164
165     @Override
166     public final int hashCode() {
167         if (hashCode == 0) {
168             hashCode = keySet.getElement().hashCode() ^ value.hashCode();
169         }
170         return hashCode;
171     }
172
173     @Override
174     public final boolean equals(final Object obj) {
175         if (this == obj) {
176             return true;
177         }
178         if (!(obj instanceof Map)) {
179             return false;
180         }
181
182         final Map<?, ?> m = (Map<?, ?>)obj;
183         return m.size() == 1 && value.equals(m.get(keySet.getElement()));
184     }
185
186     @Override
187     public final String toString() {
188         return "{" + keySet.getElement() + '=' + value + '}';
189     }
190 }