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 javax.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) {
40 public ModifiableMapPhase<K, V> toModifiableMap() {
41 return MutableOffsetMap.orderedCopyOf(this);
45 private static final class Unordered<K, V> extends SharedSingletonMap<K, V> {
46 private static final long serialVersionUID = 1L;
48 Unordered(final K key, final V value) {
54 public ModifiableMapPhase<K, V> toModifiableMap() {
55 return MutableOffsetMap.unorderedCopyOf(this);
59 private static final long serialVersionUID = 1L;
60 private static final LoadingCache<Object, SingletonSet<Object>> CACHE = CacheBuilder.newBuilder().weakValues()
61 .build(new CacheLoader<Object, SingletonSet<Object>>() {
63 public SingletonSet<Object> load(@Nonnull final Object key) {
64 return SingletonSet.of(key);
67 private final SingletonSet<K> keySet;
68 private final V value;
71 @SuppressWarnings("unchecked")
72 SharedSingletonMap(final K key, final V value) {
73 this.keySet = (SingletonSet<K>) CACHE.getUnchecked(key);
74 this.value = requireNonNull(value);
77 public static <K, V> SharedSingletonMap<K, V> orderedOf(final K key, final V value) {
78 return new Ordered<>(key, value);
81 public static <K, V> SharedSingletonMap<K, V> unorderedOf(final K key, final V value) {
82 return new Unordered<>(key, value);
85 public static <K, V> SharedSingletonMap<K, V> orderedCopyOf(final Map<K, V> map) {
86 checkArgument(map.size() == 1);
88 final Entry<K, V> e = map.entrySet().iterator().next();
89 return new Ordered<>(e.getKey(), e.getValue());
92 public static <K, V> SharedSingletonMap<K, V> unorderedCopyOf(final Map<K, V> map) {
93 checkArgument(map.size() == 1);
95 final Entry<K, V> e = map.entrySet().iterator().next();
96 return new Unordered<>(e.getKey(), e.getValue());
101 public final SingletonSet<Entry<K, V>> entrySet() {
102 return SingletonSet.of(new SimpleImmutableEntry<>(keySet.getElement(), value));
107 public final SingletonSet<K> keySet() {
113 public final SingletonSet<V> values() {
114 return SingletonSet.of(value);
118 public final boolean containsKey(final Object key) {
119 return keySet.contains(key);
123 @SuppressWarnings("checkstyle:hiddenField")
124 public final boolean containsValue(final Object value) {
125 return this.value.equals(value);
129 public final V get(final Object key) {
130 return keySet.contains(key) ? value : null;
134 public final int size() {
139 public final boolean isEmpty() {
144 @SuppressWarnings("checkstyle:hiddenField")
145 public final V put(final K key, final V value) {
146 throw new UnsupportedOperationException();
150 public final V remove(final Object key) {
151 throw new UnsupportedOperationException();
155 @SuppressWarnings("checkstyle:parameterName")
156 public final void putAll(@Nonnull final Map<? extends K, ? extends V> m) {
157 throw new UnsupportedOperationException();
161 public final void clear() {
162 throw new UnsupportedOperationException();
166 public final int hashCode() {
168 hashCode = keySet.getElement().hashCode() ^ value.hashCode();
174 public final boolean equals(final Object obj) {
178 if (!(obj instanceof Map)) {
182 final Map<?, ?> m = (Map<?, ?>)obj;
183 return m.size() == 1 && value.equals(m.get(keySet.getElement()));
187 public final String toString() {
188 return "{" + keySet.getElement() + '=' + value + '}';