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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.CacheLoader;
16 import com.google.common.cache.LoadingCache;
17 import java.io.Serializable;
18 import java.util.AbstractMap.SimpleImmutableEntry;
20 import org.eclipse.jdt.annotation.NonNull;
23 * Implementation of the {@link Map} interface which stores a single mapping. The key set is shared among all instances
24 * which contain the same key. This implementation does not support null keys or values.
26 * @param <K> the type of keys maintained by this map
27 * @param <V> the type of mapped values
30 public abstract class SharedSingletonMap<K, V> implements Serializable, UnmodifiableMapPhase<K, V> {
31 private static final class Ordered<K, V> extends SharedSingletonMap<K, V> {
32 private static final long serialVersionUID = 1L;
34 Ordered(final K key, final V value) {
39 public @NonNull ModifiableMapPhase<K, V> toModifiableMap() {
40 return MutableOffsetMap.orderedCopyOf(this);
44 private static final class Unordered<K, V> extends SharedSingletonMap<K, V> {
45 private static final long serialVersionUID = 1L;
47 Unordered(final K key, final V value) {
52 public @NonNull 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(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 = requireNonNull(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> map) {
84 checkArgument(map.size() == 1);
86 final Entry<K, V> e = map.entrySet().iterator().next();
87 return new Ordered<>(e.getKey(), e.getValue());
90 public static <K, V> SharedSingletonMap<K, V> unorderedCopyOf(final Map<K, V> map) {
91 checkArgument(map.size() == 1);
93 final Entry<K, V> e = map.entrySet().iterator().next();
94 return new Unordered<>(e.getKey(), e.getValue());
98 public final @NonNull SingletonSet<Entry<K, V>> entrySet() {
99 return SingletonSet.of(new SimpleImmutableEntry<>(keySet.getElement(), value));
103 public final @NonNull SingletonSet<K> keySet() {
108 public final @NonNull SingletonSet<V> values() {
109 return SingletonSet.of(value);
113 public final boolean containsKey(final Object key) {
114 return keySet.contains(key);
118 @SuppressWarnings("checkstyle:hiddenField")
119 public final boolean containsValue(final Object value) {
120 return this.value.equals(value);
124 public final V get(final Object key) {
125 return keySet.contains(key) ? value : null;
129 public final int size() {
134 public final boolean isEmpty() {
139 @SuppressWarnings("checkstyle:hiddenField")
140 public final V put(final K key, final V value) {
141 throw new UnsupportedOperationException();
145 public final V remove(final Object key) {
146 throw new UnsupportedOperationException();
150 @SuppressWarnings("checkstyle:parameterName")
151 public final void putAll(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 + '}';