Skip to content

Commit

Permalink
feat: change signature - Storable#locate returns Path instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Aug 21, 2024
1 parent e06549f commit d8755ca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/main/java/kiss/Storable.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface Storable<Self> {
default Self restore() {
synchronized (getClass()) {
try {
I.json(Files.newBufferedReader(Path.of(locate()))).as(this);
I.json(Files.newBufferedReader(locate())).as(this);
} catch (Throwable e) {
// ignore error
}
Expand All @@ -49,7 +49,7 @@ default Self restore() {
default Self store() {
synchronized (getClass()) {
try {
Path file = Path.of(locate());
Path file = locate();
Path tmp = Files.createTempFile(Files.createDirectories(file.getParent()), file.getFileName().toString(), null);

try (FileChannel c = FileChannel.open(file.resolveSibling(file.getFileName() + ".lock"), CREATE, WRITE, DELETE_ON_CLOSE)) {
Expand Down Expand Up @@ -119,7 +119,7 @@ private Signal auto(Model<Object> model, Object object) {
*
* @return An identifier of persistence location.
*/
default String locate() {
return I.env("PreferenceDirectory", ".preferences") + "/" + Model.of(this).type.getName() + ".json";
default Path locate() {
return Path.of(I.env("PreferenceDirectory", ".preferences") + "/" + Model.of(this).type.getName() + ".json");
}
}
6 changes: 3 additions & 3 deletions src/main/java/kiss/Subscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.http.WebSocket;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
Expand Down Expand Up @@ -261,8 +261,8 @@ public void onError(WebSocket web, Throwable e) {
* {@inheritDoc}
*/
@Override
public String locate() {
return I.env("LangDirectory", "lang") + "/" + text + ".json";
public Path locate() {
return Path.of(I.env("LangDirectory", "lang") + "/" + text + ".json");
}

// ======================================================================
Expand Down
23 changes: 11 additions & 12 deletions src/test/java/kiss/StorableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
*/
package kiss;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.*;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -34,7 +33,7 @@ class StorableTest {
@Test
void readFromNotExist() throws Exception {
Some instance = new Some();
Path path = Paths.get(instance.locate());
Path path = instance.locate();
assert Files.notExists(path);

instance.restore();
Expand All @@ -46,7 +45,7 @@ void readFromNotExist() throws Exception {
@Test
void readFromSizeZero() throws Exception {
Some instance = new Some();
Path path = Paths.get(instance.locate());
Path path = instance.locate();
Files.createDirectories(path.getParent());
Files.createFile(path);
assert Files.exists(path);
Expand All @@ -62,7 +61,7 @@ void readFromSizeZero() throws Exception {
@Test
void writeToNotExist() throws Exception {
Some instance = new Some();
Path path = Paths.get(instance.locate());
Path path = instance.locate();
assert Files.notExists(path);

instance.store();
Expand All @@ -72,7 +71,7 @@ void writeToNotExist() throws Exception {
@Test
void writeToSizeZero() throws Exception {
Some instance = new Some();
Path path = Paths.get(instance.locate());
Path path = instance.locate();
assert Files.notExists(path);

Files.createDirectories(path.getParent());
Expand All @@ -98,8 +97,8 @@ private class Some implements Storable<Some> {
* {@inheritDoc}
*/
@Override
public String locate() {
return room.locate(Some.class.getSimpleName()).toString();
public Path locate() {
return room.locate(Some.class.getSimpleName());
}
}

Expand Down Expand Up @@ -165,8 +164,8 @@ private Auto() {
* {@inheritDoc}
*/
@Override
public String locate() {
return room.locate(Auto.class.getSimpleName()).toString();
public Path locate() {
return room.locate(Auto.class.getSimpleName());
}
}

Expand Down Expand Up @@ -249,8 +248,8 @@ public Disposable auto() {
* {@inheritDoc}
*/
@Override
public String locate() {
return room.locate(AutoRoot.class.getSimpleName()).toString();
public Path locate() {
return room.locate(AutoRoot.class.getSimpleName());
}
}
}

0 comments on commit d8755ca

Please sign in to comment.