88import com .google .gson .stream .JsonReader ;
99import core .file .FileIO ;
1010import core .file .Validatable ;
11+ import core .io .IO ;
1112import org .jspecify .annotations .NullMarked ;
1213import org .jspecify .annotations .Nullable ;
1314
1617import java .io .InputStreamReader ;
1718import java .io .OutputStreamWriter ;
1819import java .lang .reflect .Type ;
19- import java .nio .file .Files ;
20- import java .nio .file .Path ;
2120import java .nio .file .attribute .FileAttribute ;
2221import 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