DIR Return Create A Forum - Home
---------------------------------------------------------
Sketchflow
HTML https://sketchflow.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: Java libs code
*****************************************************
#Post#: 3--------------------------------------------------
<array> 2
By: Rasmrq Date: November 20, 2019, 4:21 pm
---------------------------------------------------------
class ArrayableArray extends ArrayList<Object> {
public Class type;
public ArrayableArray parent;
public ArrayableArray(Class type) {
this.type = type;
}
public ArrayableArray() {
this.type = Integer.class;
}
@Override
public boolean add(Object obj) {
if(obj.getClass() == this.type) {
super.add(obj);
} else {
if(obj instanceof ArrayableArray) {
ArrayableArray arr = (ArrayableArray)obj;
if(arr.type == this.type) {
super.add(arr);
arr.parent = this;
} else {
throw new IllegalArgumentException("Given array type does not
match the parent Array type. Given
type:"".concat(arr.type.toString()).concat("" Array
type:"").concat(this.type.toString()).concat("""));
}
} else {
throw new IllegalArgumentException("Given value type does not
match the Array value type. Given
type:"".concat(obj.getClass().toString()).concat("" Array
type:"").concat(this.type.toString()).concat("""));
}
}
return true;
}
@Override
public void add(int index,Object obj){
if(obj.getClass() == this.type) {
super.add(index,obj);
} else {
if(obj instanceof ArrayableArray) {
ArrayableArray arr = (ArrayableArray)obj;
if(arr.type == this.type) {
super.add(index,arr);
arr.parent = this;
} else {
throw new IllegalArgumentException("Given array type does not
match the parent Array type. Given
type:"".concat(arr.type.toString()).concat("" Array
type:"").concat(this.type.toString()).concat("""));
}
} else {
throw new IllegalArgumentException("Given value type does not
match the Array value type. Given
type:"".concat(obj.getClass().toString()).concat("" Array
type:"").concat(this.type.toString()).concat("""));
}
}
}
public String toJSON() {
String str = null;
try{
Gson gson = new Gson();
JsonElement jsonArray = gson.toJsonTree(this, new
TypeToken<List<Object() {}.getType());
str = jsonArray.toString();
} catch(NoClassDefFoundError e) {
println("GSON not defined.cannot convert array to json");
}
return str;
}
public ArrayableArray getArray(int index) {
ArrayableArray ret;
if(super.get(index) instanceof ArrayableArray) {
ret = (ArrayableArray)super.get(index);
} else {
throw new
IllegalArgumentException("Excepted:"".concat(this.getClass().toS
tring()).concat("",but
got:"").concat(super.get(index).getClass().toString()).concat(""
"));
}
return ret;
}
}
*****************************************************