88import com .google .gson .stream .JsonReader ;
99import core .file .FileIO ;
1010import core .file .Validatable ;
11- import core .io .IO ;
1211import org .jspecify .annotations .NullMarked ;
1312import org .jspecify .annotations .Nullable ;
1413
1716import java .io .InputStreamReader ;
1817import java .io .OutputStreamWriter ;
1918import java .lang .reflect .Type ;
19+ import java .nio .file .Files ;
20+ import java .nio .file .Path ;
2021import java .nio .file .attribute .FileAttribute ;
2122import 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