Skip to content

Commit c99f277

Browse files
committed
Code cleaning
1 parent e85177b commit c99f277

File tree

7 files changed

+256
-81
lines changed

7 files changed

+256
-81
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,35 @@
33
The project contains several classes with different uses that can be run (without further dependencies)
44
as a script (without prior compilation).
55

6-
## Java Script Descriptions
6+
## Java Script Class Descriptions
77

88
* [DirectoryBookmarks](docs/DirectoryBookmarks.md) - A tool for switching directories in the character terminal using bookmarks.
99
* [Mp3PlayerGenerator](docs/Mp3PlayerGenerator.md) - The script builds a music player for your music files in HTML format.
1010
* [SqlParamBuilder](docs/SqlParamBuilder.md) - Script template for working with relational database using JDBC.
1111
* [PPUtils](docs/PPUtils.md) - Services called from the command line for general use (find, grep, json parser, base64 utils and more).
1212

13+
## Example of use
14+
15+
If the project is compiled into a JAR file, the following command can be used:
16+
17+
```bash
18+
java -jar target/script-0.0.1-SNAPSHOT.jar PPUtils datetime
19+
```
20+
21+
The command prints a **date** with **time** in a format suitable for use in file names.
22+
23+
> 2024-03-23T1142
24+
25+
Where `PPUtils` is the name of the Java class that provides the service and `datetime` is an argument.
26+
However, because the `PPUtils` class can be run as a script (without prior compilation), you can use a next simple command.
27+
28+
```bash
29+
java src/main/java/net/ponec/script/PPUtils.java datetime
30+
```
31+
32+
The script can be run from any directory with an absolute or relative file path.
33+
34+
1335
## License
1436

1537
[Apache License](LICENSE), Version 2.0, [Pavel Ponec](https://github.com/pponec/Mp3PlayerGenerator/)

docs/PPUtils.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,17 @@
22

33
Services called from the command line for general use.
44

5-
For more information see a source code: [SqlExecutor.java](../src/main/java/net/ponec/script/PPUtils.java) .
5+
## Tools and examples of use
6+
7+
* `PPUtils find [regExpContent] regExpFile` - find readable files by regular expressions, partial compliance is assessed,
8+
* `PPUtils grep regExpContent regExpFile` - find readable file rows by a regular expression.
9+
* `PPUtils date` - prints a date by ISO format, for example: "2023-12-31"
10+
* `PPUtils time` - prints hours and time, for example "2359"
11+
* `PPUtils datetime` - prints datetime format "2023-12-31T2359"
12+
* `PPUtils date-iso` - prints datetime by ISO format, eg: "2023-12-31T23:59:59.999"
13+
* `PPUtils date-format "yyyy-MM-dd'T'HH:mm:ss.SSS"` - prints a time by a custom format
14+
* `PPUtils base64encode "file.bin"` - encode any (binary) file.
15+
* `PPUtils base64decode "file.base64"` - decode base64 encoded file (result removes extension)
16+
* `PPUtils key json ` - get a value by the (composite) key, for example: `"a.b.c"`
17+
18+
For more information see a source code: [PPUtils.java](../src/main/java/net/ponec/script/PPUtils.java) .

pom.xml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,17 @@
104104

105105
<!-- Maven Assembly Plugin -->
106106
<plugin>
107-
<artifactId>maven-assembly-plugin</artifactId>
107+
<groupId>org.apache.maven.plugins</groupId>
108+
<artifactId>maven-jar-plugin</artifactId>
108109
<configuration>
109110
<archive>
110111
<manifest>
111-
<mainClass>cz.ponec.script.Main</mainClass>
112+
<addClasspath>true</addClasspath>
113+
<classpathPrefix>libs/</classpathPrefix>
114+
<mainClass>net.ponec.script.Main</mainClass>
112115
</manifest>
113116
</archive>
114-
<descriptorRefs>
115-
<descriptorRef>jar-with-dependencies</descriptorRef>
116-
</descriptorRefs>
117117
</configuration>
118-
<executions>
119-
<execution>
120-
<id>make-assembly</id>
121-
<phase>package</phase>
122-
<goals>
123-
<goal>single</goal>
124-
</goals>
125-
</execution>
126-
</executions>
127118
</plugin>
128119
</plugins>
129120
</build>

src/main/java/net/ponec/script/DirectoryBookmarks.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ private void removePackage(Path fullJavaClass) throws IOException {
503503
/** The immutable Array wrapper (from the Ujorm framework) */
504504
public record Array<T>(T[] array) {
505505

506-
/** Negative index is supported */
506+
/** Negative index is supported */
507507
public Optional<T> get(final int i) {
508508
final var j = i >= 0 ? i : array.length + i;
509509
return Optional.ofNullable(j >= 0 && j < array.length ? array[j] : null);

src/main/java/net/ponec/script/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public static void main(String[] arguments) throws NoSuchMethodException, Invoca
2222
.findFirst()
2323
.orElseThrow(Main::illegalArgument);
2424
var mainMethod = clazz.getMethod("main", String[].class);
25-
mainMethod.invoke(null, args.subArray(1).toArray());
25+
var reflectionArgs = new Object[] { args.subArray(1).toArray() };
26+
mainMethod.invoke(null, reflectionArgs);
2627
}
2728

2829
private static IllegalArgumentException illegalArgument() {

src/main/java/net/ponec/script/PPUtils.java

Lines changed: 83 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package net.ponec.script;
2020

21+
import org.jetbrains.annotations.NotNull;
2122
import javax.tools.ToolProvider;
2223
import java.io.*;
2324
import java.net.URI;
@@ -37,13 +38,16 @@
3738
/**
3839
* Usage and examples:
3940
* <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>
4142
* <li>{@code java PPUtils grep main.*String PPUtils.java } - find readable file rows by a regular expression.</li>
4243
* <li>{@code java PPUtils date} - prints a date by ISO format, for example: "2023-12-31"</li>
4344
* <li>{@code java PPUtils time} - prints hours and time, for example "2359"</li>
4445
* <li>{@code java PPUtils datetime} - prints datetime format "2023-12-31T2359"</li>
4546
* <li>{@code java PPUtils date-iso} - prints datetime by ISO format, eg: "2023-12-31T23:59:59.999"</li>
4647
* <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>
4751
* </ul>
4852
*/
4953
public final class PPUtils {
@@ -103,7 +107,7 @@ void start(Array<String> args) throws Exception {
103107
out.println(currentDate("HHmm"));
104108
}
105109
case "datetime" -> {
106-
out.println(currentDate("yyyy-MM'T'HHmm"));
110+
out.println(currentDate("yyyy-MM-dd'T'HHmm"));
107111
}
108112
case "date-iso" -> {
109113
out.println(currentDate(dateIsoFormat));
@@ -391,85 +395,101 @@ private void removePackage(Path fullJavaClass) throws IOException {
391395

392396

393397
/** 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+
}
395404

396405
/** 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+
}
401410

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+
}
409418

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+
}
414423

415-
public Optional<T> getFirst() {
416-
return get(0);
417-
}
424+
public Optional<T> getFirst() {
425+
return get(0);
426+
}
418427

419-
public Optional<T> getLast() {
420-
return get(-1);
421-
}
428+
public Optional<T> getLast() {
429+
return get(-1);
430+
}
422431

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+
}
427436

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+
}
433443

434-
public List<T> toList() {
435-
return List.of(array);
436-
}
444+
public List<T> toList() {
445+
return List.of(array);
446+
}
437447

438-
public Stream<T> stream() {
439-
return Stream.of(array);
440-
}
448+
public Stream<T> stream() {
449+
return Stream.of(array);
450+
}
441451

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+
}
449459

450-
public boolean isEmpty() {
451-
return array.length == 0;
452-
}
460+
public boolean isEmpty() {
461+
return array.length == 0;
462+
}
453463

454-
public boolean hasLength() {
455-
return array.length > 0;
456-
}
464+
public boolean hasLength() {
465+
return array.length > 0;
466+
}
457467

458-
public int size() {
459-
return array.length;
460-
}
468+
public int size() {
469+
return array.length;
470+
}
461471

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+
}
466476

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();
471485
}
472486

487+
@SuppressWarnings("unchecked")
488+
public static <T> Array<T> of(T... chars) {
489+
return new Array<>(chars);
490+
}
491+
}
492+
473493
/** JSON parser. The {@code array} type is not supported. */
474494
public static class Json {
475495
static final Pattern keyPattern = Pattern.compile("\"(.*?)\"\\s*:\\s*(\".*?\"|\\d+\\.?\\d*|true|false|null|\\{.*?\\})");

0 commit comments

Comments
 (0)