-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
MapUtil.java
468 lines (435 loc) · 15 KB
/
MapUtil.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.helpers.collection;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Utility to create {@link Map}s.
*/
public abstract class MapUtil
{
/**
* A short-hand method for creating a {@link Map} of key/value pairs.
*
* @param objects alternating key and value.
* @param <K> type of keys
* @param <V> type of values
* @return a Map with the entries supplied by {@code objects}.
*/
public static <K, V> Map<K, V> genericMap( Object... objects )
{
return genericMap( new HashMap<K, V>(), objects );
}
/**
* A short-hand method for adding key/value pairs into a {@link Map}.
*
* @param targetMap the {@link Map} to put the objects into.
* @param objects alternating key and value.
* @param <K> type of keys
* @param <V> type of values
* @return a Map with the entries supplied by {@code objects}.
*/
@SuppressWarnings( "unchecked" )
public static <K, V> Map<K,V> genericMap( Map<K,V> targetMap, Object... objects )
{
int i = 0;
while ( i < objects.length )
{
targetMap.put( (K) objects[i++], (V) objects[i++] );
}
return targetMap;
}
/**
* A short-hand method for creating a {@link Map} of key/value pairs where
* both keys and values are {@link String}s.
*
* @param strings alternating key and value.
* @return a Map with the entries supplied by {@code strings}.
*/
public static Map<String, String> stringMap( String... strings )
{
return genericMap( (Object[]) strings );
}
/**
* A short-hand method for creating a {@link Map} of key/value pairs where
* both keys and values are {@link String}s.
*
* @param targetMap the {@link Map} to put the objects into.
* @param strings alternating key and value.
* @return a Map with the entries supplied by {@code strings}.
*/
public static Map<String, String> stringMap( Map<String, String> targetMap,
String... strings )
{
return genericMap( targetMap, (Object[]) strings );
}
/**
* A short-hand method for creating a {@link Map} of key/value pairs where
* keys are {@link String}s and values are {@link Object}s.
*
* @param objects alternating key and value.
* @return a Map with the entries supplied by {@code objects}.
*/
public static Map<String, Object> map( Object... objects )
{
return genericMap( objects );
}
/**
* A short-hand method for creating a {@link Map} of key/value pairs where
* keys are {@link String}s and values are {@link Object}s.
*
* @param targetMap the {@link Map} to put the objects into.
* @param objects alternating key and value.
* @return a Map with the entries supplied by {@code objects}.
*/
public static Map<String, Object> map( Map<String, Object> targetMap,
Object... objects )
{
return genericMap( targetMap, objects );
}
/**
* Loads a {@link Map} from a {@link Reader} assuming strings as keys
* and values.
*
* @param reader the {@link Reader} containing a {@link Properties}-like
* layout of keys and values.
* @return the read data as a {@link Map}.
* @throws IOException if the {@code reader} throws {@link IOException}.
*/
public static Map<String, String> load( Reader reader ) throws IOException
{
Properties props = new Properties();
props.load( reader );
//noinspection unchecked
return new HashMap<>( (Map) props );
}
/**
* Loads a {@link Map} from a {@link Reader} assuming strings as keys
* and values. Any {@link IOException} is wrapped and thrown as a
* {@link RuntimeException} instead.
*
* @param reader the {@link Reader} containing a {@link Properties}-like
* layout of keys and values.
* @return the read data as a {@link Map}.
*/
public static Map<String, String> loadStrictly( Reader reader )
{
try
{
return load( reader );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
/**
* Loads a {@link Map} from an {@link InputStream} assuming strings as keys
* and values.
*
* @param stream the {@link InputStream} containing a
* {@link Properties}-like layout of keys and values.
* @return the read data as a {@link Map}.
* @throws IOException if the {@code stream} throws {@link IOException}.
*/
public static Map<String,String> load( InputStream stream ) throws IOException
{
Properties props = new Properties();
props.load( stream );
HashMap<String,String> result = new HashMap<>();
for ( Map.Entry<Object,Object> entry : props.entrySet() )
{
// Properties does not trim whitespace from the right side of values
result.put( (String) entry.getKey(), ( (String) entry.getValue() ).trim() );
}
return result;
}
/**
* Loads a {@link Map} from an {@link InputStream} assuming strings as keys
* and values. Any {@link IOException} is wrapped and thrown as a
* {@link RuntimeException} instead.
*
* @param stream the {@link InputStream} containing a
* {@link Properties}-like layout of keys and values.
* @return the read data as a {@link Map}.
*/
public static Map<String, String> loadStrictly( InputStream stream )
{
try
{
return load( stream );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
/**
* Loads a {@link Map} from a {@link File} assuming strings as keys
* and values.
*
* @param file the {@link File} containing a {@link Properties}-like
* layout of keys and values.
* @return the read data as a {@link Map}.
* @throws IOException if the file reader throws {@link IOException}.
*/
public static Map<String, String> load( File file ) throws IOException
{
FileInputStream stream = null;
try
{
stream = new FileInputStream( file );
return load( stream );
}
finally
{
closeIfNotNull( stream );
}
}
private static void closeIfNotNull( Closeable closeable ) throws IOException
{
if ( closeable != null )
{
closeable.close();
}
}
/**
* Stores the data in {@code config} into {@code file} in a standard java
* {@link Properties} format.
* @param config the data to store in the properties file.
* @param file the file to store the properties in.
* @throws IOException IO error.
*/
public static void store( Map<String, String> config, File file ) throws IOException
{
OutputStream stream = null;
try
{
stream = new BufferedOutputStream( new FileOutputStream( file ) );
store( config, stream );
}
finally
{
closeIfNotNull( stream );
}
}
/**
* Stores the data in {@code config} into {@code file} in a standard java
* {@link Properties} format. Any {@link IOException} is wrapped and thrown as a
* {@link RuntimeException} instead.
* @param config the data to store in the properties file.
* @param file the file to store the properties in.
*/
public static void storeStrictly( Map<String, String> config, File file )
{
try
{
store( config, file );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
/**
* Stores the data in {@code config} into {@code stream} in a standard java
* {@link Properties} format.
* @param config the data to store in the properties file.
* @param stream the {@link OutputStream} to store the properties in.
* @throws IOException IO error.
*/
public static void store( Map<String, String> config, OutputStream stream ) throws IOException
{
Properties properties = new Properties();
for ( Map.Entry<String, String> property : config.entrySet() )
{
properties.setProperty( property.getKey(), property.getValue() );
}
properties.store( stream, null );
}
/**
* Stores the data in {@code config} into {@code stream} in a standard java
* {@link Properties} format. Any {@link IOException} is wrapped and thrown as a
* {@link RuntimeException} instead.
* @param config the data to store in the properties file.
* @param stream the {@link OutputStream} to store the properties in.
*/
public static void storeStrictly( Map<String, String> config, OutputStream stream )
{
try
{
store( config, stream );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
/**
* Stores the data in {@code config} into {@code writer} in a standard java
* {@link Properties} format.
*
* @param config the data to store in the properties file.
* @param writer the {@link Writer} to store the properties in.
* @throws IOException IO error.
*/
public static void store( Map<String, String> config, Writer writer ) throws IOException
{
Properties properties = new Properties();
properties.putAll( config );
properties.store( writer, null );
}
/**
* Stores the data in {@code config} into {@code writer} in a standard java
* {@link Properties} format. Any {@link IOException} is wrapped and thrown
* as a {@link RuntimeException} instead.
*
* @param config the data to store in the properties file.
* @param writer the {@link Writer} to store the properties in.
*/
public static void storeStrictly( Map<String, String> config, Writer writer )
{
try
{
store( config, writer );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
/**
* Reversed a map, making the key value and the value key.
* @param <K> the type of key in the map to reverse. These will be the
* values in the returned map.
* @param <V> the type of values in the map to revert. These will be the
* keys in the returned map.
* @param map the {@link Map} to reverse.
* @return the reverse of {@code map}. A new {@link Map} will be returned
* where the keys from {@code map} will be the values and the values will
* be the keys.
*/
public static <K, V> Map<V, K> reverse( Map<K, V> map )
{
Map<V, K> reversedMap = new HashMap<>();
for ( Map.Entry<K, V> entry : map.entrySet() )
{
reversedMap.put( entry.getValue(), entry.getKey() );
}
return reversedMap;
}
public static <K, V> Map<K,V> copyAndPut( Map<K,V> map, K key, V value )
{
Map<K,V> copy = new HashMap<>( map );
copy.put( key, value );
return copy;
}
public static <K, V> Map<K,V> copyAndRemove( Map<K,V> map, K key )
{
Map<K,V> copy = new HashMap<>( map );
copy.remove( key );
return copy;
}
public static <K,V> Map<K, V> toMap( Iterable<Pair<K, V>> pairs )
{
return toMap( pairs.iterator() );
}
public static <K,V> Map<K, V> toMap( Iterator<Pair<K, V>> pairs )
{
Map<K,V> result = new HashMap<K,V>();
while ( pairs.hasNext() )
{
Pair<K,V> pair = pairs.next();
result.put( pair.first(), pair.other() );
}
return result;
}
public static <K, V> MapBuilder<K, V> entry( K key, V value )
{
return new MapBuilder<K, V>().entry( key, value );
}
public static class MapBuilder<K, V>
{
private final Map<K, V> map = new HashMap<>();
public MapBuilder<K, V> entry( K key, V value )
{
map.put( key, value );
return this;
}
public Map<K, V> create()
{
return map;
}
}
/**
* Mutates the input map by removing entries which do not have keys in the new backing data, as extracted with
* the keyExtractor.
* @param map the map to mutate.
* @param newBackingData the backing data to retain.
* @param keyExtractor the function to extract keys from the backing data.
*/
public static <K, V, T> void trimToList( Map<K,V> map, List<T> newBackingData, Function<T,K> keyExtractor )
{
Set<K> retainedKeys = newBackingData.stream().map( keyExtractor ).collect( Collectors.toSet() );
trimToList( map, retainedKeys );
}
/**
* Mutates the input map by removing entries which do not have keys in the new backing data, as extracted with
* the keyExtractor.
* @param map the map to mutate.
* @param newBackingData the backing data to retain.
* @param keyExtractor the function to extract keys from the backing data.
*/
public static <K, V, T> void trimToFlattenedList( Map<K,V> map, List<T> newBackingData,
Function<T,Stream<K>> keyExtractor )
{
Set<K> retainedKeys = newBackingData.stream().flatMap( keyExtractor ).collect( Collectors.toSet() );
trimToList( map, retainedKeys );
}
/**
* Mutates the input map by removing entries which are not in the retained set of keys.
* @param map the map to mutate.
* @param retainedKeys the keys to retain.
*/
public static <K, V> void trimToList( Map<K,V> map, Set<K> retainedKeys )
{
Set<K> keysToRemove = new HashSet<>( map.keySet() );
keysToRemove.removeAll( retainedKeys );
keysToRemove.forEach( map::remove );
}
}