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