Added lots of helper functions to BdfNamedList and BdfObject

This commit is contained in:
jsrobson10 2019-07-10 12:37:51 +10:00
parent 8ca912589b
commit 269ab3a386
4 changed files with 231 additions and 15 deletions

BIN
db.bdf

Binary file not shown.

View File

@ -121,7 +121,7 @@ public class BdfNamedList implements IBdfType
throw new UndefinedKeyException(key); throw new UndefinedKeyException(key);
} }
public void remove(String key) public BdfNamedList remove(String key)
{ {
// Convert the key to bytes // Convert the key to bytes
byte[] key_bytes = key.getBytes(); byte[] key_bytes = key.getBytes();
@ -139,7 +139,7 @@ public class BdfNamedList implements IBdfType
elements.remove(i); elements.remove(i);
// Exit out of the function, prevent NullPointException // Exit out of the function, prevent NullPointException
return; return this;
} }
} }
@ -147,7 +147,7 @@ public class BdfNamedList implements IBdfType
throw new UndefinedKeyException(key); throw new UndefinedKeyException(key);
} }
public void set(String key, BdfObject object) public BdfNamedList set(String key, BdfObject object)
{ {
// Convert the key to bytes // Convert the key to bytes
byte[] key_bytes = key.getBytes(); byte[] key_bytes = key.getBytes();
@ -162,7 +162,7 @@ public class BdfNamedList implements IBdfType
e.object = object; e.object = object;
// Exit out of the function, don't add another object // Exit out of the function, don't add another object
return; return this;
} }
} }
@ -173,6 +173,23 @@ public class BdfNamedList implements IBdfType
// Add the new element object to the elements list // Add the new element object to the elements list
elements.add(e); elements.add(e);
// Send this class back
return this;
}
public BdfNamedList setIfUndefined(String key, BdfObject object)
{
if(this.contains(key)) return this;
else return this.set(key, object);
}
public BdfNamedList allocIfUndefined(String key) {
return this.setIfUndefined(key, new BdfObject());
}
public BdfNamedList alloc(String key) {
return this.set(key, new BdfObject());
} }
public boolean contains(String key) public boolean contains(String key)

View File

@ -113,6 +113,54 @@ public class BdfObject implements IBdfType
return (new BdfObject()).setNamedList(v); return (new BdfObject()).setNamedList(v);
} }
public static BdfObject withInteger(int v) {
return (new BdfObject()).setInteger(v);
}
public static BdfObject withByte(byte v) {
return (new BdfObject()).setByte(v);
}
public static BdfObject withBoolean(boolean v) {
return (new BdfObject()).setBoolean(v);
}
public static BdfObject withFloat(float v) {
return (new BdfObject()).setFloat(v);
}
public static BdfObject withDouble(double v) {
return (new BdfObject()).setDouble(v);
}
public static BdfObject withLong(long v) {
return (new BdfObject()).setLong(v);
}
public static BdfObject withShort(short v) {
return (new BdfObject()).setShort(v);
}
public static BdfObject withString(String v) {
return (new BdfObject()).setString(v);
}
public static BdfObject withArray(BdfArray v) {
return (new BdfObject()).setArray(v);
}
public static BdfObject withBdfNamedList(BdfNamedList v) {
return (new BdfObject()).setNamedList(v);
}
public static BdfObject withArray() {
return (new BdfObject()).setArray(new BdfArray());
}
public static BdfObject withBdfNamedList() {
return (new BdfObject()).setNamedList(new BdfNamedList());
}
public byte getType() { public byte getType() {
return this.type; return this.type;
} }
@ -228,4 +276,163 @@ public class BdfObject implements IBdfType
this.object = value; this.object = value;
return this; return this;
} }
public BdfObject setArray() {
return this.setArray(new BdfArray());
}
public BdfObject setNamedList() {
return this.setNamedList(new BdfNamedList());
}
public BdfObject set(int v) {
return this.setInteger(v);
}
public BdfObject set(byte v) {
return this.setByte(v);
}
public BdfObject set(boolean v) {
return this.setBoolean(v);
}
public BdfObject set(float v) {
return this.setFloat(v);
}
public BdfObject set(double v) {
return this.setDouble(v);
}
public BdfObject set(long v) {
return this.setLong(v);
}
public BdfObject set(short v) {
return this.setShort(v);
}
public BdfObject set(String v) {
return this.setString(v);
}
public BdfObject set(BdfArray v) {
return this.setArray(v);
}
public BdfObject set(BdfNamedList v) {
return this.setNamedList(v);
}
public BdfObject setIfInvalid(int v) {
if(this.getType() == BdfTypes.INTEGER) return this;
return this.setInteger(v);
}
public BdfObject setIfInvalid(byte v) {
if(this.getType() == BdfTypes.BYTE) return this;
return this.setByte(v);
}
public BdfObject setIfInvalid(boolean v) {
if(this.getType() == BdfTypes.BOOLEAN) return this;
return this.setBoolean(v);
}
public BdfObject setIfInvalid(float v) {
if(this.getType() == BdfTypes.FLOAT) return this;
return this.setFloat(v);
}
public BdfObject setIfInvalid(double v) {
if(this.getType() == BdfTypes.DOUBLE) return this;
return this.setDouble(v);
}
public BdfObject setIfInvalid(long v) {
if(this.getType() == BdfTypes.LONG) return this;
return this.setLong(v);
}
public BdfObject setIfInvalid(short v) {
if(this.getType() == BdfTypes.SHORT) return this;
return this.setShort(v);
}
public BdfObject setIfInvalid(String v) {
if(this.getType() == BdfTypes.STRING) return this;
return this.setString(v);
}
public BdfObject setIfInvalid(BdfArray v) {
if(this.getType() == BdfTypes.ARRAY) return this;
return this.setArray(v);
}
public BdfObject setIfInvalid(BdfNamedList v) {
if(this.getType() == BdfTypes.NAMED_LIST) return this;
return this.setNamedList(v);
}
public BdfObject setIntegerIfInvalid(int v) {
if(this.getType() == BdfTypes.INTEGER) return this;
return this.setInteger(v);
}
public BdfObject setByteIfInvalid(byte v) {
if(this.getType() == BdfTypes.BYTE) return this;
return this.setByte(v);
}
public BdfObject setBooleanIfInvalid(boolean v) {
if(this.getType() == BdfTypes.BOOLEAN) return this;
return this.setBoolean(v);
}
public BdfObject setFloatIfInvalid(float v) {
if(this.getType() == BdfTypes.FLOAT) return this;
return this.setFloat(v);
}
public BdfObject setDoubleIfInvalid(double v) {
if(this.getType() == BdfTypes.DOUBLE) return this;
return this.setDouble(v);
}
public BdfObject setLongIfInvalid(long v) {
if(this.getType() == BdfTypes.LONG) return this;
return this.setLong(v);
}
public BdfObject setShortIfInvalid(short v) {
if(this.getType() == BdfTypes.SHORT) return this;
return this.setShort(v);
}
public BdfObject setStringIfInvalid(String v) {
if(this.getType() == BdfTypes.STRING) return this;
return this.setString(v);
}
public BdfObject setArrayIfInvalid(BdfArray v) {
if(this.getType() == BdfTypes.ARRAY) return this;
return this.setArray(v);
}
public BdfObject setNamedListIfInvalid(BdfNamedList v) {
if(this.getType() == BdfTypes.NAMED_LIST) return this;
return this.setNamedList(v);
}
public BdfObject setArrayIfInvalid() {
if(this.getType() == BdfTypes.ARRAY) return this;
return this.setArray(new BdfArray());
}
public BdfObject setNamedListIfInvalid() {
if(this.getType() == BdfTypes.NAMED_LIST) return this;
return this.setNamedList(new BdfNamedList());
}
} }

View File

@ -2,9 +2,6 @@ package tests;
import bdf.classes.BdfClassManager; import bdf.classes.BdfClassManager;
import bdf.file.BdfFileManager; import bdf.file.BdfFileManager;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
import bdf.types.BdfTypes;
public class Tests { public class Tests {
@ -12,14 +9,9 @@ public class Tests {
{ {
BdfFileManager bdf = new BdfFileManager("db.bdf"); BdfFileManager bdf = new BdfFileManager("db.bdf");
if(bdf.getType() != BdfTypes.NAMED_LIST) bdf.setNamedListIfInvalid();
bdf.setNamedList(new BdfNamedList()); bdf.getNamedList().allocIfUndefined("class1");
bdf.getNamedList().allocIfUndefined("class2");
if(!bdf.getNamedList().contains("class1"))
bdf.getNamedList().set("class1", BdfObject.with(new BdfNamedList()));
if(!bdf.getNamedList().contains("class2"))
bdf.getNamedList().set("class2", BdfObject.with(new BdfNamedList()));
TestClass t1 = new TestClass(); TestClass t1 = new TestClass();
BdfClassManager m1 = new BdfClassManager(t1); BdfClassManager m1 = new BdfClassManager(t1);