|
18 | 18 |
|
19 | 19 | package net.ponec.script;
|
20 | 20 |
|
| 21 | +import org.jetbrains.annotations.NotNull; |
21 | 22 | import javax.tools.ToolProvider;
|
22 | 23 | import java.io.*;
|
23 | 24 | import java.net.URI;
|
|
37 | 38 | /**
|
38 | 39 | * Usage and examples:
|
39 | 40 | * <ul>
|
40 |
| - * <li>{@code java PPUtils find main.*String java$ } - find readable files by regular expressions.</li> |
| 41 | + * <li>{@code java PPUtils find main.*String java$ } - find readable files by regular expressions. Partial compliance is assessed.</li> |
41 | 42 | * <li>{@code java PPUtils grep main.*String PPUtils.java } - find readable file rows by a regular expression.</li>
|
42 | 43 | * <li>{@code java PPUtils date} - prints a date by ISO format, for example: "2023-12-31"</li>
|
43 | 44 | * <li>{@code java PPUtils time} - prints hours and time, for example "2359"</li>
|
44 | 45 | * <li>{@code java PPUtils datetime} - prints datetime format "2023-12-31T2359"</li>
|
45 | 46 | * <li>{@code java PPUtils date-iso} - prints datetime by ISO format, eg: "2023-12-31T23:59:59.999"</li>
|
46 | 47 | * <li>{@code java PPUtils date-format "yyyy-MM-dd'T'HH:mm:ss.SSS"} - prints a time by a custom format</li>
|
| 48 | + * <li>{@code java PPUtils base64encode "file.bin"} - encode any (binary) file.</li> |
| 49 | + * <li>{@code java PPUtils base64decode "file.base64"} - decode base64 encoded file (result removes extension)</li> |
| 50 | + * <li>{@code java PPUtils key json } - Get a value by the (composite) key, for example: {@code "a.b.c"}</li> |
47 | 51 | * </ul>
|
48 | 52 | */
|
49 | 53 | public final class PPUtils {
|
@@ -103,7 +107,7 @@ void start(Array<String> args) throws Exception {
|
103 | 107 | out.println(currentDate("HHmm"));
|
104 | 108 | }
|
105 | 109 | case "datetime" -> {
|
106 |
| - out.println(currentDate("yyyy-MM'T'HHmm")); |
| 110 | + out.println(currentDate("yyyy-MM-dd'T'HHmm")); |
107 | 111 | }
|
108 | 112 | case "date-iso" -> {
|
109 | 113 | out.println(currentDate(dateIsoFormat));
|
@@ -391,85 +395,101 @@ private void removePackage(Path fullJavaClass) throws IOException {
|
391 | 395 |
|
392 | 396 |
|
393 | 397 | /** The immutable Array wrapper with utilities (from the Ujorm framework) */
|
394 |
| - record Array<T>(T[] array) { |
| 398 | + record Array<T>(T[] array) { |
| 399 | + |
| 400 | + @NotNull |
| 401 | + public Array<T> clone() { |
| 402 | + return new Array<>(toArray()); |
| 403 | + } |
395 | 404 |
|
396 | 405 | /** Negative index is supported */
|
397 |
| - public Optional<T> get(final int i) { |
398 |
| - final var j = i >= 0 ? i : array.length + i; |
399 |
| - return Optional.ofNullable(j >= 0 && j < array.length ? array[j] : null); |
400 |
| - } |
| 406 | + public Optional<T> get(final int i) { |
| 407 | + final var j = i >= 0 ? i : array.length + i; |
| 408 | + return Optional.ofNullable(j >= 0 && j < array.length ? array[j] : null); |
| 409 | + } |
401 | 410 |
|
402 |
| - /** Add new items to the new Array */ |
403 |
| - @SuppressWarnings("unchecked") |
404 |
| - public Array<T> add(final T... toAdd) { |
405 |
| - final var result = Arrays.copyOf(array, array.length + toAdd.length); |
406 |
| - System.arraycopy(toAdd, 0, result, array.length, toAdd.length); |
407 |
| - return new Array<>(result); |
408 |
| - } |
| 411 | + /** Add new items to the new Array */ |
| 412 | + @SuppressWarnings("unchecked") |
| 413 | + public Array<T> add(final T... toAdd) { |
| 414 | + final var result = Arrays.copyOf(array, array.length + toAdd.length); |
| 415 | + System.arraycopy(toAdd, 0, result, array.length, toAdd.length); |
| 416 | + return new Array<>(result); |
| 417 | + } |
409 | 418 |
|
410 |
| - /** Negative index is supported */ |
411 |
| - public T getItem(final int i) { |
412 |
| - return array[i >= 0 ? i : array.length + i]; |
413 |
| - } |
| 419 | + /** Negative index is supported */ |
| 420 | + public T getItem(final int i) { |
| 421 | + return array[i >= 0 ? i : array.length + i]; |
| 422 | + } |
414 | 423 |
|
415 |
| - public Optional<T> getFirst() { |
416 |
| - return get(0); |
417 |
| - } |
| 424 | + public Optional<T> getFirst() { |
| 425 | + return get(0); |
| 426 | + } |
418 | 427 |
|
419 |
| - public Optional<T> getLast() { |
420 |
| - return get(-1); |
421 |
| - } |
| 428 | + public Optional<T> getLast() { |
| 429 | + return get(-1); |
| 430 | + } |
422 | 431 |
|
423 |
| - public Array<T> removeFirst() { |
424 |
| - final var result = array.length > 0 ? Arrays.copyOfRange(array, 1, array.length) : array; |
425 |
| - return new Array<>(result); |
426 |
| - } |
| 432 | + public Array<T> removeFirst() { |
| 433 | + final var result = array.length > 0 ? Arrays.copyOfRange(array, 1, array.length) : array; |
| 434 | + return new Array<>(result); |
| 435 | + } |
427 | 436 |
|
428 |
| - public Array<T> subArray(final int from) { |
429 |
| - final var from2 = Math.min(from, array.length); |
430 |
| - final var result = Arrays.copyOfRange(array, from2, array.length); |
431 |
| - return new Array<>(result); |
432 |
| - } |
| 437 | + /** @param from Negative value is supported */ |
| 438 | + public Array<T> subArray(final int from) { |
| 439 | + final var frm = from < 0 ? array.length - from : from; |
| 440 | + final var result = Arrays.copyOfRange(array, Math.min(frm, array.length), array.length); |
| 441 | + return new Array<>(result); |
| 442 | + } |
433 | 443 |
|
434 |
| - public List<T> toList() { |
435 |
| - return List.of(array); |
436 |
| - } |
| 444 | + public List<T> toList() { |
| 445 | + return List.of(array); |
| 446 | + } |
437 | 447 |
|
438 |
| - public Stream<T> stream() { |
439 |
| - return Stream.of(array); |
440 |
| - } |
| 448 | + public Stream<T> stream() { |
| 449 | + return Stream.of(array); |
| 450 | + } |
441 | 451 |
|
442 |
| - @SuppressWarnings("unchecked") |
443 |
| - public T[] toArray() { |
444 |
| - final var type = array.getClass().getComponentType(); |
445 |
| - final var result = (T[]) java.lang.reflect.Array.newInstance(type, array.length); |
446 |
| - System.arraycopy(array, 0, result, 0, array.length); |
447 |
| - return result; |
448 |
| - } |
| 452 | + @SuppressWarnings("unchecked") |
| 453 | + public T[] toArray() { |
| 454 | + final var type = array.getClass().getComponentType(); |
| 455 | + final var result = (T[]) java.lang.reflect.Array.newInstance(type, array.length); |
| 456 | + System.arraycopy(array, 0, result, 0, array.length); |
| 457 | + return result; |
| 458 | + } |
449 | 459 |
|
450 |
| - public boolean isEmpty() { |
451 |
| - return array.length == 0; |
452 |
| - } |
| 460 | + public boolean isEmpty() { |
| 461 | + return array.length == 0; |
| 462 | + } |
453 | 463 |
|
454 |
| - public boolean hasLength() { |
455 |
| - return array.length > 0; |
456 |
| - } |
| 464 | + public boolean hasLength() { |
| 465 | + return array.length > 0; |
| 466 | + } |
457 | 467 |
|
458 |
| - public int size() { |
459 |
| - return array.length; |
460 |
| - } |
| 468 | + public int size() { |
| 469 | + return array.length; |
| 470 | + } |
461 | 471 |
|
462 |
| - @Override |
463 |
| - public String toString() { |
464 |
| - return List.of(array).toString(); |
465 |
| - } |
| 472 | + @Override |
| 473 | + public int hashCode() { |
| 474 | + return Arrays.hashCode(array); |
| 475 | + } |
466 | 476 |
|
467 |
| - @SuppressWarnings("unchecked") |
468 |
| - public static <T> Array<T> of(T... chars) { |
469 |
| - return new Array<>(chars); |
470 |
| - } |
| 477 | + @Override |
| 478 | + public boolean equals(@NotNull final Object obj) { |
| 479 | + return (obj instanceof Array objArray) && Arrays.equals(array, objArray.array); |
| 480 | + } |
| 481 | + |
| 482 | + @Override |
| 483 | + public String toString() { |
| 484 | + return List.of(array).toString(); |
471 | 485 | }
|
472 | 486 |
|
| 487 | + @SuppressWarnings("unchecked") |
| 488 | + public static <T> Array<T> of(T... chars) { |
| 489 | + return new Array<>(chars); |
| 490 | + } |
| 491 | + } |
| 492 | + |
473 | 493 | /** JSON parser. The {@code array} type is not supported. */
|
474 | 494 | public static class Json {
|
475 | 495 | static final Pattern keyPattern = Pattern.compile("\"(.*?)\"\\s*:\\s*(\".*?\"|\\d+\\.?\\d*|true|false|null|\\{.*?\\})");
|
|
0 commit comments