2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.base.MoreObjects;
12 import java.util.function.BiFunction;
13 import org.eclipse.jdt.annotation.NonNull;
16 * Template for instantiating {@link SharedSingletonMap} instances with a fixed key. The template can then be
17 * used as a factory for instances via using {@link #instantiateTransformed(Map, BiFunction)} or, more efficiently,
18 * using {@link #instantiateWithValue(Object)}.
20 * @param <K> the type of keys maintained by this template
22 public abstract sealed class SharedSingletonMapTemplate<K> extends ImmutableMapTemplate<K> {
23 private static final class Ordered<K> extends SharedSingletonMapTemplate<K> {
24 Ordered(final K key) {
29 public <V> @NonNull SharedSingletonMap<K, V> instantiateWithValue(final V value) {
30 return new SharedSingletonMap.Ordered<>(keySet(), value);
34 private static final class Unordered<K> extends SharedSingletonMapTemplate<K> {
35 Unordered(final K key) {
40 public <V> @NonNull SharedSingletonMap<K, V> instantiateWithValue(final V value) {
41 return new SharedSingletonMap.Unordered<>(keySet(), value);
45 private final @NonNull SingletonSet<K> keySet;
47 private SharedSingletonMapTemplate(final K key) {
48 keySet = SharedSingletonMap.cachedSet(key);
52 * Create a template which produces Maps with specified key. The resulting map will retain insertion order through
53 * {@link UnmodifiableMapPhase#toModifiableMap()} transformations.
55 * @param key Single key in resulting map
56 * @param <K> the type of keys maintained by resulting template
57 * @return A template object.
58 * @throws NullPointerException if {@code key} is null
60 public static <K> @NonNull SharedSingletonMapTemplate<K> ordered(final K key) {
61 return new Ordered<>(key);
65 * Create a template which produces Maps with specified key. The resulting map will NOT retain ordering through
66 * {@link UnmodifiableMapPhase#toModifiableMap()} transformations.
68 * @param key Single key in resulting map
69 * @param <K> the type of keys maintained by resulting template
70 * @return A template object.
71 * @throws NullPointerException if {@code key} is null
73 public static <K> @NonNull SharedSingletonMapTemplate<K> unordered(final K key) {
74 return new Unordered<>(key);
78 public final SingletonSet<K> keySet() {
83 public final <T, V> @NonNull SharedSingletonMap<K, V> instantiateTransformed(final Map<K, T> fromMap,
84 final BiFunction<K, T, V> valueTransformer) {
85 final var it = fromMap.entrySet().iterator();
87 throw new IllegalArgumentException("Input is empty while expecting 1 item");
90 final var entry = it.next();
91 final var expected = keySet.getElement();
92 final var actual = entry.getKey();
93 if (!expected.equals(actual)) {
94 throw new IllegalArgumentException("Unexpected key " + actual + ", expecting " + expected);
97 final var value = transformValue(actual, entry.getValue(), valueTransformer);
99 throw new IllegalArgumentException("Input has more than one item");
101 return instantiateWithValue(value);
106 public final <V> @NonNull SharedSingletonMap<K, V> instantiateWithValues(final V... values) {
107 checkSize(1, values.length);
108 return instantiateWithValue(values[0]);
112 * Instantiate an immutable map with the value supplied.
114 * @param value Value to use
115 * @param <V> the type of mapped values
116 * @return An immutable map
117 * @throws NullPointerException if {@code value} is null
119 public abstract <V> @NonNull SharedSingletonMap<K, V> instantiateWithValue(V value);
122 public final String toString() {
123 return MoreObjects.toStringHelper(this).add("keySet", keySet).toString();