-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathSnippets.java
More file actions
768 lines (672 loc) · 26 KB
/
Snippets.java
File metadata and controls
768 lines (672 loc) · 26 KB
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
package snippets;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.IntFunction;
import java.util.function.IntPredicate;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.LinkedHashMap;
public abstract class Snippets {
/**
* Calculates the greatest common denominator (gcd) of an array of numbers
*
* @param numbers Array of numbers
* @return gcd of array of numbers
*/
public static OptionalInt gcd(int[] numbers) {
return Arrays.stream(numbers)
.reduce((a, b) -> gcd(a, b));
}
/**
* Calculates the lowest common multiple (lcm) of an array of numbers.
*
* @param numbers Array of numbers
* @return lcm of array of numbers
*/
public static OptionalInt lcm(int[] numbers) {
IntBinaryOperator lcm = (x, y) -> (x * y) / gcd(x, y);
return Arrays.stream(numbers)
.reduce((a, b) -> lcm.applyAsInt(a, b));
}
private static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
/**
* Returns the maximum value in an array.
*
* @param numbers Array of numbers
* @return maximum value in an array
*/
public static OptionalInt arrayMax(int[] numbers) {
return Arrays.stream(numbers).max();
}
/**
* Returns the minimum value in an array.
*
* @param numbers Array of numbers
* @return minimum value in an array
*/
public static OptionalInt arrayMin(int[] numbers) {
return Arrays.stream(numbers).min();
}
/**
* Chunks an array into smaller arrays of a specified size.
*
* @param numbers Input array of numbers
* @param size The chunk size
* @return Smaller chunks
*/
public static int[][] chunk(int[] numbers, int size) {
return IntStream.iterate(0, i -> i + size)
.limit((long) Math.ceil((double) numbers.length / size))
.mapToObj(cur -> Arrays.copyOfRange(numbers, cur, cur + size > numbers.length ? numbers.length : cur + size))
.toArray(int[][]::new);
}
/**
* Counts the occurrences of a value in an array.
*
* @param numbers Array of numbers
* @param value the value for which we have to count occurrences
* @return count of total number of occurrences of the value
*/
public static long countOccurrences(int[] numbers, int value) {
return Arrays.stream(numbers)
.filter(number -> number == value)
.count();
}
/**
* Deep flattens an array.
*
* @param input A nested array containing integers
* @return flattened array
*/
public static int[] deepFlatten(Object[] input) {
return Arrays.stream(input)
.flatMapToInt(o -> {
if (o instanceof Object[]) {
return Arrays.stream(deepFlatten((Object[]) o));
}
return IntStream.of((Integer) o);
}).toArray();
}
/**
* Returns the difference between two arrays.
*
* @param first the first array
* @param second the second array
* @return Elements in first that are not in second
*/
public static int[] difference(int[] first, int[] second) {
Set<Integer> set = Arrays.stream(second).boxed().collect(Collectors.toSet());
return Arrays.stream(first)
.filter(v -> !set.contains(v))
.toArray();
}
/**
* Filters out all values from an array for which the comparator function does not return true.
*
* @param first the first array
* @param second the second array
* @param comparator the comparator function
* @return the resulting array
*/
public static int[] differenceWith(int[] first, int[] second, IntBinaryOperator comparator) {
return Arrays.stream(first)
.filter(a ->
Arrays.stream(second)
.noneMatch(b -> comparator.applyAsInt(a, b) == 0)
).toArray();
}
/**
* Returns all the distinct values of an array.
*
* @param elements ints
* @return distinct values
*/
public static int[] distinctValuesOfArray(int[] elements) {
return Arrays.stream(elements).distinct().toArray();
}
/**
* Removes elements in an array until the passed function returns true. Returns the remaining elements in the array.
*
* @param elements
* @param condition
* @return
*/
public static int[] dropElements(int[] elements, IntPredicate condition) {
while (elements.length > 0 && !condition.test(elements[0])) {
elements = Arrays.copyOfRange(elements, 1, elements.length);
}
return elements;
}
/**
* Returns a new array with n elements removed from the right
*
* @param elements
* @param n number of elements to remove
* @return array after removing n elements
*/
public static int[] dropRight(int[] elements, int n) {
if (n < 0) {
throw new IllegalArgumentException("n is less than 0");
}
return n < elements.length
? Arrays.copyOfRange(elements, 0, elements.length - n)
: new int[0];
}
/**
* Returns every nth element in an array.
*
* @param elements
* @param nth
* @return
*/
public static int[] everyNth(int[] elements, int nth) {
return IntStream.range(0, elements.length)
.filter(i -> i % nth == nth - 1)
.map(i -> elements[i])
.toArray();
}
/**
* Filters out the non-unique values in an array.
* <p>
* Use Array.stream().filter() for an array containing only the unique values.
*
* @param elements input array
* @return unique values in the array
*/
public static int[] filterNonUnique(int[] elements) {
return Arrays.stream(elements)
.filter(el -> indexOf(elements, el) == lastIndexOf(elements, el))
.toArray();
}
/**
* Find index of element in the array. Return -1 in case element does not exist.
* <p>
* Uses IntStream.range().filter() to find index of the element in the array.
*
* @param elements input array
* @param el element to find
* @return index of the element
*/
public static int indexOf(int[] elements, int el) {
return IntStream.range(0, elements.length)
.filter(idx -> elements[idx] == el)
.findFirst()
.orElse(-1);
}
/**
* Find last index of element in the array. Return -1 in case element does not exist.
* <p>
* Uses IntStream.iterate().limit().filter() to find index of the element in the array.
*
* @param elements input array
* @param el element to find
* @return index of the element
*/
public static int lastIndexOf(int[] elements, int el) {
return IntStream.iterate(elements.length - 1, i -> i - 1)
.limit(elements.length)
.filter(idx -> elements[idx] == el)
.findFirst()
.orElse(-1);
}
/**
* Flattens an array.
*
* @param elements input array
* @return flattened array
*/
public static int[] flatten(Object[] elements) {
return Arrays.stream(elements)
.flatMapToInt(el -> el instanceof int[]
? Arrays.stream((int[]) el)
: IntStream.of((int) el)
).toArray();
}
/**
* Flattens an array up to the specified depth.
*
* @param elements input array
* @param depth depth to which to flatten array
* @return flattened array
*/
public static Object[] flattenDepth(Object[] elements, int depth) {
if (depth == 0) {
return elements;
}
return Arrays.stream(elements)
.flatMap(el -> el instanceof Object[]
? Arrays.stream(flattenDepth((Object[]) el, depth - 1))
: Arrays.stream(new Object[]{el})
).toArray();
}
/**
* Groups the elements of an array based on the given function.
*
* @param elements input array
* @param func function
* @param <T> type parameter
* @return grouped elements in a Map
*/
public static <T, R> Map<R, List<T>> groupBy(T[] elements, Function<T, R> func) {
return Arrays.stream(elements).collect(
Collectors.groupingBy(
func,
LinkedHashMap::new,
Collectors.toList()
)
);
}
/**
* Returns all the elements of an array except the last one.
* Use Arrays.copyOfRange() to return all except the last one
*
* @param elements
* @param <T>
* @return
*/
public static <T> T[] initial(T[] elements) {
return Arrays.copyOfRange(elements, 0, elements.length - 1);
}
/**
* Initializes an array containing the numbers in the specified range where start and end are inclusive.
*
* @param end
* @param start
* @return
*/
public static int[] initializeArrayWithRange(int end, int start) {
return IntStream.rangeClosed(start, end).toArray();
}
public static int[] initializeArrayWithValues(int n, int value) {
return IntStream.generate(() -> value).limit(n).toArray();
}
public static int[] intersection(int[] first, int[] second) {
Set<Integer> set = Arrays.stream(second).boxed().collect(Collectors.toSet());
return Arrays.stream(first)
.filter(set::contains)
.toArray();
}
public static <T extends Comparable<? super T>> int isSorted(T[] arr) {
final int direction = arr[0].compareTo(arr[1]) < 0 ? 1 : -1;
for (int i = 0; i < arr.length; i++) {
T val = arr[i];
if (i == arr.length - 1) return direction;
else if ((val.compareTo(arr[i + 1]) * direction > 0)) return 0;
}
return direction;
}
public static <T> String join(T[] arr, String separator, String end) {
return IntStream.range(0, arr.length)
.mapToObj(i -> new SimpleEntry<>(i, arr[i]))
.reduce("", (acc, val) -> val.getKey() == arr.length - 2
? acc + val.getValue() + end
: val.getKey() == arr.length - 1 ? acc + val.getValue() : acc + val.getValue() + separator, (fst, snd) -> fst);
}
public static <T> String join(T[] arr, String separator) {
return join(arr, separator, separator);
}
public static <T> String join(T[] arr) {
return join(arr, ",");
}
public static <T> T nthElement(T[] arr, int n) {
if (n > 0) {
return Arrays.copyOfRange(arr, n, arr.length)[0];
}
return Arrays.copyOfRange(arr, arr.length + n, arr.length)[0];
}
public static <T, R> Map<T, R> pick(Map<T, R> obj, T[] arr) {
return Arrays.stream(arr)
.filter(obj::containsKey)
.collect(Collectors.toMap(k -> k, obj::get));
}
public static Map<String, Object>[] reducedFilter(Map<String, Object>[] data, String[] keys, Predicate<Map<String, Object>> fn) {
return Arrays.stream(data)
.filter(fn)
.map(el -> Arrays.stream(keys).filter(el::containsKey)
.collect(Collectors.toMap(Function.identity(), el::get)))
.toArray((IntFunction<Map<String, Object>[]>) Map[]::new);
}
public static <T> T sample(T[] arr) {
return arr[(int) Math.floor(Math.random() * arr.length)];
}
public static <T> T[] sampleSize(T[] input, int n) {
T[] arr = Arrays.copyOf(input, input.length);
int length = arr.length;
int m = length;
while (m > 0) {
int i = (int) Math.floor(Math.random() * m--);
T tmp = arr[i];
arr[i] = arr[m];
arr[m] = tmp;
}
return Arrays.copyOfRange(arr, 0, n > length ? length : n);
}
public static <T> T[] shuffle(T[] input) {
T[] arr = Arrays.copyOf(input, input.length);
int length = arr.length;
int m = length;
while (m > 0) {
int i = (int) Math.floor(Math.random() * m--);
T tmp = arr[i];
arr[i] = arr[m];
arr[m] = tmp;
}
return arr;
}
public static <T> T[] similarity(T[] first, T[] second) {
return Arrays.stream(first)
.filter(a -> Arrays.stream(second).anyMatch(b -> Objects.equals(a, b)))
// Make a new array of first's runtime type, but empty content:
.toArray(i -> (T[]) Arrays.copyOf(new Object[0], i, first.getClass()));
}
public static <T> T[] emptyArray(Class<T> clz) {
return (T[]) Array.newInstance(clz, 0);
}
public static <T extends Comparable<? super T>> int sortedIndex(T[] arr, T el) {
boolean isDescending = arr[0].compareTo(arr[arr.length - 1]) > 0;
return IntStream.range(0, arr.length)
.filter(i -> isDescending ? el.compareTo(arr[i]) >= 0 : el.compareTo(arr[i]) <= 0)
.findFirst()
.orElse(arr.length);
}
public static <T> T[] symmetricDifference(T[] first, T[] second) {
Set<T> sA = new HashSet<>(Arrays.asList(first));
Set<T> sB = new HashSet<>(Arrays.asList(second));
return Stream.concat(
Arrays.stream(first).filter(a -> !sB.contains(a)),
Arrays.stream(second).filter(b -> !sA.contains(b))
).toArray(i -> (T[]) Arrays.copyOf(new Object[0], i, first.getClass()));
}
public static <T> T[] tail(T[] arr) {
return arr.length > 1
? Arrays.copyOfRange(arr, 1, arr.length)
: arr;
}
public static <T> T[] take(T[] arr, int n) {
return Arrays.copyOfRange(arr, 0, n);
}
public static <T> T[] takeRight(T[] arr, int n) {
return Arrays.copyOfRange(arr, arr.length - n, arr.length);
}
public static <T> T[] union(T[] first, T[] second) {
Set<T> set = new HashSet<>(Arrays.asList(first));
set.addAll(Arrays.asList(second));
return set.toArray((T[]) Arrays.copyOf(new Object[0], 0, first.getClass()));
}
public static <T> T[] without(T[] arr, T... elements) {
List<T> excludeElements = Arrays.asList(elements);
return Arrays.stream(arr)
.filter(el -> !excludeElements.contains(el))
.toArray(i -> (T[]) Arrays.copyOf(new Object[0], i, arr.getClass()));
}
public static List<Object[]> zip(Object[]... arrays) {
OptionalInt max = Arrays.stream(arrays).mapToInt(arr -> arr.length).max();
return IntStream.range(0, max.getAsInt())
.mapToObj(i -> Arrays.stream(arrays)
.map(arr -> i < arr.length ? arr[i] : null)
.toArray())
.collect(Collectors.toList());
}
public static Map<String, Object> zipObject(String[] props, Object[] values) {
return IntStream.range(0, props.length)
.mapToObj(i -> new SimpleEntry<>(props[i], i < values.length ? values[i] : null))
.collect(
HashMap::new, (m, v) -> m.put(v.getKey(), v.getValue()), HashMap::putAll);
}
public static double average(int[] arr) {
return IntStream.of(arr)
.average()
.orElseThrow(() -> new IllegalArgumentException("Array is empty"));
}
public static List<String> anagrams(String input) {
if (input.length() <= 2) {
return input.length() == 2
? Arrays.asList(input, input.substring(1) + input.substring(0, 1))
: Collections.singletonList(input);
}
return IntStream.range(0, input.length())
.mapToObj(i -> new SimpleEntry<>(i, input.substring(i, i + 1)))
.flatMap(entry ->
anagrams(input.substring(0, entry.getKey()) + input.substring(entry.getKey() + 1))
.stream()
.map(s -> entry.getValue() + s))
.collect(Collectors.toList());
}
public static int byteSize(String input) {
// Read the link below to learn more
// https://stackoverflow.com/questions/16270994/difference-between-string-length-and-string-getbytes-length
return input.getBytes().length;
}
public static String capitalize(String input, boolean lowerRest) {
return input.substring(0, 1).toUpperCase() +
(lowerRest
? input.substring(1, input.length()).toLowerCase()
: input.substring(1, input.length()));
}
public static String capitalizeEveryWord(final String input) {
return Pattern.compile("\\b(?=\\w)").splitAsStream(input)
.map(w -> capitalize(w, false))
.collect(Collectors.joining());
}
public static int countVowels(String input) {
return input.replaceAll("[^aeiouAEIOU]", "").length();
}
public static String escapeRegExp(String input) {
return Pattern.quote(input);
}
public static String fromCamelCase(String input, String separator) {
return input
.replaceAll("([a-z\\d])([A-Z])", "$1" + separator + "$2")
.toLowerCase();
}
public static boolean isAbsoluteUrl(String url) {
return Pattern.compile("^[a-z][a-z0-9+.-]*:").matcher(url).find();
}
public static boolean isLowerCase(String input) {
return Objects.equals(input, input.toLowerCase());
}
public static boolean isUpperCase(String input) {
return Objects.equals(input, input.toUpperCase());
}
public static String mask(String input, int num, String mask) {
int length = input.length();
return num > 0
?
input.substring(0, length - num).replaceAll(".", mask)
+ input.substring(length - num)
:
input.substring(0, Math.negateExact(num))
+ input.substring(Math.negateExact(num), length).replaceAll(".", mask);
}
public static boolean isPalindrome(String input) {
String s = input.toLowerCase().replaceAll("[\\W_]", "");
return Objects.equals(
s,
new StringBuilder(s).reverse().toString()
);
}
public static String reverseString(String input) {
return new StringBuilder(input).reverse().toString();
}
public static String sortCharactersInString(String input) {
return Arrays.stream(input.split("")).sorted().collect(Collectors.joining());
}
public static String[] splitLines(String input) {
return input.split("\\r?\\n");
}
public static String toCamelCase(String input) {
Matcher matcher = Pattern.compile("[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+").matcher(input);
List<String> matchedParts = new ArrayList<>();
while (matcher.find()) {
matchedParts.add(matcher.group(0));
}
String s = matchedParts.stream()
.map(x -> x.substring(0, 1).toUpperCase() + x.substring(1).toLowerCase())
.collect(Collectors.joining());
return s.substring(0, 1).toLowerCase() + s.substring(1);
}
public static String toKebabCase(String input) {
Matcher matcher = Pattern.compile("[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+").matcher(input);
List<String> matchedParts = new ArrayList<>();
while (matcher.find()) {
matchedParts.add(matcher.group(0));
}
return matchedParts.stream()
.map(String::toLowerCase)
.collect(Collectors.joining("-"));
}
public static List<String> match(String input, String regex) {
Matcher matcher = Pattern.compile(regex).matcher(input);
List<String> matchedParts = new ArrayList<>();
while (matcher.find()) {
matchedParts.add(matcher.group(0));
}
return matchedParts;
}
public static String toSnakeCase(String input) {
Matcher matcher = Pattern.compile("[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+").matcher(input);
List<String> matchedParts = new ArrayList<>();
while (matcher.find()) {
matchedParts.add(matcher.group(0));
}
return matchedParts.stream()
.map(String::toLowerCase)
.collect(Collectors.joining("_"));
}
public static String truncateString(String input, int num) {
return input.length() > num
? input.substring(0, num > 3 ? num - 3 : num) + "..."
: input;
}
public static String[] words(String input) {
return Arrays.stream(input.split("[^a-zA-Z-]+"))
.filter(s -> !s.isEmpty())
.toArray(String[]::new);
}
//Read the link below for more information
// https://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
public static String convertInputStreamToString(final InputStream in) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString(StandardCharsets.UTF_8.name());
}
public static int[] randomInts(int total, int start, int end) {
return ThreadLocalRandom.current().ints(total, start, end).toArray();
}
public String readFileAsString(Path path) throws IOException {
return new String(Files.readAllBytes(path));
}
public static String stackTraceAsString(final Throwable throwable) {
final StringWriter sw = new StringWriter();
throwable.printStackTrace(new PrintWriter(sw));
return sw.toString();
}
public static <T> T[] concat(T[] first, T[] second) {
return Stream.concat(
Stream.of(first),
Stream.of(second)
).toArray(i -> (T[]) Arrays.copyOf(new Object[0], i, first.getClass()));
}
public static String getCurrentWorkingDirectoryPath() {
return FileSystems.getDefault().getPath("").toAbsolutePath().toString();
}
public static boolean isNumeric(final String input) {
if (input == null || input.isEmpty()) {
return false;
}
return IntStream.range(0, input.length())
.allMatch(i -> Character.isDigit(input.charAt(i)));
}
public static int findNextPositivePowerOfTwo(int value) {
return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
}
public static boolean isEven(final int value) {
return (value & 0b1) == 0;
}
public static boolean isPowerOfTwo(final int value) {
return value > 0 && ((value & (~value + 1)) == value);
}
public static int generateRandomInt() {
return ThreadLocalRandom.current().nextInt();
}
public static String tmpDirName() {
String tmpDirName = System.getProperty("java.io.tmpdir");
if (!tmpDirName.endsWith(File.separator)) {
tmpDirName += File.separator;
}
return tmpDirName;
}
public static String osName() {
return System.getProperty("os.name").toLowerCase();
}
public static boolean isDebuggerAttached() {
final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
return runtimeMXBean.getInputArguments()
.stream()
.anyMatch(arg -> arg.contains("-agentlib:jdwp"));
}
/**
* Input a line of numbers separated by space as integers
* and return ArrayList of Integers.
* eg. the String "1 2 3 4 5 6 7 8 9" is returned as an ArrayList of Integers.
*
* @param numbers range of numbers separated by space as a string
* @return ArrayList of Integers
*/
public static int[] stringToIntegers(String numbers) {
return Arrays.stream(numbers.split(" ")).mapToInt(Integer::parseInt).toArray();
}
/* Class Utilities */
public static List<Class<?>> getAllInterfaces(final Class<?> cls) {
return Stream.concat(
Arrays.stream(cls.getInterfaces()).flatMap(intf ->
Stream.concat(Stream.of(intf), getAllInterfaces(intf).stream())),
cls.getSuperclass() == null ? Stream.empty() : getAllInterfaces(cls.getSuperclass()).stream()
).distinct().collect(Collectors.toList());
}
public static boolean isInnerClass(final Class<?> cls) {
return cls != null && cls.getEnclosingClass() != null;
}
public static <E extends Enum<E>> Map<String, E> getEnumMap(final Class<E> enumClass) {
return Arrays.stream(enumClass.getEnumConstants())
.collect(Collectors.toMap(Enum::name, Function.identity()));
}
}