Skip to content

Commit 68491f1

Browse files
committed
Revert "Refactor File API"
This reverts commit 485e373.
1 parent 04b8546 commit 68491f1

File tree

15 files changed

+1039
-117
lines changed

15 files changed

+1039
-117
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 = "4.0.0-pre1"
18+
version = "3.0.1"
1919

2020
repositories {
2121
mavenCentral()

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

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

3+
import core.io.IO;
34
import org.jspecify.annotations.NonNull;
45
import org.jspecify.annotations.Nullable;
56

67
import java.io.IOException;
78
import java.nio.charset.Charset;
89
import java.nio.charset.StandardCharsets;
9-
import java.nio.file.Files;
10-
import java.nio.file.Path;
1110
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 Path file;
19+
private final IO io;
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 file the file to read from and write to
28+
* @param io 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 Path file, @NonNull Charset charset, @Nullable R root) {
34-
this.file = file;
33+
protected FileIO(@NonNull IO io, @NonNull Charset charset, @Nullable R root) {
34+
this.io = io;
3535
this.charset = charset;
3636
this.root = root;
3737
}
3838

3939
/**
4040
* Construct a new FileIO providing a file and charset
4141
*
42-
* @param file the file to read from and write to
42+
* @param io 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 Path file, @NonNull Charset charset) {
47-
this(file, charset, null);
46+
protected FileIO(@NonNull IO io, @NonNull Charset charset) {
47+
this(io, charset, null);
4848
}
4949

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

6060
/**
6161
* Construct a new FileIO providing a file
6262
*
63-
* @param file the file to read from and write to
63+
* @param io the file to read from and write to
6464
*/
65-
protected FileIO(@NonNull Path file) {
66-
this(file, (R) null);
65+
protected FileIO(@NonNull IO io) {
66+
this(io, (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 Files.isRegularFile(file) ? this : save();
125+
return getIO().exists() ? 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 Files.deleteIfExists(file);
135+
return getIO().delete();
136136
}
137137

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

147147
/**
@@ -163,4 +163,26 @@ 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+
}
166188
}

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

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

@@ -16,8 +17,6 @@
1617
import java.io.InputStreamReader;
1718
import java.io.OutputStreamWriter;
1819
import java.lang.reflect.Type;
19-
import java.nio.file.Files;
20-
import java.nio.file.Path;
2120
import java.nio.file.attribute.FileAttribute;
2221
import java.util.Objects;
2322

@@ -46,13 +45,13 @@ public class GsonFile<R> extends FileIO<R> implements Validatable<R> {
4645
/**
4746
* Construct a new GsonFile providing a file, default root object, type, and gson instance
4847
*
49-
* @param file the file to read from and write to
48+
* @param io the file to read from and write to
5049
* @param root the default root object
5150
* @param type the root type
5251
* @param gson the gson instance
5352
*/
54-
public GsonFile(Path file, @Nullable R root, Type type, Gson gson) {
55-
super(file, root);
53+
public GsonFile(IO io, @Nullable R root, Type type, Gson gson) {
54+
super(io, root);
5655
this.defaultRoot = root;
5756
this.type = type;
5857
this.gson = gson;
@@ -61,57 +60,57 @@ public GsonFile(Path file, @Nullable R root, Type type, Gson gson) {
6160
/**
6261
* Construct a new GsonFile providing a file, type and gson instance
6362
*
64-
* @param file the file to read from and write to
63+
* @param io the file to read from and write to
6564
* @param type the root type
6665
* @param gson the gson instance
6766
*/
68-
public GsonFile(Path file, Type type, Gson gson) {
69-
this(file, null, type, gson);
67+
public GsonFile(IO io, Type type, Gson gson) {
68+
this(io, null, type, gson);
7069
}
7170

7271
/**
7372
* Construct a new GsonFile providing a file, default root object, type-token, and gson instance
7473
*
75-
* @param file the file to read from and write to
74+
* @param io the file to read from and write to
7675
* @param root the default root object
7776
* @param token the type-token
7877
* @param gson the gson instance
7978
*/
80-
public GsonFile(Path file, @Nullable R root, TypeToken<R> token, Gson gson) {
81-
this(file, root, token.getType(), gson);
79+
public GsonFile(IO io, @Nullable R root, TypeToken<R> token, Gson gson) {
80+
this(io, root, token.getType(), gson);
8281
}
8382

8483
/**
8584
* Construct a new GsonFile providing a file, type-token, and gson instance
8685
*
87-
* @param file the file to read from and write to
86+
* @param io the file to read from and write to
8887
* @param token the type-token
8988
* @param gson the gson instance
9089
*/
91-
public GsonFile(Path file, TypeToken<R> token, Gson gson) {
92-
this(file, null, token, gson);
90+
public GsonFile(IO io, TypeToken<R> token, Gson gson) {
91+
this(io, null, token, gson);
9392
}
9493

9594
/**
9695
* Construct a new GsonFile providing a file, default root object and gson instance
9796
*
98-
* @param file the file to read from and write to
97+
* @param io the file to read from and write to
9998
* @param root the default root object
10099
* @param gson the gson instance
101100
*/
102-
public GsonFile(Path file, R root, Gson gson) {
103-
this(file, root, root.getClass(), gson);
101+
public GsonFile(IO io, R root, Gson gson) {
102+
this(io, root, root.getClass(), gson);
104103
}
105104

106105
/**
107106
* Construct a new GsonFile providing a file, default root object and type
108107
*
109-
* @param file the file to read from and write to
108+
* @param io the file to read from and write to
110109
* @param root the default root object
111110
* @param type the root type
112111
*/
113-
public GsonFile(Path file, @Nullable R root, Type type) {
114-
this(file, root, type, new GsonBuilder()
112+
public GsonFile(IO io, @Nullable R root, Type type) {
113+
this(io, root, type, new GsonBuilder()
115114
.disableHtmlEscaping()
116115
.setPrettyPrinting()
117116
.serializeNulls()
@@ -121,52 +120,52 @@ public GsonFile(Path file, @Nullable R root, Type type) {
121120
/**
122121
* Construct a new GsonFile providing a file and type
123122
*
124-
* @param file the file to read from and write to
123+
* @param io the file to read from and write to
125124
* @param type the root type
126125
*/
127-
public GsonFile(Path file, Type type) {
128-
this(file, null, type);
126+
public GsonFile(IO io, Type type) {
127+
this(io, null, type);
129128
}
130129

131130
/**
132131
* Construct a new GsonFile providing a file, default root object, and type-token
133132
*
134-
* @param file the file to read from and write to
133+
* @param io the file to read from and write to
135134
* @param root the default root object
136135
* @param token the type-token
137136
*/
138-
public GsonFile(Path file, @Nullable R root, TypeToken<R> token) {
139-
this(file, root, token.getType());
137+
public GsonFile(IO io, @Nullable R root, TypeToken<R> token) {
138+
this(io, root, token.getType());
140139
}
141140

142141
/**
143142
* Construct a new GsonFile providing a file and type-token
144143
*
145-
* @param file the file to read from and write to
144+
* @param io the file to read from and write to
146145
* @param token the type-token
147146
*/
148-
public GsonFile(Path file, TypeToken<R> token) {
149-
this(file, null, token);
147+
public GsonFile(IO io, TypeToken<R> token) {
148+
this(io, null, token);
150149
}
151150

152151
/**
153152
* Construct a new GsonFile providing a file and default root object
154153
*
155-
* @param file the file to read from and write to
154+
* @param io the file to read from and write to
156155
* @param root the default root object
157156
*/
158-
public GsonFile(Path file, R root) {
159-
this(file, root, root.getClass());
157+
public GsonFile(IO io, R root) {
158+
this(io, root, root.getClass());
160159
}
161160

162161
@Override
163162
protected @Nullable R load() {
164-
if (!Files.isRegularFile(getFile())) return getRoot();
163+
if (!getIO().exists()) return getRoot();
165164
try (var reader = new JsonReader(new InputStreamReader(
166-
Files.newInputStream(getFile(), READ),
165+
getIO().inputStream(READ),
167166
getCharset()
168167
))) {
169-
R root = getGson().fromJson(reader, getType());
168+
var root = getGson().<@Nullable R>fromJson(reader, getType());
170169
return root != null ? root : defaultRoot;
171170
} catch (IOException e) {
172171
throw new RuntimeException(e);
@@ -178,9 +177,9 @@ public GsonFile(Path file, R root) {
178177
public FileIO<R> save(FileAttribute<?>... attributes) {
179178
try {
180179
var root = getRoot();
181-
Files.createDirectories(getFile().getParent());
180+
getIO().createParents(attributes);
182181
try (var writer = new BufferedWriter(new OutputStreamWriter(
183-
Files.newOutputStream(getFile(), WRITE, CREATE, TRUNCATE_EXISTING),
182+
getIO().outputStream(WRITE, CREATE, TRUNCATE_EXISTING),
184183
getCharset()
185184
))) {
186185
getGson().toJson(root, getType(), writer);
@@ -193,7 +192,7 @@ public FileIO<R> save(FileAttribute<?>... attributes) {
193192

194193
@Override
195194
public FileIO<R> validate(Scope scope) {
196-
if (!Files.isRegularFile(getFile())) return this;
195+
if (!getIO().exists()) return this;
197196
var defaultTree = getGson().toJsonTree(defaultRoot, getType());
198197
var currentTree = getGson().toJsonTree(getRoot(), getType());
199198
var validatedTree = validate(scope, defaultTree, currentTree);
@@ -235,10 +234,10 @@ public int hashCode() {
235234
@Override
236235
public String toString() {
237236
return "GsonFile{" +
238-
"defaultRoot=" + defaultRoot +
239-
", type=" + type +
240-
", gson=" + gson +
241-
"} " + super.toString();
237+
"defaultRoot=" + defaultRoot +
238+
", type=" + type +
239+
", gson=" + gson +
240+
"} " + super.toString();
242241
}
243242

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

0 commit comments

Comments
 (0)