Cleaned the code, updated README, made the structure more forgiving

This commit is contained in:
josua 2019-07-26 12:25:16 +10:00
parent 3b94ac2646
commit bfafa4658e
8 changed files with 118 additions and 285 deletions

View File

@ -113,10 +113,10 @@ int size = array.size();
array.remove(3); // Could be any number array.remove(3); // Could be any number
// Set - Could be any number with any object // 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 // Add - Could be any object
array.add(BdfObject.with(53)); array.add(BdfObject.withByte(53));
// Set the array to the bdf object // Set the array to the bdf object
bdf.setArray(array); bdf.setArray(array);
@ -147,7 +147,7 @@ Named lists also have Iterator support and are an instance of
BdfNamedList list = new BdfNamedList(); BdfNamedList list = new BdfNamedList();
// Set an element with a value // Set an element with a value
list.set("key1", BdfObject.with(5)); list.set("key1", BdfObject.withInteger(5));
// Get an elements value // Get an elements value
int v = list.get("key1").getInteger(); int v = list.get("key1").getInteger();
@ -191,14 +191,11 @@ class HelloWorld implements IBdfClassManager
{ {
// Load scripts here // Load scripts here
// Create a new named list if the object isn't a named list // Get the named list
bdf.setNamedListIfInvalid(); BdfNamedList nl = bdf.getNamedList();
// Set the iterator if the iterator hasn't been set yet
bdf.getNamedList().setIfUndefined("iterator", BdfObject.withInteger(0));
// Set the iterator stored in bdf // Set the iterator stored in bdf
int iterator = bdf.getNamedList().get("iterator").getInteger(); int iterator = nl.get("iterator").getInteger();
} }
@Override @Override
@ -207,10 +204,13 @@ class HelloWorld implements IBdfClassManager
// Save scripts here // Save scripts here
// Create a named list // Create a named list
bdf.setNamedList(); BdfNamedList nl = new BdfNamedList();
// Set the iterator to the named list // 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() public void hello()

BIN
db.bdf

Binary file not shown.

View File

@ -1 +0,0 @@
/UndefinedKeyException.class

View File

@ -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.");
}
}

View File

@ -4,7 +4,6 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import bdf.data.BdfDatabase; import bdf.data.BdfDatabase;
import bdf.exception.UndefinedKeyException;
import bdf.util.DataHelpers; import bdf.util.DataHelpers;
public class BdfNamedList implements IBdfType public class BdfNamedList implements IBdfType
@ -117,8 +116,14 @@ public class BdfNamedList implements IBdfType
} }
} }
// Raise an error // Get a bdf object
throw new UndefinedKeyException(key); BdfObject o = new BdfObject();
// Set the bdf object
this.set(key, o);
// Send back the object
return o;
} }
public BdfNamedList remove(String key) public BdfNamedList remove(String key)
@ -143,8 +148,8 @@ public class BdfNamedList implements IBdfType
} }
} }
// Raise an error // Send back nothing
throw new UndefinedKeyException(key); return this;
} }
public BdfNamedList set(String key, BdfObject object) public BdfNamedList set(String key, BdfObject object)
@ -178,20 +183,6 @@ public class BdfNamedList implements IBdfType
return this; 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)
{ {
// Convert the key to bytes // Convert the key to bytes

View File

@ -73,135 +73,87 @@ public class BdfObject implements IBdfType
database = new BdfDatabase(); 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() { public byte getType() {
return this.type; return this.type;
} }
public int getInteger() { public int getInteger()
return DataHelpers.getByteBuffer(database).getInt(0); {
if(this.type == BdfTypes.INTEGER)
return DataHelpers.getByteBuffer(database).getInt(0);
else
return 0;
} }
public byte getByte() { public byte getByte()
return database.getByte(0); {
if(this.type == BdfTypes.BYTE)
return database.getByte(0);
else
return 0;
} }
public boolean getBoolean() { public boolean getBoolean()
return database.getByte(0) == 0x01; {
if(this.type == BdfTypes.BOOLEAN)
return database.getByte(0) == 0x01;
else
return false;
} }
public double getDouble() { public double getDouble()
return DataHelpers.getByteBuffer(database).getDouble(0); {
if(this.type == BdfTypes.DOUBLE)
return DataHelpers.getByteBuffer(database).getDouble(0);
else
return 0;
} }
public float getFloat() { public float getFloat()
return DataHelpers.getByteBuffer(database).getFloat(0); {
if(this.type == BdfTypes.FLOAT)
return DataHelpers.getByteBuffer(database).getFloat(0);
else
return 0;
} }
public long getLong() { public long getLong()
return DataHelpers.getByteBuffer(database).getLong(0); {
if(this.type == BdfTypes.LONG)
return DataHelpers.getByteBuffer(database).getLong(0);
else
return 0;
} }
public short getShort() { public short getShort()
return DataHelpers.getByteBuffer(database).getShort(0); {
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; return (String)object;
} }
public BdfArray getArray() { public BdfArray getArray()
{
if(this.type != BdfTypes.ARRAY)
this.setArray();
return (BdfArray)object; return (BdfArray)object;
} }
public BdfNamedList getNamedList() { public BdfNamedList getNamedList()
{
if(this.type != BdfTypes.NAMED_LIST)
this.setNamedList();
return (BdfNamedList)object; return (BdfNamedList)object;
} }
@ -285,154 +237,52 @@ public class BdfObject implements IBdfType
return this.setNamedList(new BdfNamedList()); return this.setNamedList(new BdfNamedList());
} }
public BdfObject set(int v) { public static BdfObject withInteger(int v) {
return this.setInteger(v); return (new BdfObject()).setInteger(v);
} }
public BdfObject set(byte v) { public static BdfObject withByte(byte v) {
return this.setByte(v); return (new BdfObject()).setByte(v);
} }
public BdfObject set(boolean v) { public static BdfObject withBoolean(boolean v) {
return this.setBoolean(v); return (new BdfObject()).setBoolean(v);
} }
public BdfObject set(float v) { public static BdfObject withFloat(float v) {
return this.setFloat(v); return (new BdfObject()).setFloat(v);
} }
public BdfObject set(double v) { public static BdfObject withDouble(double v) {
return this.setDouble(v); return (new BdfObject()).setDouble(v);
} }
public BdfObject set(long v) { public static BdfObject withLong(long v) {
return this.setLong(v); return (new BdfObject()).setLong(v);
} }
public BdfObject set(short v) { public static BdfObject withShort(short v) {
return this.setShort(v); return (new BdfObject()).setShort(v);
} }
public BdfObject set(String v) { public static BdfObject withString(String v) {
return this.setString(v); return (new BdfObject()).setString(v);
} }
public BdfObject set(BdfArray v) { public static BdfObject withArray(BdfArray v) {
return this.setArray(v); return (new BdfObject()).setArray(v);
} }
public BdfObject set(BdfNamedList v) { public static BdfObject withNamedList(BdfNamedList v) {
return this.setNamedList(v); return (new BdfObject()).setNamedList(v);
} }
public BdfObject setIfInvalid(int v) { public static BdfObject withArray() {
if(this.getType() == BdfTypes.INTEGER) return this; return (new BdfObject()).setArray(new BdfArray());
return this.setInteger(v);
} }
public BdfObject setIfInvalid(byte v) { public static BdfObject withNamedList() {
if(this.getType() == BdfTypes.BYTE) return this; return (new BdfObject()).setNamedList(new BdfNamedList());
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

@ -1,7 +1,9 @@
package tests; package tests;
import bdf.classes.IBdfClassManager; import bdf.classes.IBdfClassManager;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
import bdf.types.BdfTypes;
public class TestClass implements IBdfClassManager public class TestClass implements IBdfClassManager
{ {
@ -10,16 +12,16 @@ public class TestClass implements IBdfClassManager
@Override @Override
public void BdfClassLoad(BdfObject bdf) public void BdfClassLoad(BdfObject bdf)
{ {
bdf.setNamedListIfInvalid(); BdfNamedList nl = bdf.getNamedList();
bdf.getNamedList().setIfUndefined("i", BdfObject.withInteger(0)); this.i = nl.get("i").getInteger();
this.i = bdf.getNamedList().get("i").getInteger();
} }
@Override @Override
public void BdfClassSave(BdfObject bdf) public void BdfClassSave(BdfObject bdf)
{ {
bdf.setNamedList(); BdfNamedList nl = new BdfNamedList();
bdf.getNamedList().set("i", BdfObject.withInteger(i)); nl.set("i", BdfObject.withInteger(i));
bdf.setNamedList(nl);
} }
public void tick() public void tick()

View File

@ -2,24 +2,26 @@ 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;
public class Tests { public class Tests {
public static void main(String[] args) public static void main(String[] args)
{ {
BdfFileManager bdf = new BdfFileManager("db.bdf"); BdfFileManager bdf = new BdfFileManager("db.bdf");
BdfNamedList nl = bdf.getNamedList();
bdf.setNamedListIfInvalid(); BdfObject class1 = nl.get("class1");
bdf.getNamedList().allocIfUndefined("class1"); BdfObject class2 = nl.get("class2");
bdf.getNamedList().allocIfUndefined("class2");
TestClass t1 = new TestClass(); TestClass t1 = new TestClass();
BdfClassManager m1 = new BdfClassManager(t1); BdfClassManager m1 = new BdfClassManager(t1);
m1.setBdf(bdf.getNamedList().get("class1")); m1.setBdf(class1);
TestClass t2 = new TestClass(); TestClass t2 = new TestClass();
BdfClassManager m2 = new BdfClassManager(t2); BdfClassManager m2 = new BdfClassManager(t2);
m2.setBdf(bdf.getNamedList().get("class2")); m2.setBdf(class2);
m1.load(); m1.load();
m2.load(); m2.load();