Skip to content

Commit f5e8603

Browse files
committed
Corrections (optimization not done)
1 parent a0ec47c commit f5e8603

File tree

1 file changed

+71
-37
lines changed

1 file changed

+71
-37
lines changed

src/main/java/fr/medialo/api/pbmyaml/PbmYaml.java

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import org.yaml.snakeyaml.DumperOptions;
55
import org.yaml.snakeyaml.Yaml;
66
import java.io.*;
7-
import java.lang.reflect.Field;
8-
import java.lang.reflect.Modifier;
97
import java.nio.charset.Charset;
108
import java.nio.file.Files;
119
import java.nio.file.Path;
@@ -183,71 +181,80 @@ public void save(){
183181
}
184182
}
185183

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+
}
194199
}
195200

201+
/**
202+
* This method is not ready to be used !
203+
* @param url
204+
*/
205+
@Deprecated
196206
public void remove(String url){
197207
this.values.remove(url);
198208
}
199209

200210
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);
202212
}
203213

204214

205215
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);
207217
}
208218

209219
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);
211221
}
212222

213223

214224
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);
216226
}
217227

218228
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);
220230
}
221231

222232
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);
224234
}
225235

226236
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);
228238
}
229239

230240
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);
232242
}
233243

234244
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);
236246
}
237247

238248

239249
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);
241251
}
242252

243-
253+
@Deprecated
244254
public Object getObject(String url){
245255
return this.values.get(url);
246256
}
247257

248-
public <T> T getOrDefault(String url, T object){
249-
return this.values.get(url) == null ? object : (T) this.values.get(url);
250-
}
251258

252259
private String commentCheck(String str){
253260
return (!str.startsWith("#")) ? "#"+str : str;
@@ -271,26 +278,53 @@ public void customSerializer(String url, PbmSerializable obj){
271278
addValues(url,obj.getSerializedObject());
272279
}
273280

274-
public void addValues(String url, Map<String, Object> values){
281+
282+
283+
public void set(String url,Object value){
275284
if (this.values == null)
276285
this.values = new HashMap<>();
277-
Map<String, Object> mapTemp = null;
278-
// values.forEach((s, o) -> mapTemp.put(s,o));
279286
String[] urls = url.split("\\.");
280287
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;
289301
}
290-
mapTemp = mapfor;
302+
this.values.put(urls[0],mapTemp);
303+
}
304+
}
305+
}
291306

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);
292327
}
293-
this.values.put(urls[0],values);
294328
} else {
295329
this.values.putAll(values);
296330
}

0 commit comments

Comments
 (0)