This section describe the core SOJO API. The main aim from this framework is, to provide a "pluggable" basis to integrate different kind of conversions (also your own implementations). With this infrastructure can solve problems, where POJOs are not supported or are not the best decision.
This examples are an initiation to the SOJO API and they explain how this framework can use:
Converter converter = new Converter();
converter.addConversion(new Simple2SimpleConversion(String.class, Long.class));
String s = "57";
Object result = converter.convert(s);
assertEquals(Long.class, result.getClass());
assertEquals(new Long("57"), result);
The second example illustrate a customer with your adresses that is converted in a simplified representation. This means, objects
are converted to maps.
The benefit from this idea is, with this representation (convert and convert back = copy or clone) can solve problems there are
described in the previous subsection POJOs versus SOJO.
The figure show the transformation from JavaBeans to a simplified representation. On the left side you can find the
classes customer with a relation (one to many) to adress and on the right side you find a instance from a converted
customer instance to a java.util.Map. The same is with adress.
Converter converter = new Converter();
converter.addConversion(new ComplexBean2MapConversion());
converter.addConversion(new Iterateable2IterateableConversion());
Customer customer = new Customer();
customer.setFirstName("Paul");
customer.getAdresses().add(new Adress("London"));
customer.getAdresses().add(new Adress("Berlin"));
Object result = converter.convert(customer);
assertTrue(result instanceof Map);
Map map = (Map) result;
assertEquals(Customer.class.getName(), map.get("class"));
assertEquals("Paul", map.get("firstName"));
assertTrue(((List) map.get("adresses")).get(0) instanceof Map);
In the second example you can see one advantage. The navigation is simplified. It exist three types:
This simple Example convert String in a Double value (It is usefull, if the value (String) come from a property-file and want to set to a JavaBean.):
Converter converter = new Converter();
converter.addConversion(new Simple2SimpleConversion(String.class, Double.class));
String s = "57.3";
Object result = converter.convert(s);
assertEquals(Double.class, result.getClass());
assertEquals(new Double("57.3"), result);
This Example convert every Bean in a Map, where the property are the keys and the property-values are the map-values:
Node node = new Node("Test-Node");
node.getNamedChilds().put("N1", new Node("N1"));
node.getNamedChilds().put("N2", new Node("N2"));
node.getNamedChilds().put("N3", new Node("N3"));
Converter converter = new Converter();
converter.addConversion(new IterateableMap2MapConversion());
converter.addConversion(new Iterateable2IterateableConversion());
converter.addConversion(new ComplexBean2MapConversion());
Object simple = converter.convert(node);
Map map = (Map) simple;
System.out.println("Name: " + map.get("name"));
// print: Name: Test-Node
// and back to the Node-Object
Node nodeCopy = (Node) converter.convert(simple);
The same result, but a easy call is this variante:
Node node = new Node("Test-Node");
node.getNamedChilds().put("N1", new Node("N1"));
node.getNamedChilds().put("N2", new Node("N2"));
node.getNamedChilds().put("N3", new Node("N3"));
ObjectUtil objectUtil = new ObjectUtil();
Object simple = objectUtil.makeSimple(node);
// and back to the Node-Object
Node nodeCopy = (Node) objectUtil.makeComplex(simple);