Changed BdfNamedList in IBdfClassManager to BdfObject

This commit is contained in:
jsrobson10 2019-07-10 12:15:25 +10:00
parent ad7d8cb04a
commit da3b6a117d
5 changed files with 44 additions and 70 deletions

View File

@ -7,7 +7,7 @@
- <a href="#creating-an-object">Creating an object</a>
- <a href="#arrays">Arrays</a>
- <a href="#named-lists">Named lists</a>
- <a href="#example-bdf-program">Example BDF program</a>
- <a href="#saving-classes">Saving classes</a>
### Overview
@ -166,70 +166,38 @@ for(String key : list)
```
### Saving classes
### Example BDF program
Classes can be saved with `BdfClassManager` and by
implementing the `IBdfClassManager` interface,
adding 2 functions `BdfClassLoad` and `BdfClassSave`.
`BdfClassLoad` is for checking and loading data from
bdf into the classes variables, while `BdfClassSave`
is for packing pre-existing variables into bdf format.
A BdfClassManager can be used to pass the `IBdfClassManager`
interface into.
```java
// Create a new BdfObject instance
BdfObject bdf = new BdfObject();
class HelloWorld implements IBdfClassManager
{
// 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
@Override
BdfClassLoad(BdfObject bdf)
{
}
@Override
BdfClassSave(BdfObject bdf)
{
}
}
```

BIN
db.bdf Executable file

Binary file not shown.

View File

@ -22,11 +22,11 @@ public class BdfClassManager
}
public void save(BdfObject bdf) {
method.BdfClassSave(bdf.getNamedList());
method.BdfClassSave(bdf);
}
public void load(BdfObject bdf) {
method.BdfClassLoad(bdf.getNamedList());
method.BdfClassLoad(bdf);
}
public void save() {

View File

@ -1,9 +1,9 @@
package bdf.classes;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
public interface IBdfClassManager
{
public void BdfClassLoad(BdfNamedList bdf);
public void BdfClassSave(BdfNamedList bdf);
public void BdfClassLoad(BdfObject bdf);
public void BdfClassSave(BdfObject bdf);
}

View File

@ -3,19 +3,25 @@ package tests;
import bdf.classes.IBdfClassManager;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
import bdf.types.BdfTypes;
public class TestClass implements IBdfClassManager
{
int i = 0;
@Override
public void BdfClassLoad(BdfNamedList bdf) {
this.i = bdf.contains("i") ? bdf.get("i").getInteger() : 0;
public void BdfClassLoad(BdfObject bdf)
{
if(bdf.getType() != BdfTypes.NAMED_LIST) bdf.setNamedList(new BdfNamedList());
BdfNamedList nl = bdf.getNamedList();
this.i = nl.contains("i") ? nl.get("i").getInteger() : 0;
}
@Override
public void BdfClassSave(BdfNamedList bdf) {
bdf.set("i", BdfObject.with(i));
public void BdfClassSave(BdfObject bdf)
{
bdf.setNamedList(new BdfNamedList());
bdf.getNamedList().set("i", BdfObject.with(i));
}
public void tick()