Introduce SharedSingletonMap
[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 final class SharedSingletonMap<K, V> implements Serializable, UnmodifiableMapPhase<K, V> {
28     private static final long serialVersionUID = 1L;
29     private static final LoadingCache<Object, SingletonSet<Object>> CACHE = CacheBuilder.newBuilder().weakValues()
30             .build(new CacheLoader<Object, SingletonSet<Object>>() {
31                 @Override
32                 public SingletonSet<Object> load(final Object key) {
33                     return SingletonSet.of(key);
34                 }
35             });
36     private final SingletonSet<K> keySet;
37     private final V value;
38     private int hashCode;
39
40     @SuppressWarnings("unchecked")
41     private SharedSingletonMap(final K key, final V value) {
42         this.keySet = (SingletonSet<K>) CACHE.getUnchecked(key);
43         this.value = Preconditions.checkNotNull(value);
44     }
45
46     public static <K, V> SharedSingletonMap<K, V> of(final K key, final V value) {
47         return new SharedSingletonMap<>(key, value);
48     }
49
50     public static <K, V> SharedSingletonMap<K, V> copyOf(final Map<K, V> m) {
51         Preconditions.checkArgument(m.size() == 1);
52
53         final Entry<K, V> e = m.entrySet().iterator().next();
54         return new SharedSingletonMap<>(e.getKey(), e.getValue());
55     }
56
57     @Override
58     public ModifiableMapPhase<K, V> toModifiableMap() {
59         return new MutableOffsetMap<K, V>(this);
60     }
61
62     @Override
63     public SingletonSet<Entry<K, V>> entrySet() {
64         return SingletonSet.<Entry<K, V>>of(new SimpleImmutableEntry<>(keySet.getElement(), value));
65     }
66
67     @Override
68     public SingletonSet<K> keySet() {
69         return keySet;
70     }
71
72     @Override
73     public SingletonSet<V> values() {
74         return SingletonSet.of(value);
75     }
76
77     @Override
78     public boolean containsKey(final Object key) {
79         return keySet.contains(key);
80     }
81
82     @Override
83     public boolean containsValue(final Object value) {
84         return this.value.equals(value);
85     }
86
87     @Override
88     public V get(final Object key) {
89         return keySet.contains(key) ? value : null;
90     }
91
92     @Override
93     public int size() {
94         return 1;
95     }
96
97     @Override
98     public boolean isEmpty() {
99         return false;
100     }
101
102     @Override
103     public V put(final K key, final V value) {
104         throw new UnsupportedOperationException();
105     }
106
107     @Override
108     public V remove(final Object key) {
109         throw new UnsupportedOperationException();
110     }
111
112     @Override
113     public void putAll(final Map<? extends K, ? extends V> m) {
114         throw new UnsupportedOperationException();
115     }
116
117     @Override
118     public void clear() {
119         throw new UnsupportedOperationException();
120     }
121
122     @Override
123     public int hashCode() {
124         if (hashCode == 0) {
125             hashCode = keySet.getElement().hashCode() ^ value.hashCode();
126         }
127         return hashCode;
128     }
129
130     @Override
131     public boolean equals(final Object obj) {
132         if (this == obj) {
133             return true;
134         }
135         if (!(obj instanceof Map)) {
136             return false;
137         }
138
139         final Map<?, ?> m = (Map<?, ?>)obj;
140         return m.size() == 1 && value.equals(m.get(keySet.getElement()));
141     }
142
143     @Override
144     public String toString() {
145         return "{" + keySet.getElement() + '=' + value + '}';
146     }
147 }