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;
18 import javax.annotation.Nonnull;
21 * Implementation of the {@link Map} interface which stores a single mapping. The key set is shared among all instances
22 * which contain the same key. This implementation does not support null keys or values.
24 * @param <K> the type of keys maintained by this map
25 * @param <V> the type of mapped values
28 public abstract class SharedSingletonMap<K, V> implements Serializable, UnmodifiableMapPhase<K, V> {
29 private static final class Ordered<K, V> extends SharedSingletonMap<K, V> {
30 private static final long serialVersionUID = 1L;
32 Ordered(final K key, final V value) {
38 public ModifiableMapPhase<K, V> toModifiableMap() {
39 return MutableOffsetMap.orderedCopyOf(this);
43 private static final class Unordered<K, V> extends SharedSingletonMap<K, V> {
44 private static final long serialVersionUID = 1L;
46 Unordered(final K key, final V value) {
52 public ModifiableMapPhase<K, V> toModifiableMap() {
53 return MutableOffsetMap.unorderedCopyOf(this);
57 private static final long serialVersionUID = 1L;
58 private static final LoadingCache<Object, SingletonSet<Object>> CACHE = CacheBuilder.newBuilder().weakValues()
59 .build(new CacheLoader<Object, SingletonSet<Object>>() {
61 public SingletonSet<Object> load(@Nonnull final Object key) {
62 return SingletonSet.of(key);
65 private final SingletonSet<K> keySet;
66 private final V value;
69 @SuppressWarnings("unchecked")
70 SharedSingletonMap(final K key, final V value) {
71 this.keySet = (SingletonSet<K>) CACHE.getUnchecked(key);
72 this.value = Preconditions.checkNotNull(value);
75 public static <K, V> SharedSingletonMap<K, V> orderedOf(final K key, final V value) {
76 return new Ordered<>(key, value);
79 public static <K, V> SharedSingletonMap<K, V> unorderedOf(final K key, final V value) {
80 return new Unordered<>(key, value);
83 public static <K, V> SharedSingletonMap<K, V> orderedCopyOf(final Map<K, V> m) {
84 Preconditions.checkArgument(m.size() == 1);
86 final Entry<K, V> e = m.entrySet().iterator().next();
87 return new Ordered<>(e.getKey(), e.getValue());
90 public static <K, V> SharedSingletonMap<K, V> unorderedCopyOf(final Map<K, V> m) {
91 Preconditions.checkArgument(m.size() == 1);
93 final Entry<K, V> e = m.entrySet().iterator().next();
94 return new Unordered<>(e.getKey(), e.getValue());
99 public final SingletonSet<Entry<K, V>> entrySet() {
100 return SingletonSet.of(new SimpleImmutableEntry<>(keySet.getElement(), value));
105 public final SingletonSet<K> keySet() {
111 public final SingletonSet<V> values() {
112 return SingletonSet.of(value);
116 public final boolean containsKey(final Object key) {
117 return keySet.contains(key);
121 public final boolean containsValue(final Object value) {
122 return this.value.equals(value);
126 public final V get(final Object key) {
127 return keySet.contains(key) ? value : null;
131 public final int size() {
136 public final boolean isEmpty() {
141 public final V put(final K key, final V value) {
142 throw new UnsupportedOperationException();
146 public final V remove(final Object key) {
147 throw new UnsupportedOperationException();
151 public final void putAll(@Nonnull final Map<? extends K, ? extends V> m) {
152 throw new UnsupportedOperationException();
156 public final void clear() {
157 throw new UnsupportedOperationException();
161 public final int hashCode() {
163 hashCode = keySet.getElement().hashCode() ^ value.hashCode();
169 public final boolean equals(final Object obj) {
173 if (!(obj instanceof Map)) {
177 final Map<?, ?> m = (Map<?, ?>)obj;
178 return m.size() == 1 && value.equals(m.get(keySet.getElement()));
182 public final String toString() {
183 return "{" + keySet.getElement() + '=' + value + '}';