forked from KyoriPowered/adventure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Index.java
262 lines (242 loc) · 7.98 KB
/
Index.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2022 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.function.Function;
import java.util.function.IntFunction;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* A bi-directional map in which keys and values must be unique.
*
* @param <K> the key type
* @param <V> the value type
* @since 4.0.0
*/
public final class Index<K, V> {
private final Map<K, V> keyToValue;
private final Map<V, K> valueToKey;
private Index(final Map<K, V> keyToValue, final Map<V, K> valueToKey) {
this.keyToValue = keyToValue;
this.valueToKey = valueToKey;
}
/**
* Creates an index map.
*
* @param type the value type
* @param keyFunction the key function
* @param <K> the key type
* @param <V> the value type
* @return the key map
* @since 4.0.0
*/
public static <K, V extends Enum<V>> @NotNull Index<K, V> create(final Class<V> type, final @NotNull Function<? super V, ? extends K> keyFunction) {
return create(type, keyFunction, type.getEnumConstants());
}
/**
* Creates an index map.
*
* @param type the value type
* @param keyFunction the key function
* @param values the values
* @param <K> the key type
* @param <V> the value type
* @return the key map
* @since 4.0.0
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <K, V extends Enum<V>> @NotNull Index<K, V> create(final Class<V> type, final @NotNull Function<? super V, ? extends K> keyFunction, final @NotNull V@NotNull... values) {
return create(values, length -> new EnumMap<>(type), keyFunction);
}
/**
* Creates an index map.
*
* @param keyFunction the key function
* @param values the values
* @param <K> the key type
* @param <V> the value type
* @return the key map
* @since 4.0.0
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <K, V> @NotNull Index<K, V> create(final @NotNull Function<? super V, ? extends K> keyFunction, final @NotNull V@NotNull... values) {
return create(values, HashMap::new, keyFunction);
}
/**
* Creates an index map.
*
* @param keyFunction the key function
* @param constants the constants
* @param <K> the key type
* @param <V> the value type
* @return the key map
* @since 4.0.0
*/
public static <K, V> @NotNull Index<K, V> create(final @NotNull Function<? super V, ? extends K> keyFunction, final @NotNull List<V> constants) {
return create(constants, HashMap::new, keyFunction);
}
private static <K, V> @NotNull Index<K, V> create(final V[] values, final IntFunction<Map<V, K>> valueToKeyFactory, final @NotNull Function<? super V, ? extends K> keyFunction) {
return create(Arrays.asList(values), valueToKeyFactory, keyFunction);
}
private static <K, V> @NotNull Index<K, V> create(final List<V> values, final IntFunction<Map<V, K>> valueToKeyFactory, final @NotNull Function<? super V, ? extends K> keyFunction) {
final int length = values.size();
final Map<K, V> keyToValue = new HashMap<>(length);
final Map<V, K> valueToKey = valueToKeyFactory.apply(length); // to support using EnumMap instead of HashMap when possible
for (int i = 0; i < length; i++) {
final V value = values.get(i);
final K key = keyFunction.apply(value);
if (keyToValue.putIfAbsent(key, value) != null) {
throw new IllegalStateException(String.format("Key %s already mapped to value %s", key, keyToValue.get(key)));
}
if (valueToKey.putIfAbsent(value, key) != null) {
throw new IllegalStateException(String.format("Value %s already mapped to key %s", value, valueToKey.get(value)));
}
}
return new Index<>(Collections.unmodifiableMap(keyToValue), Collections.unmodifiableMap(valueToKey));
}
/**
* Gets the keys.
*
* @return the keys
* @since 4.0.0
*/
public @NotNull Set<K> keys() {
return Collections.unmodifiableSet(this.keyToValue.keySet());
}
/**
* Gets the key for a value.
*
* @param value the value
* @return the key
* @since 4.0.0
*/
public @Nullable K key(final @NotNull V value) {
return this.valueToKey.get(value);
}
/**
* Gets the key for a value or throws an exception.
*
* @param value the value
* @return the key
* @throws NoSuchElementException if there is no key for the value
* @since 4.11.0
*/
public @NotNull K keyOrThrow(final @NotNull V value) {
final K key = this.key(value);
if (key == null) {
throw new NoSuchElementException("There is no key for value " + value);
}
return key;
}
/**
* Gets a key by its value or returns
* a fallback key.
*
* @param value the value
* @param defaultKey the fallback key
* @return the key
* @since 4.11.0
*/
@Contract("_, null -> null; _, !null -> !null")
public K keyOrDefault(final @NotNull V value, @Nullable K defaultKey) {
final K key = this.key(value);
return key == null ? defaultKey : key;
}
/**
* Gets the keys.
*
* @return the keys
* @since 4.0.0
*/
public @NotNull Set<V> values() {
return Collections.unmodifiableSet(this.valueToKey.keySet());
}
/**
* Gets a value by its key.
*
* @param key the key
* @return the value
* @since 4.0.0
*/
public @Nullable V value(final @NotNull K key) {
return this.keyToValue.get(key);
}
/**
* Gets a value by its key.
*
* @param key the key
* @return the value
* @throws NoSuchElementException if there is no value for the key
* @since 4.11.0
*/
public @NotNull V valueOrThrow(final @NotNull K key) {
final V value = this.value(key);
if (value == null) {
throw new NoSuchElementException("There is no value for key " + key);
}
return value;
}
/**
* Gets a value by its key or returns
* a fallback value.
*
* @param key the key
* @param defaultValue the fallback value
* @return the value
* @since 4.11.0
*/
@Contract("_, null -> null; _, !null -> !null")
public V valueOrDefault(final @NotNull K key, @Nullable V defaultValue) {
final V value = this.value(key);
return value == null ? defaultValue : value;
}
/**
* Get an unmodifiable mapping of index entries from key to value.
*
* @return a mapping from key to value in the index
* @since 4.10.0
*/
public @NotNull Map<K, V> keyToValue() {
return Collections.unmodifiableMap(this.keyToValue);
}
/**
* Get an unmodifiable mapping of index entries from value to key.
*
* @return a mapping from value to key in the index
* @since 4.10.0
*/
public @NotNull Map<V, K> valueToKey() {
return Collections.unmodifiableMap(this.valueToKey);
}
}