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 MutableOffsetMap.orderedCopyOf(this);
41 private static final class Unordered<K, V> extends SharedSingletonMap<K, V> {
42 private static final long serialVersionUID = 1L;
44 Unordered(final K key, final V value) {
49 public ModifiableMapPhase<K, V> toModifiableMap() {
50 return MutableOffsetMap.unorderedCopyOf(this);
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>>() {
58 public SingletonSet<Object> load(final Object key) {
59 return SingletonSet.of(key);
62 private final SingletonSet<K> keySet;
63 private final V value;
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);
73 * @deprecated Use {@link #orderedOf(Object, Object)} or {@link #unorderedOf(Object, Object)} instead.
76 public static <K, V> SharedSingletonMap<K, V> of(final K key, final V value) {
77 return new Ordered<>(key, value);
80 public static <K, V> SharedSingletonMap<K, V> orderedOf(final K key, final V value) {
81 return new Ordered<>(key, value);
84 public static <K, V> SharedSingletonMap<K, V> unorderedOf(final K key, final V value) {
85 return new Unordered<>(key, value);
89 * @deprecated Use {@link #orderedCopyOf(Map)} or {@link #unorderedCopyOf(Map)} instead.
92 public static <K, V> SharedSingletonMap<K, V> copyOf(final Map<K, V> m) {
93 return orderedCopyOf(m);
96 public static <K, V> SharedSingletonMap<K, V> orderedCopyOf(final Map<K, V> m) {
97 Preconditions.checkArgument(m.size() == 1);
99 final Entry<K, V> e = m.entrySet().iterator().next();
100 return new Ordered<>(e.getKey(), e.getValue());
103 public static <K, V> SharedSingletonMap<K, V> unorderedCopyOf(final Map<K, V> m) {
104 Preconditions.checkArgument(m.size() == 1);
106 final Entry<K, V> e = m.entrySet().iterator().next();
107 return new Unordered<>(e.getKey(), e.getValue());
111 public final SingletonSet<Entry<K, V>> entrySet() {
112 return SingletonSet.<Entry<K, V>>of(new SimpleImmutableEntry<>(keySet.getElement(), value));
116 public final SingletonSet<K> keySet() {
121 public final SingletonSet<V> values() {
122 return SingletonSet.of(value);
126 public final boolean containsKey(final Object key) {
127 return keySet.contains(key);
131 public final boolean containsValue(final Object value) {
132 return this.value.equals(value);
136 public final V get(final Object key) {
137 return keySet.contains(key) ? value : null;
141 public final int size() {
146 public final boolean isEmpty() {
151 public final V put(final K key, final V value) {
152 throw new UnsupportedOperationException();
156 public final V remove(final Object key) {
157 throw new UnsupportedOperationException();
161 public final void putAll(final Map<? extends K, ? extends V> m) {
162 throw new UnsupportedOperationException();
166 public final void clear() {
167 throw new UnsupportedOperationException();
171 public final int hashCode() {
173 hashCode = keySet.getElement().hashCode() ^ value.hashCode();
179 public final boolean equals(final Object obj) {
183 if (!(obj instanceof Map)) {
187 final Map<?, ?> m = (Map<?, ?>)obj;
188 return m.size() == 1 && value.equals(m.get(keySet.getElement()));
192 public final String toString() {
193 return "{" + keySet.getElement() + '=' + value + '}';