cb3dbe8df02f68055c2e7d0a534b4b83a6e0037e
[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 import javax.annotation.Nonnull;
19
20 /**
21  * Implementation of the {@link Map} interface which stores a single mapping. The key set is shared among all instances
22  * which contain the same key. This implementation does not support null keys or values.
23  *
24  * @param <K> the type of keys maintained by this map
25  * @param <V> the type of mapped values
26  */
27 @Beta
28 public abstract class SharedSingletonMap<K, V> implements Serializable, UnmodifiableMapPhase<K, V> {
29     private static final class Ordered<K, V> extends SharedSingletonMap<K, V> {
30         private static final long serialVersionUID = 1L;
31
32         Ordered(final K key, final V value) {
33             super(key, value);
34         }
35
36         @Nonnull
37         @Override
38         public ModifiableMapPhase<K, V> toModifiableMap() {
39             return MutableOffsetMap.orderedCopyOf(this);
40         }
41     }
42
43     private static final class Unordered<K, V> extends SharedSingletonMap<K, V> {
44         private static final long serialVersionUID = 1L;
45
46         Unordered(final K key, final V value) {
47             super(key, value);
48         }
49
50         @Nonnull
51         @Override
52         public 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(@Nonnull 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 = Preconditions.checkNotNull(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> m) {
84         Preconditions.checkArgument(m.size() == 1);
85
86         final Entry<K, V> e = m.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> m) {
91         Preconditions.checkArgument(m.size() == 1);
92
93         final Entry<K, V> e = m.entrySet().iterator().next();
94         return new Unordered<>(e.getKey(), e.getValue());
95     }
96
97     @Nonnull
98     @Override
99     public final SingletonSet<Entry<K, V>> entrySet() {
100         return SingletonSet.of(new SimpleImmutableEntry<>(keySet.getElement(), value));
101     }
102
103     @Nonnull
104     @Override
105     public final SingletonSet<K> keySet() {
106         return keySet;
107     }
108
109     @Nonnull
110     @Override
111     public final SingletonSet<V> values() {
112         return SingletonSet.of(value);
113     }
114
115     @Override
116     public final boolean containsKey(final Object key) {
117         return keySet.contains(key);
118     }
119
120     @Override
121     public final boolean containsValue(final Object value) {
122         return this.value.equals(value);
123     }
124
125     @Override
126     public final V get(final Object key) {
127         return keySet.contains(key) ? value : null;
128     }
129
130     @Override
131     public final int size() {
132         return 1;
133     }
134
135     @Override
136     public final boolean isEmpty() {
137         return false;
138     }
139
140     @Override
141     public final V put(final K key, final V value) {
142         throw new UnsupportedOperationException();
143     }
144
145     @Override
146     public final V remove(final Object key) {
147         throw new UnsupportedOperationException();
148     }
149
150     @Override
151     public final void putAll(@Nonnull 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 }