-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now child list can also be ModelField
- Loading branch information
Showing
30 changed files
with
544 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package Model.Field; | ||
|
||
import Model.AbstractModel; | ||
import Model.IModel; | ||
import Stuff.Tools.Logger; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.util.List; | ||
|
||
public class Arr<ELEM_CLASS extends AbstractModel> extends ModelField<List<ELEM_CLASS>> { | ||
|
||
Class<ELEM_CLASS> elemClass; | ||
|
||
public Arr(String name, List<ELEM_CLASS> value, IModel owner, Class<ELEM_CLASS> elemClass) { | ||
super(name, value, owner); | ||
this.elemClass = elemClass; | ||
} | ||
|
||
@Override | ||
public JSONArray getJsonValue() { | ||
JSONArray arr = new JSONArray("[]"); | ||
for (AbstractModel el: getValue()) { | ||
arr.put(el.getJsonRepresentation()); | ||
} | ||
return arr; | ||
} | ||
|
||
@Override | ||
public void setValueFromJsObject(JSONObject jsObject) { | ||
getValue().clear(); | ||
JSONArray arr = jsObject.getJSONArray(getName()); | ||
for (int i = 0; i < arr.length(); ++i) { | ||
ELEM_CLASS el = null; | ||
try { el = elemClass.getDeclaredConstructor(owner.getClass()).newInstance(owner); } | ||
catch (Exception e) { Logger.fatal(e, "Failed to make instance of {" + elemClass.getSimpleName() + "}"); } | ||
|
||
el.reconstructFromJson(arr.getJSONObject(i)); | ||
getValue().add(el); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package Model.Field; | ||
|
||
import Model.IModel; | ||
import Stuff.Tools.Logger; | ||
import org.json.JSONObject; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public class ModelField<huj> { | ||
|
||
private String name; | ||
private huj value; | ||
protected IModel owner; | ||
|
||
public ModelField(String name, huj value, IModel owner) { | ||
if (new JSONObject("{}").getGetterByClass(value.getClass()) == null) { | ||
Logger.fatal("Unsupported Field Value Class! [" + value.getClass().getSimpleName() + "]"); | ||
} | ||
this.owner = owner; | ||
this.value = value; | ||
this.name = name; | ||
} | ||
|
||
// override me please! | ||
public Object getJsonValue() { return getValue(); }; | ||
|
||
// override me please! | ||
public void setValueFromJsObject(JSONObject jsObject) { | ||
huj value = (huj)jsObject.get(getName(), getValue().getClass()); | ||
setValue(value); | ||
} | ||
|
||
public ModelField<huj> addTo(List<ModelField> fieldStorage) { | ||
fieldStorage.add(this); | ||
return this; | ||
} | ||
|
||
// field getters/setters | ||
|
||
public huj getValue() { return value; } | ||
|
||
public String getName() { return name; } | ||
|
||
public ModelField setValue(huj value) { | ||
this.value = value; | ||
return this; | ||
} | ||
|
||
public ModelField setValueFromString(String str) { | ||
setValue((huj)getParseStringLambda().apply(str)); | ||
return this; | ||
} | ||
|
||
private Function<String, Object> getParseStringLambda() { | ||
return getValue().getClass() == Integer.class ? Integer::parseInt : | ||
getValue().getClass() == Boolean.class ? Boolean::parseBoolean : | ||
getValue().getClass() == String.class ? s -> s : | ||
s -> Logger.fatal("NO WAI!!!"); | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.