-
Notifications
You must be signed in to change notification settings - Fork 1
ElasticObjects
EO is the core of the project ElasticObjects.
Similar to Optional it wraps java objects, but add some additional features like a name map.
It allows to add/read/remove element via path, add some sort features to objects and maps and some type informations.
EO child = eo.add("level0/level1/level2/key")
.set("value");
assertEquals("value", child.get());
assertEquals("value", eo.get("level0/level1/level2/key"));
With its JSON serialization and and deserialization one can create solutions for looseless communication of applications.
For this EO stores additional information like log, a list of calls or debug level to the root element.
The serialization and deserialization is ordered, so objects can be compared as String:
final String mapJson = "{\"first\": 1,\"second\": 2,\"third\": 3}";
eo.add()
.map(mapJson);
assertEquals(1L, eo.get("first"));
assertEquals(mapJson, new EOToJSON().toJSON(eo));
One can convert different type with the map method setting a target model. It only sets the scalar values and convert it if possible.
This could also be used to upcast an object. All scalar values with the same path will be set to the subtype.
final Map map = new HashMap();
map.put("testString", "value");
map.put("testFloat", 1.1D);
final EO child = eo.add("level0")
.setModels(BasicTest.class.getSimpleName())
.map(map);
assertEquals("value", eo.get("level0/testString"));
assertEquals(1.1F, child.get("testFloat"));
assertEquals(BasicTest.class, child.get().getClass());
One can merge objects.
final BasicTest BT1 = new BasicTest();
BT1.setTestString( "value");
final BasicTest BT2 = new BasicTest();
BT2.setTestFloat( 1.1F);
eo.add()
.set(BT1);
eo.add()
.map(BT2);
assertEquals("value", eo.get("testString"));
assertEquals(1.1F, eo.get("testFloat"));
assertEquals(1.1F, BT1.getTestFloat());
With the map functionality a complete new object is build.
final EO eo = TestObjectProvider.create(BasicTest.class);
final BasicTest BT1 = new BasicTest();
BT1.setTestString( "value");
eo.add()
.map(BT1);
Assert.assertNotEquals(BT1, eo.get());
final EO eo2 = TestObjectProvider.create(BasicTest.class);
eo2.add()
.set(BT1);
Assert.assertEquals(BT1, eo2.get());
The compare method compares two objects by it's scalar values. So different objects with the same path structure could be compared. Here its a map with BasicTest object.
final Map map = new HashMap();
map.put("testString", "value");
final EO eo = TestObjectProvider.create();
eo.add().set(map);
BasicTest BT = new BasicTest();
BT.setTestString("value");
final EO eo2 = TestObjectProvider.create();
eo2.add().set(BT);
StringBuilder diff = new StringBuilder();
eo.compare(diff, eo2);
Assert.assertEquals("", diff.toString());
The next example the testString value for the BasicTest object is set to 'value2' and the diff shows the differences:
final Map map = new HashMap();
map.put("testString", "value");
final EO eo = TestObjectProvider.create();
eo.add().set(map);
BasicTest BT = new BasicTest();
BT.setTestString("value2");
final EO eo2 = TestObjectProvider.create();
eo2.add().set(BT);
StringBuilder diff = new StringBuilder();
eo.compare(diff, eo2);
Assert.assertEquals("/testString = value: != value2\n", diff.toString());