Added some functionality to allow saving classes easier
This commit is contained in:
parent
9effbc40c2
commit
f01696328b
BIN
db/file.db
BIN
db/file.db
Binary file not shown.
|
@ -1,75 +0,0 @@
|
|||
package bdf;
|
||||
|
||||
import bdf.data.BdfDatabase;
|
||||
import bdf.types.BdfArray;
|
||||
import bdf.types.BdfNamedList;
|
||||
import bdf.types.BdfObject;
|
||||
|
||||
public class Tests {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// Create a new BdfObject instance
|
||||
BdfObject bdf = new BdfObject();
|
||||
|
||||
// Create a named list
|
||||
BdfNamedList bdf_nl = new BdfNamedList();
|
||||
|
||||
// Add some variables to the named list
|
||||
bdf_nl.set("boolean", BdfObject.with(true));
|
||||
bdf_nl.set("an_int", BdfObject.with((int)53));
|
||||
bdf_nl.set("double", new BdfObject().setDouble(632.5));
|
||||
|
||||
// Output some checks on BdfNamedList
|
||||
System.out.println(bdf_nl.contains("an_int")); // true
|
||||
System.out.println(bdf_nl.contains("this_dosn't_exist")); // false
|
||||
|
||||
// Create an array
|
||||
BdfArray bdf_array = new BdfArray();
|
||||
|
||||
// Add some values to the array
|
||||
bdf_array.add(BdfObject.with("Hello, World!"));
|
||||
bdf_array.add(BdfObject.with(1234567890L));
|
||||
bdf_array.set(1, BdfObject.with((short)432));
|
||||
|
||||
// Output the size of the array
|
||||
System.out.println(bdf_array.size()); // 2
|
||||
|
||||
// Output the type of the 2nd item in the array, value types are in BdfTypes
|
||||
System.out.println(bdf_array.get(1).getType()); // 3 (BdfTypes.SHORT)
|
||||
|
||||
// Save the array to the named list
|
||||
bdf_nl.set("array", BdfObject.with(bdf_array));
|
||||
|
||||
// Set the named list to the bdf object
|
||||
bdf.setNamedList(bdf_nl);
|
||||
|
||||
// Serialize the data
|
||||
byte[] bdf_data = bdf.serialize().getBytes();
|
||||
|
||||
|
||||
|
||||
// Load the serialized data
|
||||
BdfObject bdf2 = new BdfObject(new BdfDatabase(bdf_data));
|
||||
|
||||
// Show the human readable serialized data
|
||||
System.out.println(bdf2.serializeHumanReadable()); // {"boolean": true, "an_int": 53I, "double": 632.5D, "array": ["Hello, World!", 432S]}
|
||||
|
||||
// Show the value of the boolean in the named list
|
||||
System.out.println(bdf2.getNamedList().get("boolean").getBoolean()); // true
|
||||
|
||||
// Show the value of item 0 in the array
|
||||
System.out.println(bdf2.getNamedList().get("array").getArray().get(0).getString()); // Hello, World!
|
||||
|
||||
// Check if the double exists
|
||||
System.out.println(bdf2.getNamedList().contains("double")); // true
|
||||
|
||||
// Remove the double from the named list
|
||||
bdf2.getNamedList().remove("double");
|
||||
|
||||
// Check if the double exists
|
||||
System.out.println(bdf2.getNamedList().contains("double")); // false
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package bdf.classes;
|
||||
|
||||
import bdf.types.BdfObject;
|
||||
|
||||
public class BdfClassManager
|
||||
{
|
||||
protected IBdfClassManager method;
|
||||
protected BdfObject object = new BdfObject();
|
||||
|
||||
public BdfClassManager(IBdfClassManager method)
|
||||
{
|
||||
// Save some variables for later
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public void setBdf(BdfObject bdf) {
|
||||
this.object = bdf;
|
||||
}
|
||||
|
||||
public BdfObject getBdf() {
|
||||
return this.object;
|
||||
}
|
||||
|
||||
public void save(BdfObject bdf) {
|
||||
method.BdfClassSave(bdf.getNamedList());
|
||||
}
|
||||
|
||||
public void load(BdfObject bdf) {
|
||||
method.BdfClassLoad(bdf.getNamedList());
|
||||
}
|
||||
|
||||
public void save() {
|
||||
this.save(this.object);
|
||||
}
|
||||
|
||||
public void load() {
|
||||
this.load(this.object);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package bdf.classes;
|
||||
|
||||
import bdf.types.BdfNamedList;
|
||||
|
||||
public interface IBdfClassManager
|
||||
{
|
||||
public void BdfClassLoad(BdfNamedList bdf);
|
||||
public void BdfClassSave(BdfNamedList bdf);
|
||||
}
|
|
@ -41,7 +41,7 @@ public class BdfFileManager extends BdfObject
|
|||
File file = new File(path);
|
||||
|
||||
// Create the parent directories
|
||||
file.getParentFile().mkdirs();
|
||||
file.getAbsoluteFile().getParentFile().mkdirs();
|
||||
|
||||
// Get the database file for output
|
||||
FileOutputStream out = new FileOutputStream(path);
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package tests;
|
||||
|
||||
import bdf.classes.IBdfClassManager;
|
||||
import bdf.types.BdfNamedList;
|
||||
import bdf.types.BdfObject;
|
||||
|
||||
public class TestClass implements IBdfClassManager
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
@Override
|
||||
public void BdfClassLoad(BdfNamedList bdf) {
|
||||
this.i = bdf.contains("i") ? bdf.get("i").getInteger() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void BdfClassSave(BdfNamedList bdf) {
|
||||
bdf.set("i", BdfObject.with(i));
|
||||
}
|
||||
|
||||
public void tick()
|
||||
{
|
||||
System.out.println(i);
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
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 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
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()));
|
||||
|
||||
TestClass t1 = new TestClass();
|
||||
BdfClassManager m1 = new BdfClassManager(t1);
|
||||
m1.setBdf(bdf.getNamedList().get("class1"));
|
||||
|
||||
TestClass t2 = new TestClass();
|
||||
BdfClassManager m2 = new BdfClassManager(t2);
|
||||
m2.setBdf(bdf.getNamedList().get("class2"));
|
||||
|
||||
m1.load();
|
||||
m2.load();
|
||||
|
||||
t1.tick();
|
||||
t2.tick();
|
||||
|
||||
m1.save();
|
||||
m2.save();
|
||||
|
||||
bdf.saveDatabase();
|
||||
|
||||
System.out.println(bdf.serializeHumanReadable());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue