Skip to content

Commit 485e373

Browse files
committed
Refactor File API
1 parent 849f388 commit 485e373

File tree

15 files changed

+117
-1039
lines changed

15 files changed

+117
-1039
lines changed

files/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ tasks.compileJava {
1515
}
1616

1717
group = "net.thenextlvl.core"
18-
version = "3.0.1"
18+
version = "4.0.0-pre1"
1919

2020
repositories {
2121
mavenCentral()

files/src/main/java/core/file/FileIO.java

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package core.file;
22

3-
import core.io.IO;
43
import org.jspecify.annotations.NonNull;
54
import org.jspecify.annotations.Nullable;
65

76
import java.io.IOException;
87
import java.nio.charset.Charset;
98
import java.nio.charset.StandardCharsets;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
1011
import java.nio.file.attribute.FileAttribute;
11-
import java.util.Objects;
1212

1313
/**
1414
* Abstract class for performing file input and output operations.
1515
*
1616
* @param <R> the type of the root object
1717
*/
1818
public abstract class FileIO<R> {
19-
private final IO io;
19+
private final Path file;
2020
private Charset charset;
2121
private @Nullable R root;
2222

@@ -25,45 +25,45 @@ public abstract class FileIO<R> {
2525
/**
2626
* Construct a new FileIO providing a file, charset, and default root object
2727
*
28-
* @param io the file to read from and write to
28+
* @param file the file to read from and write to
2929
* @param charset the charset to use for read and write operations
3030
* @param root the default root object
3131
*/
3232

33-
protected FileIO(@NonNull IO io, @NonNull Charset charset, @Nullable R root) {
34-
this.io = io;
33+
protected FileIO(@NonNull Path file, @NonNull Charset charset, @Nullable R root) {
34+
this.file = file;
3535
this.charset = charset;
3636
this.root = root;
3737
}
3838

3939
/**
4040
* Construct a new FileIO providing a file and charset
4141
*
42-
* @param io the file to read from and write to
42+
* @param file the file to read from and write to
4343
* @param charset the charset to use for read and write operations
4444
*/
4545

46-
protected FileIO(@NonNull IO io, @NonNull Charset charset) {
47-
this(io, charset, null);
46+
protected FileIO(@NonNull Path file, @NonNull Charset charset) {
47+
this(file, charset, null);
4848
}
4949

5050
/**
5151
* Construct a new FileIO providing a file and default root object
5252
*
53-
* @param io the file to read from and write to
53+
* @param file the file to read from and write to
5454
* @param root the default root object
5555
*/
56-
protected FileIO(@NonNull IO io, R root) {
57-
this(io, StandardCharsets.UTF_8, root);
56+
protected FileIO(@NonNull Path file, @Nullable R root) {
57+
this(file, StandardCharsets.UTF_8, root);
5858
}
5959

6060
/**
6161
* Construct a new FileIO providing a file
6262
*
63-
* @param io the file to read from and write to
63+
* @param file the file to read from and write to
6464
*/
65-
protected FileIO(@NonNull IO io) {
66-
this(io, (R) null);
65+
protected FileIO(@NonNull Path file) {
66+
this(file, (R) null);
6767
}
6868

6969
/**
@@ -122,7 +122,7 @@ public R getRoot() {
122122
* @return the own instance
123123
*/
124124
public @NonNull FileIO<R> saveIfAbsent() {
125-
return getIO().exists() ? this : save();
125+
return Files.isRegularFile(file) ? this : save();
126126
}
127127

128128
/**
@@ -132,16 +132,16 @@ public R getRoot() {
132132
* @throws IOException if an I/O error occurs
133133
*/
134134
public boolean delete() throws IOException {
135-
return getIO().delete();
135+
return Files.deleteIfExists(file);
136136
}
137137

138138
/**
139-
* Retrieves the IO instance associated with this FileIO.
139+
* Retrieves the file instance associated with this FileIO.
140140
*
141-
* @return the IO instance that provides input and output operations.
141+
* @return the file instance that provides input and output operations.
142142
*/
143-
public IO getIO() {
144-
return io;
143+
public Path getFile() {
144+
return file;
145145
}
146146

147147
/**
@@ -163,26 +163,4 @@ public FileIO<R> setCharset(Charset charset) {
163163
this.charset = charset;
164164
return this;
165165
}
166-
167-
@Override
168-
public boolean equals(Object o) {
169-
if (o == null || getClass() != o.getClass()) return false;
170-
FileIO<?> fileIO = (FileIO<?>) o;
171-
return Objects.equals(io, fileIO.io) && Objects.equals(charset, fileIO.charset) && Objects.equals(root, fileIO.root);
172-
}
173-
174-
@Override
175-
public int hashCode() {
176-
return Objects.hash(io, charset, root);
177-
}
178-
179-
@Override
180-
public String toString() {
181-
return getClass().getSimpleName() + "{" +
182-
"io=" + io +
183-
", charset=" + charset +
184-
", root=" + root +
185-
", loaded=" + loaded +
186-
'}';
187-
}
188166
}

files/src/main/java/core/file/format/GsonFile.java

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.google.gson.stream.JsonReader;
99
import core.file.FileIO;
1010
import core.file.Validatable;
11-
import core.io.IO;
1211
import org.jspecify.annotations.NullMarked;
1312
import org.jspecify.annotations.Nullable;
1413

@@ -17,6 +16,8 @@
1716
import java.io.InputStreamReader;
1817
import java.io.OutputStreamWriter;
1918
import java.lang.reflect.Type;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
2021
import java.nio.file.attribute.FileAttribute;
2122
import java.util.Objects;
2223

@@ -45,13 +46,13 @@ public class GsonFile<R> extends FileIO<R> implements Validatable<R> {
4546
/**
4647
* Construct a new GsonFile providing a file, default root object, type, and gson instance
4748
*
48-
* @param io the file to read from and write to
49+
* @param file the file to read from and write to
4950
* @param root the default root object
5051
* @param type the root type
5152
* @param gson the gson instance
5253
*/
53-
public GsonFile(IO io, @Nullable R root, Type type, Gson gson) {
54-
super(io, root);
54+
public GsonFile(Path file, @Nullable R root, Type type, Gson gson) {
55+
super(file, root);
5556
this.defaultRoot = root;
5657
this.type = type;
5758
this.gson = gson;
@@ -60,57 +61,57 @@ public GsonFile(IO io, @Nullable R root, Type type, Gson gson) {
6061
/**
6162
* Construct a new GsonFile providing a file, type and gson instance
6263
*
63-
* @param io the file to read from and write to
64+
* @param file the file to read from and write to
6465
* @param type the root type
6566
* @param gson the gson instance
6667
*/
67-
public GsonFile(IO io, Type type, Gson gson) {
68-
this(io, null, type, gson);
68+
public GsonFile(Path file, Type type, Gson gson) {
69+
this(file, null, type, gson);
6970
}
7071

7172
/**
7273
* Construct a new GsonFile providing a file, default root object, type-token, and gson instance
7374
*
74-
* @param io the file to read from and write to
75+
* @param file the file to read from and write to
7576
* @param root the default root object
7677
* @param token the type-token
7778
* @param gson the gson instance
7879
*/
79-
public GsonFile(IO io, @Nullable R root, TypeToken<R> token, Gson gson) {
80-
this(io, root, token.getType(), gson);
80+
public GsonFile(Path file, @Nullable R root, TypeToken<R> token, Gson gson) {
81+
this(file, root, token.getType(), gson);
8182
}
8283

8384
/**
8485
* Construct a new GsonFile providing a file, type-token, and gson instance
8586
*
86-
* @param io the file to read from and write to
87+
* @param file the file to read from and write to
8788
* @param token the type-token
8889
* @param gson the gson instance
8990
*/
90-
public GsonFile(IO io, TypeToken<R> token, Gson gson) {
91-
this(io, null, token, gson);
91+
public GsonFile(Path file, TypeToken<R> token, Gson gson) {
92+
this(file, null, token, gson);
9293
}
9394

9495
/**
9596
* Construct a new GsonFile providing a file, default root object and gson instance
9697
*
97-
* @param io the file to read from and write to
98+
* @param file the file to read from and write to
9899
* @param root the default root object
99100
* @param gson the gson instance
100101
*/
101-
public GsonFile(IO io, R root, Gson gson) {
102-
this(io, root, root.getClass(), gson);
102+
public GsonFile(Path file, R root, Gson gson) {
103+
this(file, root, root.getClass(), gson);
103104
}
104105

105106
/**
106107
* Construct a new GsonFile providing a file, default root object and type
107108
*
108-
* @param io the file to read from and write to
109+
* @param file the file to read from and write to
109110
* @param root the default root object
110111
* @param type the root type
111112
*/
112-
public GsonFile(IO io, @Nullable R root, Type type) {
113-
this(io, root, type, new GsonBuilder()
113+
public GsonFile(Path file, @Nullable R root, Type type) {
114+
this(file, root, type, new GsonBuilder()
114115
.disableHtmlEscaping()
115116
.setPrettyPrinting()
116117
.serializeNulls()
@@ -120,52 +121,52 @@ public GsonFile(IO io, @Nullable R root, Type type) {
120121
/**
121122
* Construct a new GsonFile providing a file and type
122123
*
123-
* @param io the file to read from and write to
124+
* @param file the file to read from and write to
124125
* @param type the root type
125126
*/
126-
public GsonFile(IO io, Type type) {
127-
this(io, null, type);
127+
public GsonFile(Path file, Type type) {
128+
this(file, null, type);
128129
}
129130

130131
/**
131132
* Construct a new GsonFile providing a file, default root object, and type-token
132133
*
133-
* @param io the file to read from and write to
134+
* @param file the file to read from and write to
134135
* @param root the default root object
135136
* @param token the type-token
136137
*/
137-
public GsonFile(IO io, @Nullable R root, TypeToken<R> token) {
138-
this(io, root, token.getType());
138+
public GsonFile(Path file, @Nullable R root, TypeToken<R> token) {
139+
this(file, root, token.getType());
139140
}
140141

141142
/**
142143
* Construct a new GsonFile providing a file and type-token
143144
*
144-
* @param io the file to read from and write to
145+
* @param file the file to read from and write to
145146
* @param token the type-token
146147
*/
147-
public GsonFile(IO io, TypeToken<R> token) {
148-
this(io, null, token);
148+
public GsonFile(Path file, TypeToken<R> token) {
149+
this(file, null, token);
149150
}
150151

151152
/**
152153
* Construct a new GsonFile providing a file and default root object
153154
*
154-
* @param io the file to read from and write to
155+
* @param file the file to read from and write to
155156
* @param root the default root object
156157
*/
157-
public GsonFile(IO io, R root) {
158-
this(io, root, root.getClass());
158+
public GsonFile(Path file, R root) {
159+
this(file, root, root.getClass());
159160
}
160161

161162
@Override
162163
protected @Nullable R load() {
163-
if (!getIO().exists()) return getRoot();
164+
if (!Files.isRegularFile(getFile())) return getRoot();
164165
try (var reader = new JsonReader(new InputStreamReader(
165-
getIO().inputStream(READ),
166+
Files.newInputStream(getFile(), READ),
166167
getCharset()
167168
))) {
168-
var root = getGson().<@Nullable R>fromJson(reader, getType());
169+
R root = getGson().fromJson(reader, getType());
169170
return root != null ? root : defaultRoot;
170171
} catch (IOException e) {
171172
throw new RuntimeException(e);
@@ -177,9 +178,9 @@ public GsonFile(IO io, R root) {
177178
public FileIO<R> save(FileAttribute<?>... attributes) {
178179
try {
179180
var root = getRoot();
180-
getIO().createParents(attributes);
181+
Files.createDirectories(getFile().getParent());
181182
try (var writer = new BufferedWriter(new OutputStreamWriter(
182-
getIO().outputStream(WRITE, CREATE, TRUNCATE_EXISTING),
183+
Files.newOutputStream(getFile(), WRITE, CREATE, TRUNCATE_EXISTING),
183184
getCharset()
184185
))) {
185186
getGson().toJson(root, getType(), writer);
@@ -192,7 +193,7 @@ public FileIO<R> save(FileAttribute<?>... attributes) {
192193

193194
@Override
194195
public FileIO<R> validate(Scope scope) {
195-
if (!getIO().exists()) return this;
196+
if (!Files.isRegularFile(getFile())) return this;
196197
var defaultTree = getGson().toJsonTree(defaultRoot, getType());
197198
var currentTree = getGson().toJsonTree(getRoot(), getType());
198199
var validatedTree = validate(scope, defaultTree, currentTree);
@@ -234,10 +235,10 @@ public int hashCode() {
234235
@Override
235236
public String toString() {
236237
return "GsonFile{" +
237-
"defaultRoot=" + defaultRoot +
238-
", type=" + type +
239-
", gson=" + gson +
240-
"} " + super.toString();
238+
"defaultRoot=" + defaultRoot +
239+
", type=" + type +
240+
", gson=" + gson +
241+
"} " + super.toString();
241242
}
242243

243244
private static JsonElement validate(Scope scope, JsonElement defaultTree, JsonElement currentTree) {

0 commit comments

Comments
 (0)