diff options
author | Marko Zajc <marko@zajc.eu.org> | 2024-03-19 01:45:53 +0100 |
---|---|---|
committer | Marko Zajc <marko@zajc.eu.org> | 2024-03-19 02:21:27 +0100 |
commit | 54cb615a682e1a5a886ec8c93fca8cfe8c1a8bc3 (patch) | |
tree | aa3746a058991d4bfb438390bfb104b130fc940b /src/main/java/zajc/gogarchiver/util |
Diffstat (limited to 'src/main/java/zajc/gogarchiver/util')
-rw-r--r-- | src/main/java/zajc/gogarchiver/util/LazyValue.java | 42 | ||||
-rw-r--r-- | src/main/java/zajc/gogarchiver/util/Utilities.java | 80 |
2 files changed, 122 insertions, 0 deletions
diff --git a/src/main/java/zajc/gogarchiver/util/LazyValue.java b/src/main/java/zajc/gogarchiver/util/LazyValue.java new file mode 100644 index 0000000..118193f --- /dev/null +++ b/src/main/java/zajc/gogarchiver/util/LazyValue.java | |||
@@ -0,0 +1,42 @@ | |||
1 | //SPDX-License-Identifier: GPL-3.0 | ||
2 | /* | ||
3 | * gogarchiver-ng, an archival tool for GOG.com | ||
4 | * Copyright (C) 2024 Marko Zajc | ||
5 | * | ||
6 | * This program is free software: you can redistribute it and/or modify it under the | ||
7 | * terms of the GNU General Public License as published by the Free Software | ||
8 | * Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
12 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License along with this | ||
15 | * program. If not, see <https://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | package zajc.gogarchiver.util; | ||
18 | |||
19 | import java.util.function.Supplier; | ||
20 | |||
21 | import javax.annotation.*; | ||
22 | |||
23 | public class LazyValue<T> { | ||
24 | |||
25 | @Nullable private volatile T value; | ||
26 | |||
27 | public T get(@Nonnull Supplier<T> generator) { | ||
28 | if (this.value != null) | ||
29 | return this.value; | ||
30 | synchronized (this) { | ||
31 | if (this.value != null) // double checked locking | ||
32 | return this.value; | ||
33 | else | ||
34 | return this.value = generator.get(); | ||
35 | } | ||
36 | } | ||
37 | |||
38 | public synchronized void unset() { | ||
39 | this.value = null; | ||
40 | } | ||
41 | |||
42 | } | ||
diff --git a/src/main/java/zajc/gogarchiver/util/Utilities.java b/src/main/java/zajc/gogarchiver/util/Utilities.java new file mode 100644 index 0000000..d039ab1 --- /dev/null +++ b/src/main/java/zajc/gogarchiver/util/Utilities.java | |||
@@ -0,0 +1,80 @@ | |||
1 | //SPDX-License-Identifier: GPL-3.0 | ||
2 | /* | ||
3 | * gogarchiver-ng, an archival tool for GOG.com | ||
4 | * Copyright (C) 2024 Marko Zajc | ||
5 | * | ||
6 | * This program is free software: you can redistribute it and/or modify it under the | ||
7 | * terms of the GNU General Public License as published by the Free Software | ||
8 | * Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
12 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License along with this | ||
15 | * program. If not, see <https://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | package zajc.gogarchiver.util; | ||
18 | |||
19 | import static java.lang.System.err; | ||
20 | import static picocli.CommandLine.Help.defaultColorScheme; | ||
21 | |||
22 | import java.util.stream.*; | ||
23 | |||
24 | import javax.annotation.*; | ||
25 | |||
26 | import kong.unirest.HttpResponse; | ||
27 | import kong.unirest.json.JSONArray; | ||
28 | import picocli.CommandLine.Help.*; | ||
29 | |||
30 | public class Utilities { | ||
31 | |||
32 | private static boolean enableVerbose = false; | ||
33 | private static ColorScheme colorScheme; | ||
34 | |||
35 | public static void setVerbose(boolean verbose) { | ||
36 | enableVerbose = verbose; | ||
37 | } | ||
38 | |||
39 | public static void setColorMode(@Nonnull Ansi ansi) { | ||
40 | colorScheme = defaultColorScheme(ansi); | ||
41 | } | ||
42 | |||
43 | public static void println(@Nullable Object text) { | ||
44 | err.println(colorScheme.text(String.valueOf(text))); | ||
45 | } | ||
46 | |||
47 | public static void printf(@Nonnull String format, @Nonnull Object... args) { | ||
48 | err.print(colorScheme.text(format.formatted(args))); | ||
49 | } | ||
50 | |||
51 | public static void warn(@Nonnull String text, @Nonnull Object... args) { | ||
52 | println("@|yellow [W]|@ " + text.formatted(args)); | ||
53 | } | ||
54 | |||
55 | public static void verbose(@Nonnull String text, @Nonnull Object... args) { | ||
56 | if (enableVerbose) | ||
57 | println("@|faint [V]|@ " + text.formatted(args)); | ||
58 | } | ||
59 | |||
60 | public static <T> HttpResponse<T> checkResponse(String url, HttpResponse<T> resp) { | ||
61 | if (!resp.isSuccess()) | ||
62 | throw new RuntimeException("Got a bad HTTP response on %s: %d %s".formatted(url, resp.getStatus(), | ||
63 | resp.getStatusText())); | ||
64 | |||
65 | return resp; | ||
66 | } | ||
67 | |||
68 | @Nonnull | ||
69 | @SuppressWarnings("null") | ||
70 | public static Stream<Object> stream(JSONArray array) { | ||
71 | return StreamSupport.stream(array.spliterator(), false); | ||
72 | } | ||
73 | |||
74 | public static void cursorUp() { | ||
75 | err.print("\u001b[1A"); | ||
76 | } | ||
77 | |||
78 | private Utilities() {} | ||
79 | |||
80 | } | ||