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;
19 import java.util.Iterator;
21 import org.eclipse.jdt.annotation.NonNull;
24 * Implementation of the {@link Map} interface which stores a single mapping. The key set is shared among all instances
25 * which contain the same key. This implementation does not support null keys or values.
28 * In case the set of keys is statically known, you can use {@link SharedSingletonMapTemplate} to efficiently create
29 * {@link SharedSingletonMap} instances.
31 * @param <K> the type of keys maintained by this map
32 * @param <V> the type of mapped values
35 public abstract class SharedSingletonMap<K, V> implements Serializable, UnmodifiableMapPhase<K, V> {
36 static final class Ordered<K, V> extends SharedSingletonMap<K, V> {
37 private static final long serialVersionUID = 1L;
39 Ordered(final K key, final V value) {
43 Ordered(final SingletonSet<K> keySet, final V value) {
48 public ModifiableMapPhase<K, V> toModifiableMap() {
49 return MutableOffsetMap.orderedCopyOf(this);
53 static final class Unordered<K, V> extends SharedSingletonMap<K, V> {
54 private static final long serialVersionUID = 1L;
56 Unordered(final K key, final V value) {
60 Unordered(final SingletonSet<K> keySet, final V value) {
65 public ModifiableMapPhase<K, V> toModifiableMap() {
66 return MutableOffsetMap.unorderedCopyOf(this);
70 private static final long serialVersionUID = 1L;
71 private static final LoadingCache<Object, SingletonSet<Object>> CACHE = CacheBuilder.newBuilder().weakValues()
72 .build(new CacheLoader<Object, SingletonSet<Object>>() {
74 public SingletonSet<Object> load(final Object key) {
75 return SingletonSet.of(key);
79 private final @NonNull SingletonSet<K> keySet;
80 private final @NonNull V value;
83 SharedSingletonMap(final SingletonSet<K> keySet, final V value) {
84 this.keySet = requireNonNull(keySet);
85 this.value = requireNonNull(value);
88 SharedSingletonMap(final K key, final V value) {
89 this(cachedSet(key), value);
93 * Create a {@link SharedSingletonMap} of specified {@code key} and {@code value}, which retains insertion order
94 * when transformed via {@link #toModifiableMap()}.
98 * @return A SharedSingletonMap
99 * @throws NullPointerException if any of the arguments is null
101 public static <K, V> @NonNull SharedSingletonMap<K, V> orderedOf(final K key, final V value) {
102 return new Ordered<>(key, value);
106 * Create a {@link SharedSingletonMap} of specified {@code key} and {@code value}, which does not retain insertion
107 * order when transformed via {@link #toModifiableMap()}.
111 * @return A SharedSingletonMap
112 * @throws NullPointerException if any of the arguments is null
114 public static <K, V> @NonNull SharedSingletonMap<K, V> unorderedOf(final K key, final V value) {
115 return new Unordered<>(key, value);
119 * Create a {@link SharedSingletonMap} of specified {@code key} and {@code value}, which retains insertion order
120 * when transformed via {@link #toModifiableMap()}.
122 * @param map input map
123 * @return A SharedSingletonMap
124 * @throws NullPointerException if {@code map} is null
125 * @throws IllegalArgumentException if {@code map} does not have exactly one entry
127 public static <K, V> @NonNull SharedSingletonMap<K, V> orderedCopyOf(final Map<K, V> map) {
128 final Entry<K, V> e = singleEntry(map);
129 return new Ordered<>(e.getKey(), e.getValue());
133 * Create a {@link SharedSingletonMap} from specified single-element map, which does not retain insertion order when
134 * transformed via {@link #toModifiableMap()}.
136 * @param map input map
137 * @return A SharedSingletonMap
138 * @throws NullPointerException if {@code map} is null
139 * @throws IllegalArgumentException if {@code map} does not have exactly one entry
141 public static <K, V> @NonNull SharedSingletonMap<K, V> unorderedCopyOf(final Map<K, V> map) {
142 final Entry<K, V> e = singleEntry(map);
143 return new Unordered<>(e.getKey(), e.getValue());
147 public final SingletonSet<Entry<K, V>> entrySet() {
148 return SingletonSet.of(new SimpleImmutableEntry<>(keySet.getElement(), value));
152 public final SingletonSet<K> keySet() {
157 public final SingletonSet<V> values() {
158 return SingletonSet.of(value);
162 public final boolean containsKey(final Object key) {
163 return keySet.contains(key);
167 @SuppressWarnings("checkstyle:hiddenField")
168 public final boolean containsValue(final Object value) {
169 return this.value.equals(value);
173 public final V get(final Object key) {
174 return keySet.contains(key) ? value : null;
178 public final int size() {
183 public final boolean isEmpty() {
188 @SuppressWarnings("checkstyle:hiddenField")
189 public final V put(final K key, final V value) {
190 throw new UnsupportedOperationException();
194 public final V remove(final Object key) {
195 throw new UnsupportedOperationException();
199 @SuppressWarnings("checkstyle:parameterName")
200 public final void putAll(final Map<? extends K, ? extends V> m) {
201 throw new UnsupportedOperationException();
205 public final void clear() {
206 throw new UnsupportedOperationException();
210 public final int hashCode() {
212 hashCode = keySet.getElement().hashCode() ^ value.hashCode();
218 public final boolean equals(final Object obj) {
222 if (!(obj instanceof Map)) {
226 final Map<?, ?> m = (Map<?, ?>)obj;
227 return m.size() == 1 && value.equals(m.get(keySet.getElement()));
231 public final String toString() {
232 return "{" + keySet.getElement() + '=' + value + '}';
235 @SuppressWarnings("unchecked")
236 static <K> @NonNull SingletonSet<K> cachedSet(final K key) {
237 return (SingletonSet<K>) CACHE.getUnchecked(key);
240 private static <K, V> Entry<K, V> singleEntry(final Map<K, V> map) {
241 final Iterator<Entry<K, V>> it = map.entrySet().iterator();
242 checkArgument(it.hasNext(), "Input map is empty");
243 final Entry<K, V> ret = it.next();
244 checkArgument(!it.hasNext(), "Input map has more than one entry");