Drop unneeded generic type specifiers
[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     public static <K, V> SharedSingletonMap<K, V> orderedOf(final K key, final V value) {
73         return new Ordered<>(key, value);
74     }
75
76     public static <K, V> SharedSingletonMap<K, V> unorderedOf(final K key, final V value) {
77         return new Unordered<>(key, value);
78     }
79
80     public static <K, V> SharedSingletonMap<K, V> orderedCopyOf(final Map<K, V> m) {
81         Preconditions.checkArgument(m.size() == 1);
82
83         final Entry<K, V> e = m.entrySet().iterator().next();
84         return new Ordered<>(e.getKey(), e.getValue());
85     }
86
87     public static <K, V> SharedSingletonMap<K, V> unorderedCopyOf(final Map<K, V> m) {
88         Preconditions.checkArgument(m.size() == 1);
89
90         final Entry<K, V> e = m.entrySet().iterator().next();
91         return new Unordered<>(e.getKey(), e.getValue());
92     }
93
94     @Override
95     public final SingletonSet<Entry<K, V>> entrySet() {
96         return SingletonSet.of(new SimpleImmutableEntry<>(keySet.getElement(), value));
97     }
98
99     @Override
100     public final SingletonSet<K> keySet() {
101         return keySet;
102     }
103
104     @Override
105     public final SingletonSet<V> values() {
106         return SingletonSet.of(value);
107     }
108
109     @Override
110     public final boolean containsKey(final Object key) {
111         return keySet.contains(key);
112     }
113
114     @Override
115     public final boolean containsValue(final Object value) {
116         return this.value.equals(value);
117     }
118
119     @Override
120     public final V get(final Object key) {
121         return keySet.contains(key) ? value : null;
122     }
123
124     @Override
125     public final int size() {
126         return 1;
127     }
128
129     @Override
130     public final boolean isEmpty() {
131         return false;
132     }
133
134     @Override
135     public final V put(final K key, final V value) {
136         throw new UnsupportedOperationException();
137     }
138
139     @Override
140     public final V remove(final Object key) {
141         throw new UnsupportedOperationException();
142     }
143
144     @Override
145     public final void putAll(final Map<? extends K, ? extends V> m) {
146         throw new UnsupportedOperationException();
147     }
148
149     @Override
150     public final void clear() {
151         throw new UnsupportedOperationException();
152     }
153
154     @Override
155     public final int hashCode() {
156         if (hashCode == 0) {
157             hashCode = keySet.getElement().hashCode() ^ value.hashCode();
158         }
159         return hashCode;
160     }
161
162     @Override
163     public final boolean equals(final Object obj) {
164         if (this == obj) {
165             return true;
166         }
167         if (!(obj instanceof Map)) {
168             return false;
169         }
170
171         final Map<?, ?> m = (Map<?, ?>)obj;
172         return m.size() == 1 && value.equals(m.get(keySet.getElement()));
173     }
174
175     @Override
176     public final String toString() {
177         return "{" + keySet.getElement() + '=' + value + '}';
178     }
179 }