Skip to content

Commit cf97b04

Browse files
committed
Revert "Refactoring."
This reverts commit 3dc84f7.
1 parent dde6465 commit cf97b04

File tree

11 files changed

+54
-64
lines changed

11 files changed

+54
-64
lines changed

src/main/java/net/mindustry_ddns/filestore/AbstractFileStore.java renamed to src/main/java/net/mindustry_ddns/store/AbstractFileStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package net.mindustry_ddns.filestore;
1+
package net.mindustry_ddns.store;
22

33
import java.io.*;
44
import java.util.function.*;
55

6+
67
/**
78
* Base class for specific implementations of a file store.
89
*
910
* @param <T> the stored object type
1011
*/
1112
public abstract class AbstractFileStore<T> implements FileStore<T> {
12-
1313
private File file;
1414
private T object;
1515

src/main/java/net/mindustry_ddns/filestore/ConfigFileStore.java renamed to src/main/java/net/mindustry_ddns/store/ConfigFileStore.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package net.mindustry_ddns.filestore;
1+
package net.mindustry_ddns.store;
22

3-
import net.mindustry_ddns.filestore.util.*;
3+
import net.mindustry_ddns.store.util.*;
44
import org.aeonbits.owner.*;
5+
56
import java.io.*;
67
import java.nio.charset.*;
78
import java.util.*;
89

10+
911
/**
1012
* A property based store backed by the <a href="http://owner.aeonbits.org/">OWNER library</a>.
1113
*
@@ -20,7 +22,6 @@
2022
* @param <T> the stored config type
2123
*/
2224
public class ConfigFileStore<T extends Accessible> extends AbstractFileStore<T> {
23-
2425
private final Factory factory;
2526
private final Class<T> clazz;
2627

@@ -33,7 +34,6 @@ public class ConfigFileStore<T extends Accessible> extends AbstractFileStore<T>
3334
* @param imports imported properties for the initial value of the file store
3435
*/
3536
public ConfigFileStore(String path, Class<T> clazz, Factory factory, Map<?, ?>... imports) {
36-
3737
super(new File(path), () -> factory.create(clazz, imports));
3838
this.factory = factory;
3939
this.clazz = clazz;
@@ -54,7 +54,6 @@ public ConfigFileStore(String path, Class<T> clazz, Map<?, ?>... imports) {
5454
* @see ConfigFileStore#ConfigFileStore(String, Class, Factory, Map[])
5555
*/
5656
public static <T extends Accessible> ConfigFileStore<T> load(String path, Class<T> clazz, Factory factory, Map<?, ?>... imports) {
57-
5857
ConfigFileStore<T> store = new ConfigFileStore<>(path, clazz, factory, imports);
5958
store.load();
6059
return store;
@@ -66,7 +65,6 @@ public static <T extends Accessible> ConfigFileStore<T> load(String path, Class<
6665
* @see ConfigFileStore#ConfigFileStore(String, Class, Map[])
6766
*/
6867
public static <T extends Accessible> ConfigFileStore<T> load(String path, Class<T> clazz, Map<?, ?>... imports) {
69-
7068
ConfigFileStore<T> store = new ConfigFileStore<>(path, clazz, imports);
7169
store.load();
7270
return store;
@@ -75,11 +73,9 @@ public static <T extends Accessible> ConfigFileStore<T> load(String path, Class<
7573
@Override
7674
@SuppressWarnings("ResultOfMethodCallIgnored")
7775
public void save() {
78-
7976
getFile().getAbsoluteFile().getParentFile().mkdirs();
8077

8178
try (final Writer writer = new FileWriter(getFile(), StandardCharsets.UTF_8)) {
82-
8379
Properties properties = new Properties();
8480
get().fill(properties);
8581
properties.store(writer, null);
@@ -90,19 +86,16 @@ public void save() {
9086

9187
@Override
9288
public void load() {
93-
9489
if (!getFile().exists()) {
9590
save();
96-
return;
97-
}
98-
99-
try (final Reader reader = new FileReader(getFile(), StandardCharsets.UTF_8)) {
100-
101-
Properties properties = new Properties();
102-
properties.load(reader);
103-
set(factory.create(clazz, properties));
104-
} catch (IOException e) {
105-
throw new RuntimeException("Unable to load the config at " + getFile(), e);
91+
} else {
92+
try (final Reader reader = new FileReader(getFile(), StandardCharsets.UTF_8)) {
93+
Properties properties = new Properties();
94+
properties.load(reader);
95+
set(factory.create(clazz, properties));
96+
} catch (IOException e) {
97+
throw new RuntimeException("Unable to load the config at " + getFile(), e);
98+
}
10699
}
107100
}
108101

src/main/java/net/mindustry_ddns/filestore/FileStore.java renamed to src/main/java/net/mindustry_ddns/store/FileStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package net.mindustry_ddns.filestore;
1+
package net.mindustry_ddns.store;
22

33
import java.io.*;
44

5+
56
/**
67
* A {@code FileStore} provides easier management of io operations with objects.
78
*
89
* @param <T> the stored object type
910
*/
1011
public interface FileStore<T> {
11-
1212
/**
1313
* @return the stored object
1414
*/

src/main/java/net/mindustry_ddns/filestore/JsonFileStore.java renamed to src/main/java/net/mindustry_ddns/store/JsonFileStore.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package net.mindustry_ddns.filestore;
1+
package net.mindustry_ddns.store;
22

33
import com.google.gson.*;
4+
45
import java.io.*;
56
import java.lang.reflect.*;
67
import java.nio.charset.*;
78
import java.util.function.*;
89

10+
911
/**
1012
* A json based store backed by Gson.
1113
*
@@ -20,7 +22,6 @@
2022
* @param <T> the stored object type
2123
*/
2224
public class JsonFileStore<T> extends AbstractFileStore<T> {
23-
2425
private final Type type;
2526
private final Gson gson;
2627

@@ -33,7 +34,6 @@ public class JsonFileStore<T> extends AbstractFileStore<T> {
3334
* @param gson the gson instance
3435
*/
3536
public JsonFileStore(String path, Type type, Supplier<T> supplier, Gson gson) {
36-
3737
super(new File(path), supplier);
3838
this.type = type;
3939
this.gson = gson;
@@ -54,7 +54,6 @@ public JsonFileStore(String path, Type type, Supplier<T> supplier) {
5454
* @see JsonFileStore#JsonFileStore(String, Type, Supplier, Gson)
5555
*/
5656
public static <T> JsonFileStore<T> load(String path, Type type, Supplier<T> supplier, Gson gson) {
57-
5857
JsonFileStore<T> store = new JsonFileStore<>(path, type, supplier, gson);
5958
store.load();
6059
return store;
@@ -66,7 +65,6 @@ public static <T> JsonFileStore<T> load(String path, Type type, Supplier<T> supp
6665
* @see JsonFileStore#JsonFileStore(String, Type, Supplier)
6766
*/
6867
public static <T> JsonFileStore<T> load(String path, Type type, Supplier<T> supplier) {
69-
7068
JsonFileStore<T> store = new JsonFileStore<>(path, type, supplier);
7169
store.load();
7270
return store;
@@ -86,16 +84,14 @@ public void save() {
8684

8785
@Override
8886
public void load() {
89-
9087
if (!getFile().exists()) {
9188
save();
92-
return;
93-
}
94-
95-
try (final Reader reader = new FileReader(getFile(), StandardCharsets.UTF_8)) {
96-
set(gson.fromJson(reader, type));
97-
} catch (IOException e) {
98-
throw new RuntimeException("Unable to load the object at " + getFile(), e);
89+
} else {
90+
try (final Reader reader = new FileReader(getFile(), StandardCharsets.UTF_8)) {
91+
set(gson.fromJson(reader, type));
92+
} catch (IOException e) {
93+
throw new RuntimeException("Unable to load the object at " + getFile(), e);
94+
}
9995
}
10096
}
10197

src/main/java/net/mindustry_ddns/filestore/util/JsonArrayReader.java renamed to src/main/java/net/mindustry_ddns/store/util/JsonArrayReader.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
package net.mindustry_ddns.filestore.util;
1+
package net.mindustry_ddns.store.util;
22

33
import com.google.gson.*;
44
import com.google.gson.stream.*;
5-
import net.mindustry_ddns.filestore.*;
5+
import net.mindustry_ddns.store.*;
6+
67
import java.io.*;
78
import java.lang.reflect.*;
89
import java.nio.charset.*;
910
import java.util.*;
1011

12+
1113
/**
1214
* An iterator backed by a {@link JsonReader} to read the elements of a json array.
1315
*
1416
* @param <E> the element type
1517
*/
16-
public class JsonArrayReader<E> implements Iterator<E>, Closeable {
17-
18+
public class JsonArrayReader<E> implements Iterator<E>, Closeable{
1819
private final JsonReader reader;
1920
private final Type type;
2021
private final Gson gson;
@@ -28,7 +29,6 @@ public class JsonArrayReader<E> implements Iterator<E>, Closeable {
2829
* @param gson the gson instance
2930
*/
3031
public JsonArrayReader(Reader reader, Type type, Gson gson) {
31-
3232
try {
3333
this.reader = new JsonReader(reader);
3434
this.type = type;
@@ -54,9 +54,7 @@ public JsonArrayReader(Reader reader, Type type) {
5454
* @param gson the gson instance
5555
*/
5656
public JsonArrayReader(String path, Type type, Gson gson) {
57-
5857
try (Reader reader = new FileReader(path, StandardCharsets.UTF_8)) {
59-
6058
this.reader = new JsonReader(reader);
6159
this.type = type;
6260
this.gson = gson;
@@ -88,15 +86,15 @@ public JsonArrayReader(JsonFileStore<?> store, Type type) {
8886
*/
8987
@Override
9088
public boolean hasNext() {
91-
9289
try {
93-
94-
if (!closed && reader.hasNext()) return true;
95-
96-
if (!closed) reader.close();
97-
closed = true;
98-
return false;
99-
90+
if (!closed && reader.hasNext()) {
91+
return true;
92+
} else {
93+
if (!closed)
94+
reader.close();
95+
closed = true;
96+
return false;
97+
}
10098
} catch (IOException e) {
10199
throw new RuntimeException(e);
102100
}
@@ -110,9 +108,11 @@ public boolean hasNext() {
110108
*/
111109
@Override
112110
public E next() {
113-
114-
if (!closed) return gson.fromJson(reader, type);
115-
throw new NoSuchElementException("No more elements to read.");
111+
if (!closed) {
112+
return gson.fromJson(reader, type);
113+
} else {
114+
throw new NoSuchElementException("No more elements to read.");
115+
}
116116
}
117117

118118
@Override

src/main/java/net/mindustry_ddns/filestore/util/SingletonConfigFactory.java renamed to src/main/java/net/mindustry_ddns/store/util/SingletonConfigFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
package net.mindustry_ddns.filestore.util;
1+
package net.mindustry_ddns.store.util;
22

33
import org.aeonbits.owner.*;
44
import org.aeonbits.owner.loaders.*;
5+
56
import java.util.*;
67

8+
79
/**
810
* A {@link Factory} view of {@link ConfigFactory}.
911
*/
1012
public final class SingletonConfigFactory implements Factory {
11-
1213
private static final SingletonConfigFactory INSTANCE = new SingletonConfigFactory();
1314

1415
public static SingletonConfigFactory getInstance() {

src/test/java/net/mindustry_ddns/filestore/ConfigFileStoreTest.java renamed to src/test/java/net/mindustry_ddns/store/ConfigFileStoreTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package net.mindustry_ddns.filestore;
1+
package net.mindustry_ddns.store;
22

3-
import net.mindustry_ddns.filestore.util.*;
3+
import net.mindustry_ddns.store.util.*;
44
import org.aeonbits.owner.*;
55
import org.junit.jupiter.api.*;
66
import org.junit.jupiter.api.io.*;

src/test/java/net/mindustry_ddns/filestore/JsonArrayReaderTest.java renamed to src/test/java/net/mindustry_ddns/store/JsonArrayReaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package net.mindustry_ddns.filestore;
1+
package net.mindustry_ddns.store;
22

3-
import net.mindustry_ddns.filestore.util.*;
3+
import net.mindustry_ddns.store.util.*;
44
import org.junit.jupiter.api.*;
55

66
import java.io.*;

src/test/java/net/mindustry_ddns/filestore/JsonFileStoreTest.java renamed to src/test/java/net/mindustry_ddns/store/JsonFileStoreTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package net.mindustry_ddns.filestore;
1+
package net.mindustry_ddns.store;
22

3-
import net.mindustry_ddns.filestore.util.*;
3+
import net.mindustry_ddns.store.util.*;
44
import org.junit.jupiter.api.*;
55
import org.junit.jupiter.api.io.*;
66

src/test/java/net/mindustry_ddns/filestore/util/TestConfig.java renamed to src/test/java/net/mindustry_ddns/store/util/TestConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.mindustry_ddns.filestore.util;
1+
package net.mindustry_ddns.store.util;
22

33
import org.aeonbits.owner.*;
44

src/test/java/net/mindustry_ddns/filestore/util/TestObject.java renamed to src/test/java/net/mindustry_ddns/store/util/TestObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.mindustry_ddns.filestore.util;
1+
package net.mindustry_ddns.store.util;
22

33
public class TestObject {
44
private int luckyNumber = 7;

0 commit comments

Comments
 (0)