From 269ab3a38631d49242cb410b1f69204201c9090d Mon Sep 17 00:00:00 2001 From: jsrobson10 Date: Wed, 10 Jul 2019 12:37:51 +1000 Subject: [PATCH] Added lots of helper functions to BdfNamedList and BdfObject --- db.bdf | Bin 59 -> 59 bytes src/bdf/types/BdfNamedList.java | 25 +++- src/bdf/types/BdfObject.java | 207 ++++++++++++++++++++++++++++++++ src/tests/Tests.java | 14 +-- 4 files changed, 231 insertions(+), 15 deletions(-) diff --git a/db.bdf b/db.bdf index fb18c43dc5eea989131b0b4e9d2a82dafb6f3d21..6ba2bbf50abaf7c68deaddc7c60b917deff77e1a 100755 GIT binary patch delta 11 ScmcDvo*>J}J5f%Ckrx0C-vSE& delta 11 ScmcDvo*>J}IZ;lAkrMz8!vX^U diff --git a/src/bdf/types/BdfNamedList.java b/src/bdf/types/BdfNamedList.java index 46296ee..56924ae 100644 --- a/src/bdf/types/BdfNamedList.java +++ b/src/bdf/types/BdfNamedList.java @@ -121,7 +121,7 @@ public class BdfNamedList implements IBdfType throw new UndefinedKeyException(key); } - public void remove(String key) + public BdfNamedList remove(String key) { // Convert the key to bytes byte[] key_bytes = key.getBytes(); @@ -139,7 +139,7 @@ public class BdfNamedList implements IBdfType elements.remove(i); // Exit out of the function, prevent NullPointException - return; + return this; } } @@ -147,7 +147,7 @@ public class BdfNamedList implements IBdfType throw new UndefinedKeyException(key); } - public void set(String key, BdfObject object) + public BdfNamedList set(String key, BdfObject object) { // Convert the key to bytes byte[] key_bytes = key.getBytes(); @@ -162,7 +162,7 @@ public class BdfNamedList implements IBdfType e.object = 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 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) diff --git a/src/bdf/types/BdfObject.java b/src/bdf/types/BdfObject.java index 661583d..217273a 100644 --- a/src/bdf/types/BdfObject.java +++ b/src/bdf/types/BdfObject.java @@ -113,6 +113,54 @@ public class BdfObject implements IBdfType 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; } @@ -228,4 +276,163 @@ public class BdfObject implements IBdfType this.object = value; 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()); + } + } diff --git a/src/tests/Tests.java b/src/tests/Tests.java index 19a9f85..ca7aa62 100755 --- a/src/tests/Tests.java +++ b/src/tests/Tests.java @@ -2,9 +2,6 @@ package tests; import bdf.classes.BdfClassManager; import bdf.file.BdfFileManager; -import bdf.types.BdfNamedList; -import bdf.types.BdfObject; -import bdf.types.BdfTypes; public class Tests { @@ -12,14 +9,9 @@ public class Tests { { BdfFileManager bdf = new BdfFileManager("db.bdf"); - if(bdf.getType() != BdfTypes.NAMED_LIST) - bdf.setNamedList(new BdfNamedList()); - - 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())); + bdf.setNamedListIfInvalid(); + bdf.getNamedList().allocIfUndefined("class1"); + bdf.getNamedList().allocIfUndefined("class2"); TestClass t1 = new TestClass(); BdfClassManager m1 = new BdfClassManager(t1);