Cleaned the code, updated README, made the structure more forgiving
This commit is contained in:
parent
3b94ac2646
commit
bfafa4658e
22
README.md
22
README.md
|
@ -113,10 +113,10 @@ int size = array.size();
|
|||
array.remove(3); // Could be any number
|
||||
|
||||
// Set - Could be any number with any object
|
||||
array.set(4, BdfObject.with("A String"));
|
||||
array.set(4, BdfObject.withString("A String"));
|
||||
|
||||
// Add - Could be any object
|
||||
array.add(BdfObject.with(53));
|
||||
array.add(BdfObject.withByte(53));
|
||||
|
||||
// Set the array to the bdf object
|
||||
bdf.setArray(array);
|
||||
|
@ -147,7 +147,7 @@ Named lists also have Iterator support and are an instance of
|
|||
BdfNamedList list = new BdfNamedList();
|
||||
|
||||
// Set an element with a value
|
||||
list.set("key1", BdfObject.with(5));
|
||||
list.set("key1", BdfObject.withInteger(5));
|
||||
|
||||
// Get an elements value
|
||||
int v = list.get("key1").getInteger();
|
||||
|
@ -191,14 +191,11 @@ class HelloWorld implements IBdfClassManager
|
|||
{
|
||||
// Load scripts here
|
||||
|
||||
// Create a new named list if the object isn't a named list
|
||||
bdf.setNamedListIfInvalid();
|
||||
|
||||
// Set the iterator if the iterator hasn't been set yet
|
||||
bdf.getNamedList().setIfUndefined("iterator", BdfObject.withInteger(0));
|
||||
// Get the named list
|
||||
BdfNamedList nl = bdf.getNamedList();
|
||||
|
||||
// Set the iterator stored in bdf
|
||||
int iterator = bdf.getNamedList().get("iterator").getInteger();
|
||||
int iterator = nl.get("iterator").getInteger();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -207,10 +204,13 @@ class HelloWorld implements IBdfClassManager
|
|||
// Save scripts here
|
||||
|
||||
// Create a named list
|
||||
bdf.setNamedList();
|
||||
BdfNamedList nl = new BdfNamedList();
|
||||
|
||||
// Set the iterator to the named list
|
||||
bdf.getNamedList().set("iterator", BdfObject.withInteger(iterator));
|
||||
nl.set("iterator", BdfObject.withInteger(iterator));
|
||||
|
||||
// Store the named list
|
||||
bdf.setNamedList(nl);
|
||||
}
|
||||
|
||||
public void hello()
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
/UndefinedKeyException.class
|
|
@ -1,11 +0,0 @@
|
|||
package bdf.exception;
|
||||
|
||||
public class UndefinedKeyException extends RuntimeException
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public UndefinedKeyException(String key)
|
||||
{
|
||||
super("The key \""+key+"\" has not been defined.");
|
||||
}
|
||||
}
|
|
@ -4,7 +4,6 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.util.ArrayList;
|
||||
|
||||
import bdf.data.BdfDatabase;
|
||||
import bdf.exception.UndefinedKeyException;
|
||||
import bdf.util.DataHelpers;
|
||||
|
||||
public class BdfNamedList implements IBdfType
|
||||
|
@ -117,8 +116,14 @@ public class BdfNamedList implements IBdfType
|
|||
}
|
||||
}
|
||||
|
||||
// Raise an error
|
||||
throw new UndefinedKeyException(key);
|
||||
// Get a bdf object
|
||||
BdfObject o = new BdfObject();
|
||||
|
||||
// Set the bdf object
|
||||
this.set(key, o);
|
||||
|
||||
// Send back the object
|
||||
return o;
|
||||
}
|
||||
|
||||
public BdfNamedList remove(String key)
|
||||
|
@ -143,8 +148,8 @@ public class BdfNamedList implements IBdfType
|
|||
}
|
||||
}
|
||||
|
||||
// Raise an error
|
||||
throw new UndefinedKeyException(key);
|
||||
// Send back nothing
|
||||
return this;
|
||||
}
|
||||
|
||||
public BdfNamedList set(String key, BdfObject object)
|
||||
|
@ -178,20 +183,6 @@ public class BdfNamedList implements IBdfType
|
|||
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)
|
||||
{
|
||||
// Convert the key to bytes
|
||||
|
|
|
@ -73,135 +73,87 @@ public class BdfObject implements IBdfType
|
|||
database = new BdfDatabase();
|
||||
}
|
||||
|
||||
public static BdfObject with(int v) {
|
||||
return (new BdfObject()).setInteger(v);
|
||||
}
|
||||
|
||||
public static BdfObject with(byte v) {
|
||||
return (new BdfObject()).setByte(v);
|
||||
}
|
||||
|
||||
public static BdfObject with(boolean v) {
|
||||
return (new BdfObject()).setBoolean(v);
|
||||
}
|
||||
|
||||
public static BdfObject with(float v) {
|
||||
return (new BdfObject()).setFloat(v);
|
||||
}
|
||||
|
||||
public static BdfObject with(double v) {
|
||||
return (new BdfObject()).setDouble(v);
|
||||
}
|
||||
|
||||
public static BdfObject with(long v) {
|
||||
return (new BdfObject()).setLong(v);
|
||||
}
|
||||
|
||||
public static BdfObject with(short v) {
|
||||
return (new BdfObject()).setShort(v);
|
||||
}
|
||||
|
||||
public static BdfObject with(String v) {
|
||||
return (new BdfObject()).setString(v);
|
||||
}
|
||||
|
||||
public static BdfObject with(BdfArray v) {
|
||||
return (new BdfObject()).setArray(v);
|
||||
}
|
||||
|
||||
public static BdfObject with(BdfNamedList 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() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public int getInteger() {
|
||||
return DataHelpers.getByteBuffer(database).getInt(0);
|
||||
public int getInteger()
|
||||
{
|
||||
if(this.type == BdfTypes.INTEGER)
|
||||
return DataHelpers.getByteBuffer(database).getInt(0);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
public byte getByte() {
|
||||
return database.getByte(0);
|
||||
public byte getByte()
|
||||
{
|
||||
if(this.type == BdfTypes.BYTE)
|
||||
return database.getByte(0);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean getBoolean() {
|
||||
return database.getByte(0) == 0x01;
|
||||
public boolean getBoolean()
|
||||
{
|
||||
if(this.type == BdfTypes.BOOLEAN)
|
||||
return database.getByte(0) == 0x01;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
public double getDouble() {
|
||||
return DataHelpers.getByteBuffer(database).getDouble(0);
|
||||
public double getDouble()
|
||||
{
|
||||
if(this.type == BdfTypes.DOUBLE)
|
||||
return DataHelpers.getByteBuffer(database).getDouble(0);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
public float getFloat() {
|
||||
return DataHelpers.getByteBuffer(database).getFloat(0);
|
||||
public float getFloat()
|
||||
{
|
||||
if(this.type == BdfTypes.FLOAT)
|
||||
return DataHelpers.getByteBuffer(database).getFloat(0);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
public long getLong() {
|
||||
return DataHelpers.getByteBuffer(database).getLong(0);
|
||||
public long getLong()
|
||||
{
|
||||
if(this.type == BdfTypes.LONG)
|
||||
return DataHelpers.getByteBuffer(database).getLong(0);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
public short getShort() {
|
||||
return DataHelpers.getByteBuffer(database).getShort(0);
|
||||
public short getShort()
|
||||
{
|
||||
if(this.type == BdfTypes.SHORT)
|
||||
return DataHelpers.getByteBuffer(database).getShort(0);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
public String getString()
|
||||
{
|
||||
if(this.type != BdfTypes.STRING)
|
||||
this.setString("");
|
||||
|
||||
return (String)object;
|
||||
}
|
||||
|
||||
public BdfArray getArray() {
|
||||
public BdfArray getArray()
|
||||
{
|
||||
if(this.type != BdfTypes.ARRAY)
|
||||
this.setArray();
|
||||
|
||||
return (BdfArray)object;
|
||||
}
|
||||
|
||||
public BdfNamedList getNamedList() {
|
||||
public BdfNamedList getNamedList()
|
||||
{
|
||||
if(this.type != BdfTypes.NAMED_LIST)
|
||||
this.setNamedList();
|
||||
|
||||
return (BdfNamedList)object;
|
||||
}
|
||||
|
||||
|
@ -285,154 +237,52 @@ public class BdfObject implements IBdfType
|
|||
return this.setNamedList(new BdfNamedList());
|
||||
}
|
||||
|
||||
public BdfObject set(int v) {
|
||||
return this.setInteger(v);
|
||||
public static BdfObject withInteger(int v) {
|
||||
return (new BdfObject()).setInteger(v);
|
||||
}
|
||||
|
||||
public BdfObject set(byte v) {
|
||||
return this.setByte(v);
|
||||
public static BdfObject withByte(byte v) {
|
||||
return (new BdfObject()).setByte(v);
|
||||
}
|
||||
|
||||
public BdfObject set(boolean v) {
|
||||
return this.setBoolean(v);
|
||||
public static BdfObject withBoolean(boolean v) {
|
||||
return (new BdfObject()).setBoolean(v);
|
||||
}
|
||||
|
||||
public BdfObject set(float v) {
|
||||
return this.setFloat(v);
|
||||
public static BdfObject withFloat(float v) {
|
||||
return (new BdfObject()).setFloat(v);
|
||||
}
|
||||
|
||||
public BdfObject set(double v) {
|
||||
return this.setDouble(v);
|
||||
public static BdfObject withDouble(double v) {
|
||||
return (new BdfObject()).setDouble(v);
|
||||
}
|
||||
|
||||
public BdfObject set(long v) {
|
||||
return this.setLong(v);
|
||||
public static BdfObject withLong(long v) {
|
||||
return (new BdfObject()).setLong(v);
|
||||
}
|
||||
|
||||
public BdfObject set(short v) {
|
||||
return this.setShort(v);
|
||||
public static BdfObject withShort(short v) {
|
||||
return (new BdfObject()).setShort(v);
|
||||
}
|
||||
|
||||
public BdfObject set(String v) {
|
||||
return this.setString(v);
|
||||
public static BdfObject withString(String v) {
|
||||
return (new BdfObject()).setString(v);
|
||||
}
|
||||
|
||||
public BdfObject set(BdfArray v) {
|
||||
return this.setArray(v);
|
||||
public static BdfObject withArray(BdfArray v) {
|
||||
return (new BdfObject()).setArray(v);
|
||||
}
|
||||
|
||||
public BdfObject set(BdfNamedList v) {
|
||||
return this.setNamedList(v);
|
||||
public static BdfObject withNamedList(BdfNamedList v) {
|
||||
return (new BdfObject()).setNamedList(v);
|
||||
}
|
||||
|
||||
public static BdfObject withArray() {
|
||||
return (new BdfObject()).setArray(new BdfArray());
|
||||
}
|
||||
|
||||
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());
|
||||
public static BdfObject withNamedList() {
|
||||
return (new BdfObject()).setNamedList(new BdfNamedList());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package tests;
|
||||
|
||||
import bdf.classes.IBdfClassManager;
|
||||
import bdf.types.BdfNamedList;
|
||||
import bdf.types.BdfObject;
|
||||
import bdf.types.BdfTypes;
|
||||
|
||||
public class TestClass implements IBdfClassManager
|
||||
{
|
||||
|
@ -10,16 +12,16 @@ public class TestClass implements IBdfClassManager
|
|||
@Override
|
||||
public void BdfClassLoad(BdfObject bdf)
|
||||
{
|
||||
bdf.setNamedListIfInvalid();
|
||||
bdf.getNamedList().setIfUndefined("i", BdfObject.withInteger(0));
|
||||
this.i = bdf.getNamedList().get("i").getInteger();
|
||||
BdfNamedList nl = bdf.getNamedList();
|
||||
this.i = nl.get("i").getInteger();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void BdfClassSave(BdfObject bdf)
|
||||
{
|
||||
bdf.setNamedList();
|
||||
bdf.getNamedList().set("i", BdfObject.withInteger(i));
|
||||
BdfNamedList nl = new BdfNamedList();
|
||||
nl.set("i", BdfObject.withInteger(i));
|
||||
bdf.setNamedList(nl);
|
||||
}
|
||||
|
||||
public void tick()
|
||||
|
|
|
@ -2,24 +2,26 @@ package tests;
|
|||
|
||||
import bdf.classes.BdfClassManager;
|
||||
import bdf.file.BdfFileManager;
|
||||
import bdf.types.BdfNamedList;
|
||||
import bdf.types.BdfObject;
|
||||
|
||||
public class Tests {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
BdfFileManager bdf = new BdfFileManager("db.bdf");
|
||||
BdfNamedList nl = bdf.getNamedList();
|
||||
|
||||
bdf.setNamedListIfInvalid();
|
||||
bdf.getNamedList().allocIfUndefined("class1");
|
||||
bdf.getNamedList().allocIfUndefined("class2");
|
||||
BdfObject class1 = nl.get("class1");
|
||||
BdfObject class2 = nl.get("class2");
|
||||
|
||||
TestClass t1 = new TestClass();
|
||||
BdfClassManager m1 = new BdfClassManager(t1);
|
||||
m1.setBdf(bdf.getNamedList().get("class1"));
|
||||
m1.setBdf(class1);
|
||||
|
||||
TestClass t2 = new TestClass();
|
||||
BdfClassManager m2 = new BdfClassManager(t2);
|
||||
m2.setBdf(bdf.getNamedList().get("class2"));
|
||||
m2.setBdf(class2);
|
||||
|
||||
m1.load();
|
||||
m2.load();
|
||||
|
|
Loading…
Reference in New Issue