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 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;
31 Ordered(final K key, final V value) {
36 public ModifiableMapPhase<K, V> toModifiableMap() {
37 return new MutableOffsetMap<K, V>(this);
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>>() {
45 public SingletonSet<Object> load(final Object key) {
46 return SingletonSet.of(key);
49 private final SingletonSet<K> keySet;
50 private final V value;
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);
59 public static <K, V> SharedSingletonMap<K, V> of(final K key, final V value) {
60 return new Ordered<>(key, value);
63 public static <K, V> SharedSingletonMap<K, V> copyOf(final Map<K, V> m) {
64 Preconditions.checkArgument(m.size() == 1);
66 final Entry<K, V> e = m.entrySet().iterator().next();
67 return new Ordered<>(e.getKey(), e.getValue());
71 public final SingletonSet<Entry<K, V>> entrySet() {
72 return SingletonSet.<Entry<K, V>>of(new SimpleImmutableEntry<>(keySet.getElement(), value));
76 public final SingletonSet<K> keySet() {
81 public final SingletonSet<V> values() {
82 return SingletonSet.of(value);
86 public final boolean containsKey(final Object key) {
87 return keySet.contains(key);
91 public final boolean containsValue(final Object value) {
92 return this.value.equals(value);
96 public final V get(final Object key) {
97 return keySet.contains(key) ? value : null;
101 public final int size() {
106 public final boolean isEmpty() {
111 public final V put(final K key, final V value) {
112 throw new UnsupportedOperationException();
116 public final V remove(final Object key) {
117 throw new UnsupportedOperationException();
121 public final void putAll(final Map<? extends K, ? extends V> m) {
122 throw new UnsupportedOperationException();
126 public final void clear() {
127 throw new UnsupportedOperationException();
131 public final int hashCode() {
133 hashCode = keySet.getElement().hashCode() ^ value.hashCode();
139 public final boolean equals(final Object obj) {
143 if (!(obj instanceof Map)) {
147 final Map<?, ?> m = (Map<?, ?>)obj;
148 return m.size() == 1 && value.equals(m.get(keySet.getElement()));
152 public final String toString() {
153 return "{" + keySet.getElement() + '=' + value + '}';