2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util;
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;
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.
23 * @param <K> the type of keys maintained by this map
24 * @param <V> the type of mapped values
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>>() {
32 public SingletonSet<Object> load(final Object key) {
33 return SingletonSet.of(key);
36 private final SingletonSet<K> keySet;
37 private final V value;
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);
46 public static <K, V> SharedSingletonMap<K, V> of(final K key, final V value) {
47 return new SharedSingletonMap<>(key, value);
50 public static <K, V> SharedSingletonMap<K, V> copyOf(final Map<K, V> m) {
51 Preconditions.checkArgument(m.size() == 1);
53 final Entry<K, V> e = m.entrySet().iterator().next();
54 return new SharedSingletonMap<>(e.getKey(), e.getValue());
58 public ModifiableMapPhase<K, V> toModifiableMap() {
59 return new MutableOffsetMap<K, V>(this);
63 public SingletonSet<Entry<K, V>> entrySet() {
64 return SingletonSet.<Entry<K, V>>of(new SimpleImmutableEntry<>(keySet.getElement(), value));
68 public SingletonSet<K> keySet() {
73 public SingletonSet<V> values() {
74 return SingletonSet.of(value);
78 public boolean containsKey(final Object key) {
79 return keySet.contains(key);
83 public boolean containsValue(final Object value) {
84 return this.value.equals(value);
88 public V get(final Object key) {
89 return keySet.contains(key) ? value : null;
98 public boolean isEmpty() {
103 public V put(final K key, final V value) {
104 throw new UnsupportedOperationException();
108 public V remove(final Object key) {
109 throw new UnsupportedOperationException();
113 public void putAll(final Map<? extends K, ? extends V> m) {
114 throw new UnsupportedOperationException();
118 public void clear() {
119 throw new UnsupportedOperationException();
123 public int hashCode() {
125 hashCode = keySet.getElement().hashCode() ^ value.hashCode();
131 public boolean equals(final Object obj) {
135 if (!(obj instanceof Map)) {
139 final Map<?, ?> m = (Map<?, ?>)obj;
140 return m.size() == 1 && value.equals(m.get(keySet.getElement()));
144 public String toString() {
145 return "{" + keySet.getElement() + '=' + value + '}';