Skip to content

Commit 069ae71

Browse files
committed
type inference fix + optimizations
1 parent e126d85 commit 069ae71

File tree

5 files changed

+47
-30
lines changed

5 files changed

+47
-30
lines changed

build.gradle

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ plugins {
77
}
88

99
group "net.mindustry_ddns"
10-
version "2.0.0"
10+
version "2.1.0"
1111

1212
ext.versions = [
1313
"gson" : "2.9.0",
1414
"owner": "1.0.12",
1515
"junit": "5.8.2",
16-
"jackson": "2.13.2.2"
16+
"jackson": "2.13.2.2",
17+
"geantyref": "1.3.13"
1718
]
1819

1920
java {
@@ -38,13 +39,13 @@ dependencies {
3839
compileOnly "com.google.code.gson:gson:${versions.gson}"
3940
compileOnly "org.aeonbits.owner:owner-java8:${versions.owner}"
4041
compileOnly "com.fasterxml.jackson.core:jackson-databind:${versions.jackson}"
41-
api "io.leangen.geantyref:geantyref:1.3.13"
42+
api "io.leangen.geantyref:geantyref:${versions.geantyref}"
4243

4344
// Tests
4445
testImplementation "com.google.code.gson:gson:${versions.gson}"
4546
testImplementation "org.aeonbits.owner:owner-java8:${versions.owner}"
4647
testImplementation "com.fasterxml.jackson.core:jackson-databind:${versions.jackson}"
47-
testImplementation "io.leangen.geantyref:geantyref:1.3.13"
48+
testImplementation "io.leangen.geantyref:geantyref:${versions.geantyref}"
4849

4950
testImplementation "org.junit.jupiter:junit-jupiter-params:${versions.junit}"
5051
testImplementation "org.junit.jupiter:junit-jupiter-api:${versions.junit}"

src/main/java/net/mindustry_ddns/filestore/FileStore.java

+27-16
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,27 @@ public interface FileStore<T> extends Store<T> {
1818
*
1919
* @param file the file
2020
* @param serializer the serializer instance
21+
* @param token the type token of the stored object
2122
* @param object the initial value of the store
2223
* @param <T> the stored object type
2324
* @return a new simple {@code FileStore}
2425
*/
25-
static <T> FileStore<T> of(File file, Serializer<T> serializer, T object) {
26-
return new SimpleFileStore<>(file, serializer, new TypeToken<>() {}, object);
26+
static <T> FileStore<T> of(File file, Serializer<T> serializer, TypeToken<T> token, T object) {
27+
return new SimpleFileStore<>(file, serializer, token, object);
2728
}
2829

2930
/**
3031
* Creates a new {@code FileStore}.
3132
*
3233
* @param path the path of the file
3334
* @param serializer the serializer instance
35+
* @param token the type token of the stored object
3436
* @param object the initial value of the store
3537
* @param <T> the stored object type
3638
* @return a new simple {@code FileStore}
3739
*/
38-
static <T> FileStore<T> of(String path, Serializer<T> serializer, T object) {
39-
return of(new File(path), serializer, object);
40+
static <T> FileStore<T> of(String path, Serializer<T> serializer, TypeToken<T> token, T object) {
41+
return of(new File(path), serializer, token, object);
4042
}
4143

4244
/**
@@ -46,51 +48,60 @@ static <T> FileStore<T> of(String path, Serializer<T> serializer, T object) {
4648
*
4749
* @param file the file
4850
* @param serializer the serializer instance
51+
* @param token the type token of the stored object
4952
* @param <T> the stored object type
5053
* @return a new simple {@code FileStore}
5154
*/
52-
static <T> FileStore<T> of(File file, Serializer<T> serializer) {
53-
return of(file, serializer, null);
55+
static <T> FileStore<T> of(File file, Serializer<T> serializer, TypeToken<T> token) {
56+
return of(file, serializer, token, null);
5457
}
5558

56-
5759
/**
5860
* Creates a new {@code FileStore}.
5961
* <p>
6062
* <strong>Attention</strong>, the initial value is null.
6163
*
6264
* @param path the path of the file
6365
* @param serializer the serializer instance
66+
* @param token the type token of the stored object
6467
* @param <T> the stored object type
6568
* @return a new simple {@code FileStore}
6669
*/
67-
static <T> FileStore<T> of(String path, Serializer<T> serializer) {
68-
return of(new File(path), serializer, null);
70+
static <T> FileStore<T> of(String path, Serializer<T> serializer, TypeToken<T> token) {
71+
return of(new File(path), serializer, token, null);
6972
}
7073

74+
/**
75+
* {@inheritDoc}
76+
* <p>
77+
* If the parent directories of the file don't exist, they are created.
78+
*/
79+
@Override
80+
void save();
81+
7182
/**
7283
* Returns the file where the object is stored.
7384
*/
7485
File getFile();
7586

7687
/**
77-
* Set the file where the object is stored.
88+
* Sets the file where the object is stored.
7889
*
7990
* @param file the file
8091
*/
8192
void setFile(File file);
8293

8394
/**
84-
* @return If the file exists.
85-
*/
86-
boolean doesFileExist();
87-
88-
/**
89-
* Set the file where the object is stored.
95+
* Sets the file where the object is stored.
9096
*
9197
* @param path the path of the file
9298
*/
9399
default void setFile(String path) {
94100
setFile(new File(path));
95101
}
102+
103+
@Override
104+
default boolean exists() {
105+
return getFile().exists();
106+
}
96107
}

src/main/java/net/mindustry_ddns/filestore/SimpleFileStore.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.leangen.geantyref.TypeToken;
44
import net.mindustry_ddns.filestore.serial.Serializer;
5+
56
import java.io.*;
67
import java.nio.charset.StandardCharsets;
78

@@ -29,11 +30,6 @@ public void setFile(File file) {
2930
this.file = file;
3031
}
3132

32-
@Override
33-
public boolean doesFileExist() {
34-
return file.exists();
35-
}
36-
3733
@Override
3834
public T get() {
3935
return object;

src/main/java/net/mindustry_ddns/filestore/Store.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public interface Store<T> {
1616
T get();
1717

1818
/**
19-
* Set the stored object.
19+
* Sets the stored object.
2020
*
2121
* @param object the object to store
2222
*/
@@ -29,16 +29,25 @@ public interface Store<T> {
2929

3030
/**
3131
* Loads the stored object.
32+
* <p>
33+
* If the store doesn't exist, it is created and the stored object of the store is saved.
34+
*
35+
* @see #save()
3236
*/
3337
void load();
3438

3539
/**
36-
* Returns the type of the stored object.
40+
* Returns the type token of the stored object.
3741
*/
3842
TypeToken<T> getTypeToken();
3943

4044
/**
4145
* Returns the serializer of the store.
4246
*/
4347
Serializer<T> getSerializer();
48+
49+
/**
50+
* Returns whether the store exists or not.
51+
*/
52+
boolean exists();
4453
}

src/main/java/net/mindustry_ddns/filestore/serial/ConfigSerializer.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public T deserialize(Reader reader, TypeToken<T> token) throws IOException {
3232
? ConfigFactory.create((Class<T>) clazz, properties)
3333
: factory.create((Class<T>) clazz, properties);
3434
} else {
35-
throw new IOException("The type is invalid, it should be a 'Class', not "
36-
+ token.getType().getClass().getSimpleName()
37-
+ ".");
35+
throw new IOException("The type is invalid, it should be an interface with no generic parameters, not '"
36+
+ token.getType().getClass().toString()
37+
+ "'.");
3838
}
3939
}
4040

0 commit comments

Comments
 (0)