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.
27 * @param <K> the type of keys maintained by this map
28 * @param <V> the type of mapped values
31 public abstract class SharedSingletonMap<K, V> implements Serializable, UnmodifiableMapPhase<K, V> {
32 static final class Ordered<K, V> extends SharedSingletonMap<K, V> {
33 private static final long serialVersionUID = 1L;
35 Ordered(final K key, final V value) {
39 Ordered(final SingletonSet<K> keySet, final V value) {
44 public ModifiableMapPhase<K, V> toModifiableMap() {
45 return MutableOffsetMap.orderedCopyOf(this);
49 static final class Unordered<K, V> extends SharedSingletonMap<K, V> {
50 private static final long serialVersionUID = 1L;
52 Unordered(final K key, final V value) {
56 Unordered(final SingletonSet<K> keySet, final V value) {
61 public ModifiableMapPhase<K, V> toModifiableMap() {
62 return MutableOffsetMap.unorderedCopyOf(this);
66 private static final long serialVersionUID = 1L;
67 private static final LoadingCache<Object, SingletonSet<Object>> CACHE = CacheBuilder.newBuilder().weakValues()
68 .build(new CacheLoader<Object, SingletonSet<Object>>() {
70 public SingletonSet<Object> load(final Object key) {
71 return SingletonSet.of(key);
75 private final @NonNull SingletonSet<K> keySet;
76 private final @NonNull V value;
79 SharedSingletonMap(final SingletonSet<K> keySet, final V value) {
80 this.keySet = requireNonNull(keySet);
81 this.value = requireNonNull(value);
84 SharedSingletonMap(final K key, final V value) {
85 this(cachedSet(key), value);
89 * Create a {@link SharedSingletonMap} of specified {@code key} and {@code value}, which retains insertion order
90 * when transformed via {@link #toModifiableMap()}.
94 * @return A SharedSingletonMap
95 * @throws NullPointerException if any of the arguments is null
97 public static <K, V> @NonNull SharedSingletonMap<K, V> orderedOf(final K key, final V value) {
98 return new Ordered<>(key, value);
102 * Create a {@link SharedSingletonMap} of specified {@code key} and {@code value}, which does not retain insertion
103 * order when transformed via {@link #toModifiableMap()}.
107 * @return A SharedSingletonMap
108 * @throws NullPointerException if any of the arguments is null
110 public static <K, V> @NonNull SharedSingletonMap<K, V> unorderedOf(final K key, final V value) {
111 return new Unordered<>(key, value);
115 * Create a {@link SharedSingletonMap} of specified {@code key} and {@code value}, which retains insertion order
116 * when transformed via {@link #toModifiableMap()}.
118 * @param map input map
119 * @return A SharedSingletonMap
120 * @throws NullPointerException if {@code map} is null
121 * @throws IllegalArgumentException if {@code map} does not have exactly one entry
123 public static <K, V> @NonNull SharedSingletonMap<K, V> orderedCopyOf(final Map<K, V> map) {
124 final Entry<K, V> e = singleEntry(map);
125 return new Ordered<>(e.getKey(), e.getValue());
129 * Create a {@link SharedSingletonMap} from specified single-element map, which does not retain insertion order when
130 * transformed via {@link #toModifiableMap()}.
132 * @param map input map
133 * @return A SharedSingletonMap
134 * @throws NullPointerException if {@code map} is null
135 * @throws IllegalArgumentException if {@code map} does not have exactly one entry
137 public static <K, V> @NonNull SharedSingletonMap<K, V> unorderedCopyOf(final Map<K, V> map) {
138 final Entry<K, V> e = singleEntry(map);
139 return new Unordered<>(e.getKey(), e.getValue());
143 public final SingletonSet<Entry<K, V>> entrySet() {
144 return SingletonSet.of(new SimpleImmutableEntry<>(keySet.getElement(), value));
148 public final SingletonSet<K> keySet() {
153 public final SingletonSet<V> values() {
154 return SingletonSet.of(value);
158 public final boolean containsKey(final Object key) {
159 return keySet.contains(key);
163 @SuppressWarnings("checkstyle:hiddenField")
164 public final boolean containsValue(final Object value) {
165 return this.value.equals(value);
169 public final V get(final Object key) {
170 return keySet.contains(key) ? value : null;
174 public final int size() {
179 public final boolean isEmpty() {
184 @SuppressWarnings("checkstyle:hiddenField")
185 public final V put(final K key, final V value) {
186 throw new UnsupportedOperationException();
190 public final V remove(final Object key) {
191 throw new UnsupportedOperationException();
195 @SuppressWarnings("checkstyle:parameterName")
196 public final void putAll(final Map<? extends K, ? extends V> m) {
197 throw new UnsupportedOperationException();
201 public final void clear() {
202 throw new UnsupportedOperationException();
206 public final int hashCode() {
208 hashCode = keySet.getElement().hashCode() ^ value.hashCode();
214 public final boolean equals(final Object obj) {
218 if (!(obj instanceof Map)) {
222 final Map<?, ?> m = (Map<?, ?>)obj;
223 return m.size() == 1 && value.equals(m.get(keySet.getElement()));
227 public final String toString() {
228 return "{" + keySet.getElement() + '=' + value + '}';
231 @SuppressWarnings("unchecked")
232 static <K> @NonNull SingletonSet<K> cachedSet(final K key) {
233 return (SingletonSet<K>) CACHE.getUnchecked(key);
236 private static <K, V> Entry<K, V> singleEntry(final Map<K, V> map) {
237 final Iterator<Entry<K, V>> it = map.entrySet().iterator();
238 checkArgument(it.hasNext(), "Input map is empty");
239 final Entry<K, V> ret = it.next();
240 checkArgument(!it.hasNext(), "Input map has more than one entry");