4
4
import org .yaml .snakeyaml .DumperOptions ;
5
5
import org .yaml .snakeyaml .Yaml ;
6
6
import java .io .*;
7
- import java .lang .reflect .Field ;
8
- import java .lang .reflect .Modifier ;
9
7
import java .nio .charset .Charset ;
10
8
import java .nio .file .Files ;
11
9
import java .nio .file .Path ;
@@ -183,71 +181,80 @@ public void save(){
183
181
}
184
182
}
185
183
186
-
187
- public void set (String url ,Object value ){
188
- this .values .replace (url ,value );
189
- }
190
-
191
- //Not tested yet
192
- public void setOrAdd (String url ,Object value ){
193
- this .values .put (url ,value );
184
+ private Object urlToObj (String url ){
185
+ String [] urls = url .split ("\\ ." );
186
+ if ( urls .length <= 1 ){
187
+ return this .values .get (url );
188
+ } else {
189
+ Map <String ,Object > tempMap = null ;
190
+ for (int i = 0 ; i < urls .length -1 ; i ++) {
191
+ if (tempMap == null ){
192
+ tempMap = (Map <String , Object >) this .values .get (urls [i ]);
193
+ } else {
194
+ tempMap = (Map <String , Object >) tempMap .get (urls [i ]);
195
+ }
196
+ }
197
+ return tempMap .get (urls [urls .length -1 ]);
198
+ }
194
199
}
195
200
201
+ /**
202
+ * This method is not ready to be used !
203
+ * @param url
204
+ */
205
+ @ Deprecated
196
206
public void remove (String url ){
197
207
this .values .remove (url );
198
208
}
199
209
200
210
public boolean getBoolean (String url ){
201
- return this . values . get (url ) != null && (boolean ) this . values . get (url );
211
+ return urlToObj (url ) != null && (boolean ) urlToObj (url );
202
212
}
203
213
204
214
205
215
public float getFloat (String url ){
206
- return this . values . get (url ) == null ? 0.0f : (float ) this . values . get (url );
216
+ return urlToObj (url ) == null ? 0.0f : (float ) urlToObj (url );
207
217
}
208
218
209
219
public double getDouble (String url ){
210
- return this . values . get (url ) == null ? 0.0 : (double ) this . values . get (url );
220
+ return urlToObj (url ) == null ? 0.0 : (double ) urlToObj (url );
211
221
}
212
222
213
223
214
224
public char getChar (String url ){
215
- return this . values . get (url ) == null ? '0' : (char ) this . values . get (url );
225
+ return urlToObj (url ) == null ? '0' : (char ) urlToObj (url );
216
226
}
217
227
218
228
public byte getByte (String url ){
219
- return this . values . get (url ) == null ? - 1 : (byte ) this . values . get (url );
229
+ return urlToObj (url ) == null ? 0 : (byte ) urlToObj (url );
220
230
}
221
231
222
232
public short getShort (String url ){
223
- return this . values . get (url ) == null ? 0 : (short ) this . values . get (url );
233
+ return urlToObj (url ) == null ? 0 : (short ) urlToObj (url );
224
234
}
225
235
226
236
public int getInt (String url ){
227
- return this . values . get (url ) == null ? 0 : (int ) this . values . get (url );
237
+ return urlToObj (url ) == null ? 0 : (int ) urlToObj (url );
228
238
}
229
239
230
240
public long getLong (String url ){
231
- return this . values . get (url ) == null ? 0 : (long ) this . values . get (url );
241
+ return urlToObj (url ) == null ? 0 : (long ) urlToObj (url );
232
242
}
233
243
234
244
public String getString (String url ){
235
- return this . values . get (url ) == null ? null : (String ) this . values . get (url );
245
+ return urlToObj (url ) == null ? "" : (String ) urlToObj (url );
236
246
}
237
247
238
248
239
249
public List <?> getList (String url ){
240
- return this . values . get (url ) == null ? null : (List <?>) this . values . get (url );
250
+ return urlToObj (url ) == null ? Collections . emptyList () : (List <?>) urlToObj (url );
241
251
}
242
252
243
-
253
+ @ Deprecated
244
254
public Object getObject (String url ){
245
255
return this .values .get (url );
246
256
}
247
257
248
- public <T > T getOrDefault (String url , T object ){
249
- return this .values .get (url ) == null ? object : (T ) this .values .get (url );
250
- }
251
258
252
259
private String commentCheck (String str ){
253
260
return (!str .startsWith ("#" )) ? "#" +str : str ;
@@ -271,26 +278,53 @@ public void customSerializer(String url, PbmSerializable obj){
271
278
addValues (url ,obj .getSerializedObject ());
272
279
}
273
280
274
- public void addValues (String url , Map <String , Object > values ){
281
+
282
+
283
+ public void set (String url ,Object value ){
275
284
if (this .values == null )
276
285
this .values = new HashMap <>();
277
- Map <String , Object > mapTemp = null ;
278
- // values.forEach((s, o) -> mapTemp.put(s,o));
279
286
String [] urls = url .split ("\\ ." );
280
287
if (!url .isEmpty () ){
281
- if (urls .length < 2 )
282
- urls [0 ] = url ;
283
- for (int i = urls .length -1 ; i > 0 ; i --) {
284
- Map <String , Object > mapfor = new HashMap <>();
285
- if (i == urls .length -1 ){
286
- mapfor .put (urls [i ],values );
287
- } else {
288
- mapfor .put (urls [i ],mapTemp );
288
+ Map <String , Object > mapTemp = null ;
289
+ if (urls .length < 2 ){
290
+ this .values .put (url ,value );
291
+ }
292
+ else {
293
+ for (int i = urls .length -1 ; i > 0 ; i --) {
294
+ Map <String , Object > mapfor = new HashMap <>();
295
+ if (i == urls .length -1 ){
296
+ mapfor .put (urls [i ],value );
297
+ } else {
298
+ mapfor .put (urls [i ],mapTemp );
299
+ }
300
+ mapTemp = mapfor ;
289
301
}
290
- mapTemp = mapfor ;
302
+ this .values .put (urls [0 ],mapTemp );
303
+ }
304
+ }
305
+ }
291
306
307
+ public void addValues (String url , Map <String , Object > values ){
308
+ if (this .values == null )
309
+ this .values = new HashMap <>();
310
+ String [] urls = url .split ("\\ ." );
311
+ if (!url .isEmpty () ){
312
+ Map <String , Object > mapTemp = null ;
313
+ if (urls .length < 2 ){
314
+ this .values .put (url ,values );
315
+ }
316
+ else {
317
+ for (int i = urls .length -1 ; i > 0 ; i --) {
318
+ Map <String , Object > mapfor = new HashMap <>();
319
+ if (i == urls .length -1 ){
320
+ mapfor .put (urls [i ],values );
321
+ } else {
322
+ mapfor .put (urls [i ],mapTemp );
323
+ }
324
+ mapTemp = mapfor ;
325
+ }
326
+ this .values .put (urls [0 ],mapTemp );
292
327
}
293
- this .values .put (urls [0 ],values );
294
328
} else {
295
329
this .values .putAll (values );
296
330
}
0 commit comments