Skip to content

Commit e62f474

Browse files
committed
Code cleaning
1 parent dcf8e06 commit e62f474

File tree

5 files changed

+89
-101
lines changed

5 files changed

+89
-101
lines changed

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public static void main(String[] arguments) throws Exception {
5555
System.err, enforcedLinux, false).start(args);
5656
}
5757

58-
protected DirectoryBookmarks(File storeName,
59-
PrintStream out,
60-
PrintStream err,
61-
boolean enforcedLinux,
62-
boolean exitByException) {
58+
DirectoryBookmarks(File storeName,
59+
PrintStream out,
60+
PrintStream err,
61+
boolean enforcedLinux,
62+
boolean exitByException) {
6363
this.storeName = storeName;
6464
this.out = out;
6565
this.err = err;
@@ -474,7 +474,7 @@ private String[] getAllClassFiles(Class<?> mainClass) {
474474
result.add(mainClass.getSimpleName() + suffix);
475475
Stream.of(mainClass.getDeclaredClasses())
476476
.map(c -> mainClass.getSimpleName() + '$' + c.getSimpleName() + suffix)
477-
.forEach(c -> result.add(c));
477+
.forEach(result::add);
478478
return result.toArray(String[]::new);
479479
}
480480

@@ -501,14 +501,9 @@ private void removePackage(Path fullJavaClass) throws IOException {
501501
}
502502

503503
/** The immutable Array wrapper (from the Ujorm framework) */
504-
static class Array<T> {
505-
protected final T[] array;
504+
public record Array<T>(T[] array) {
506505

507-
public Array(T[] array) {
508-
this.array = array;
509-
}
510-
511-
/** Negative index is supported */
506+
/** Negative index is supported */
512507
public Optional<T> get(final int i) {
513508
final var j = i >= 0 ? i : array.length + i;
514509
return Optional.ofNullable(j >= 0 && j < array.length ? array[j] : null);
@@ -556,7 +551,7 @@ public Stream<T> stream() {
556551
@SuppressWarnings("unchecked")
557552
public T[] toArray() {
558553
final var type = array.getClass().getComponentType();
559-
final var result = java.lang.reflect.Array.newInstance(type, array.length);
554+
final var result = java.lang.reflect.Array.newInstance(type, array.length);
560555
System.arraycopy(array, 0, result, 0, array.length);
561556
return (T[]) result;
562557
}
@@ -580,7 +575,7 @@ public String toString() {
580575

581576
@SuppressWarnings("unchecked")
582577
public static <T> Array<T> of(T... chars) {
583-
return new Array<T>(chars);
578+
return new Array<>(chars);
584579
}
585580
}
586581
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/** Common runner */
88
public class Main {
99

10-
private static final Set<Class> classes = Set.of(
10+
private static final Set<Class<?>> classes = Set.of(
1111
DirectoryBookmarks.class,
1212
Mp3PlayerGenerator.class,
1313
PPUtils.class,
@@ -20,14 +20,14 @@ public static void main(String[] arguments) throws NoSuchMethodException, Invoca
2020
var clazz = classes.stream()
2121
.filter(t -> mainClassName.equals(t.getSimpleName()))
2222
.findFirst()
23-
.orElseThrow(() -> illegalArgument());
23+
.orElseThrow(Main::illegalArgument);
2424
var mainMethod = clazz.getMethod("main", String[].class);
2525
mainMethod.invoke(null, args.subArray(1).toArray());
2626
}
2727

2828
private static IllegalArgumentException illegalArgument() {
2929
var message = "Use some of the class %s".formatted(
30-
classes.stream().map(t -> t.getSimpleName()).toList());
30+
classes.stream().map(Class::getSimpleName).toList());
3131
return new IllegalArgumentException(message);
3232
}
3333

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public static void main(String[] args) throws Exception {
3535
}
3636

3737
void printHelpAndExit() {
38-
System.out.println("Script '%s' v%s (%s)".formatted(appName, appVersion, homePage));
39-
System.out.println("Usage version: %s".formatted(appName));
38+
System.out.printf("Script '%s' v%s (%s)%n", appName, appVersion, homePage);
39+
System.out.printf("Usage version: %s%n", appName);
4040
System.exit(1);
4141
}
4242

@@ -65,7 +65,7 @@ List<String> getSoundFilesSorted() {
6565
.filter(file -> file.isFile())
6666
.map (file -> file.getName())
6767
.filter(file -> filePattern.matcher(file).find())
68-
.sorted(Comparator.comparing(file -> removeDiacritics(file)))
68+
.sorted(Comparator.comparing(this::removeDiacritics))
6969
.toList();
7070
}
7171

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

Lines changed: 72 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public final class PPUtils {
5858

5959
private final String dateIsoFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
6060

61-
protected static final String grepSeparator = ":";
61+
private static final String grepSeparator = ":";
6262

63-
protected static final boolean sortDirectoryLast = true;
63+
private static final boolean sortDirectoryLast = true;
6464

6565
private final String sourceUrl = "https://raw.githubusercontent.com/pponec/DirectoryBookmarks/%s/utils/%s.java"
6666
.formatted(!true ? "main" : "development", appName);
@@ -84,18 +84,16 @@ void start(Array<String> args) throws Exception {
8484
final var file = args.get(1).map(Path::of).get();
8585
final var printLine = args.get(2).orElse("").equals("--print");
8686
final var subArgs = args.subArray(2 + (printLine ? 1 : 0 ));
87-
final var bodyPattern = subArgs.get(-2).map(t -> Pattern.compile(t)).orElse(null);
88-
final var filePattern = subArgs.get(-1).map(t -> Pattern.compile(t)).orElse(null);
87+
final var bodyPattern = subArgs.get(-2).map(Pattern::compile).orElse(null);
88+
final var filePattern = subArgs.get(-1).map(Pattern::compile).orElse(null);
8989
new FinderUtilitiy(bodyPattern, filePattern, enforcedLinux, out)
9090
.findFiles(file, printLine && bodyPattern != null);
9191
}
9292
case "grep" -> {
9393
if (args.size() > 3) {
94-
final var bodyPattern = args.get(2).map(t -> Pattern.compile(t)).orElse(null); // Pattern.CASE_INSENSITIVE);
94+
final var bodyPattern = args.get(2).map(Pattern::compile).orElse(null); // Pattern.CASE_INSENSITIVE);
9595
final var pathFinder = new FinderUtilitiy(bodyPattern, null, enforcedLinux, out);
96-
args.stream().skip(3).forEach(file -> {
97-
pathFinder.grep(Path.of(file), true);
98-
});
96+
args.stream().skip(3).forEach(file -> pathFinder.grep(Path.of(file), true));
9997
}
10098
}
10199
case "date" -> {
@@ -186,7 +184,7 @@ public void convertBase64(Path inpFile, boolean encode) throws IOException {
186184
os.write(b2);
187185
}
188186
}
189-
out.println("Converted file has a name: '%s'".formatted(outFile));
187+
out.printf("Converted file has a name: '%s'%n", outFile);
190188
}
191189
}
192190

@@ -245,7 +243,7 @@ public boolean grep(Path file, boolean printLine) {
245243
}
246244

247245
/** Method supports a GitBash shell. */
248-
protected PrintStream printFileName(Path path) {
246+
private PrintStream printFileName(Path path) {
249247
if (enforcedLinux) {
250248
out.print(path.toString().replace('\\', '/'));
251249
} else {
@@ -264,7 +262,7 @@ public int compare(final Path p1, final Path p2) {
264262
if (d1 != d2) {
265263
return d1 ? 1 : -1;
266264
} else {
267-
return p1.getFileName().toString().compareTo(p1.getFileName().toString());
265+
return p1.getFileName().toString().compareTo(p2.getFileName().toString());
268266
}
269267
}
270268
}
@@ -276,7 +274,7 @@ class Utilities {
276274
/** Compile the script and build it to the executable JAR file */
277275
private void compile() throws Exception {
278276
if (isJar()) {
279-
out.printf("Use the statement rather: java %s.java c %s".formatted(appName));
277+
out.printf("Use the statement rather: java %s.java c %s", appName);
280278
System.exit(1);
281279
}
282280

@@ -365,7 +363,7 @@ private String[] getAllClassFiles(Class<?> mainClass) {
365363
result.add(mainClass.getSimpleName() + suffix);
366364
Stream.of(mainClass.getDeclaredClasses())
367365
.map(c -> mainClass.getSimpleName() + '$' + c.getSimpleName() + suffix)
368-
.forEach(c -> result.add(c));
366+
.forEach(result::add);
369367
return result.toArray(String[]::new);
370368
}
371369

@@ -393,89 +391,84 @@ private void removePackage(Path fullJavaClass) throws IOException {
393391

394392

395393
/** The immutable Array wrapper with utilities (from the Ujorm framework) */
396-
static class Array<T> {
397-
protected final T[] array;
398-
399-
public Array(T[] array) {
400-
this.array = array;
401-
}
394+
record Array<T>(T[] array) {
402395

403396
/** Negative index is supported */
404-
public Optional<T> get(final int i) {
405-
final var j = i >= 0 ? i : array.length + i;
406-
return Optional.ofNullable(j >= 0 && j < array.length ? array[j] : null);
407-
}
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+
}
408401

409-
/** Add new items to the new Array */
410-
@SuppressWarnings("unchecked")
411-
public Array<T> add(final T... toAdd) {
412-
final var result = Arrays.copyOf(array, array.length + toAdd.length);
413-
System.arraycopy(toAdd, 0, result, array.length, toAdd.length);
414-
return new Array<>(result);
415-
}
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+
}
416409

417-
/** Negative index is supported */
418-
public T getItem(final int i) {
419-
return array[i >= 0 ? i : array.length + i];
420-
}
410+
/** Negative index is supported */
411+
public T getItem(final int i) {
412+
return array[i >= 0 ? i : array.length + i];
413+
}
421414

422-
public Optional<T> getFirst() {
423-
return get(0);
424-
}
415+
public Optional<T> getFirst() {
416+
return get(0);
417+
}
425418

426-
public Optional<T> getLast() {
427-
return get(-1);
428-
}
419+
public Optional<T> getLast() {
420+
return get(-1);
421+
}
429422

430-
public Array<T> removeFirst() {
431-
final var result = array.length > 0 ? Arrays.copyOfRange(array, 1, array.length) : array;
432-
return new Array<>(result);
433-
}
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+
}
434427

435-
public Array<T> subArray(final int from) {
436-
final var from2 = Math.min(from, array.length);
437-
final var result = Arrays.copyOfRange(array, from2, array.length);
438-
return new Array<>(result);
439-
}
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+
}
440433

441-
public List<T> toList() {
442-
return List.of(array);
443-
}
434+
public List<T> toList() {
435+
return List.of(array);
436+
}
444437

445-
public Stream<T> stream() {
446-
return Stream.of(array);
447-
}
438+
public Stream<T> stream() {
439+
return Stream.of(array);
440+
}
448441

449-
@SuppressWarnings("unchecked")
450-
public T[] toArray() {
451-
final var type = array.getClass().getComponentType();
452-
final var result = java.lang.reflect.Array.newInstance(type, array.length);
453-
System.arraycopy(array, 0, result, 0, array.length);
454-
return (T[]) result;
455-
}
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+
}
456449

457-
public boolean isEmpty() {
458-
return array.length == 0;
459-
}
450+
public boolean isEmpty() {
451+
return array.length == 0;
452+
}
460453

461-
public boolean hasLength() {
462-
return array.length > 0;
463-
}
454+
public boolean hasLength() {
455+
return array.length > 0;
456+
}
464457

465-
public int size() {
466-
return array.length;
467-
}
458+
public int size() {
459+
return array.length;
460+
}
468461

469-
@Override
470-
public String toString() {
471-
return List.of(array).toString();
472-
}
462+
@Override
463+
public String toString() {
464+
return List.of(array).toString();
465+
}
473466

474-
@SuppressWarnings("unchecked")
475-
public static <T> Array<T> of(T... chars) {
476-
return new Array<T>(chars);
467+
@SuppressWarnings("unchecked")
468+
public static <T> Array<T> of(T... chars) {
469+
return new Array<>(chars);
470+
}
477471
}
478-
}
479472

480473
/** JSON parser. The {@code array} type is not supported. */
481474
public static class Json {

src/main/java/net/ponec/script/SqlExecutorKt.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Environment: Java 17+ with JDBC driver com.h2database:h2:2.2.224 are required.
66
* Licence: Apache License, Version 2.0, Pavel Ponec, https://github.com/pponec/DirectoryBookmarks
77
*/
8-
package net.ponec.script;
8+
package net.ponec.script
99

1010
import java.io.Closeable
1111
import java.io.IOException

0 commit comments

Comments
 (0)