From bfafa4658ec46c5af144e4e7f2dfcd1ca189919b Mon Sep 17 00:00:00 2001 From: josua Date: Fri, 26 Jul 2019 12:25:16 +1000 Subject: [PATCH] Cleaned the code, updated README, made the structure more forgiving --- README.md | 22 +- db.bdf | Bin 59 -> 0 bytes src/bdf/exception/.gitignore | 1 - src/bdf/exception/UndefinedKeyException.java | 11 - src/bdf/types/BdfNamedList.java | 29 +- src/bdf/types/BdfObject.java | 316 +++++-------------- src/tests/TestClass.java | 12 +- src/tests/Tests.java | 12 +- 8 files changed, 118 insertions(+), 285 deletions(-) delete mode 100755 db.bdf delete mode 100644 src/bdf/exception/.gitignore delete mode 100644 src/bdf/exception/UndefinedKeyException.java diff --git a/README.md b/README.md index aca3372..1a3fefe 100644 --- a/README.md +++ b/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() diff --git a/db.bdf b/db.bdf deleted file mode 100755 index 6ba2bbf50abaf7c68deaddc7c60b917deff77e1a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 59 ncmd;NU|?WN&PgmTHUzTxIe`RYCXird1QNVJ43RVfvd|>~${GdF diff --git a/src/bdf/exception/.gitignore b/src/bdf/exception/.gitignore deleted file mode 100644 index 214f134..0000000 --- a/src/bdf/exception/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/UndefinedKeyException.class diff --git a/src/bdf/exception/UndefinedKeyException.java b/src/bdf/exception/UndefinedKeyException.java deleted file mode 100644 index 5355b2e..0000000 --- a/src/bdf/exception/UndefinedKeyException.java +++ /dev/null @@ -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."); - } -} diff --git a/src/bdf/types/BdfNamedList.java b/src/bdf/types/BdfNamedList.java index 56924ae..1273d12 100644 --- a/src/bdf/types/BdfNamedList.java +++ b/src/bdf/types/BdfNamedList.java @@ -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 diff --git a/src/bdf/types/BdfObject.java b/src/bdf/types/BdfObject.java index 217273a..a4d0635 100644 --- a/src/bdf/types/BdfObject.java +++ b/src/bdf/types/BdfObject.java @@ -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()); } } diff --git a/src/tests/TestClass.java b/src/tests/TestClass.java index c12b74a..e544d0e 100755 --- a/src/tests/TestClass.java +++ b/src/tests/TestClass.java @@ -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() diff --git a/src/tests/Tests.java b/src/tests/Tests.java index ca7aa62..5616d4a 100755 --- a/src/tests/Tests.java +++ b/src/tests/Tests.java @@ -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();