Changed BdfNamedList in IBdfClassManager to BdfObject
This commit is contained in:
parent
ad7d8cb04a
commit
da3b6a117d
90
README.md
90
README.md
|
@ -7,7 +7,7 @@
|
||||||
- <a href="#creating-an-object">Creating an object</a>
|
- <a href="#creating-an-object">Creating an object</a>
|
||||||
- <a href="#arrays">Arrays</a>
|
- <a href="#arrays">Arrays</a>
|
||||||
- <a href="#named-lists">Named lists</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
|
### 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
|
```java
|
||||||
|
|
||||||
// Create a new BdfObject instance
|
class HelloWorld implements IBdfClassManager
|
||||||
BdfObject bdf = new BdfObject();
|
{
|
||||||
|
|
||||||
// Create a named list
|
@Override
|
||||||
BdfNamedList bdf_nl = new BdfNamedList();
|
BdfClassLoad(BdfObject bdf)
|
||||||
|
{
|
||||||
// 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
|
||||||
|
BdfClassSave(BdfObject bdf)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
|
@ -22,11 +22,11 @@ public class BdfClassManager
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(BdfObject bdf) {
|
public void save(BdfObject bdf) {
|
||||||
method.BdfClassSave(bdf.getNamedList());
|
method.BdfClassSave(bdf);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load(BdfObject bdf) {
|
public void load(BdfObject bdf) {
|
||||||
method.BdfClassLoad(bdf.getNamedList());
|
method.BdfClassLoad(bdf);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save() {
|
public void save() {
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package bdf.classes;
|
package bdf.classes;
|
||||||
|
|
||||||
import bdf.types.BdfNamedList;
|
import bdf.types.BdfObject;
|
||||||
|
|
||||||
public interface IBdfClassManager
|
public interface IBdfClassManager
|
||||||
{
|
{
|
||||||
public void BdfClassLoad(BdfNamedList bdf);
|
public void BdfClassLoad(BdfObject bdf);
|
||||||
public void BdfClassSave(BdfNamedList bdf);
|
public void BdfClassSave(BdfObject bdf);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,19 +3,25 @@ package tests;
|
||||||
import bdf.classes.IBdfClassManager;
|
import bdf.classes.IBdfClassManager;
|
||||||
import bdf.types.BdfNamedList;
|
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
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void BdfClassLoad(BdfNamedList bdf) {
|
public void BdfClassLoad(BdfObject bdf)
|
||||||
this.i = bdf.contains("i") ? bdf.get("i").getInteger() : 0;
|
{
|
||||||
|
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
|
@Override
|
||||||
public void BdfClassSave(BdfNamedList bdf) {
|
public void BdfClassSave(BdfObject bdf)
|
||||||
bdf.set("i", BdfObject.with(i));
|
{
|
||||||
|
bdf.setNamedList(new BdfNamedList());
|
||||||
|
bdf.getNamedList().set("i", BdfObject.with(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void tick()
|
public void tick()
|
||||||
|
|
Loading…
Reference in New Issue