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