The obvious answer is to use a HashMap (or a LinkedHashMap if you care about the order of the fields). Then you can add dynamic fields using the get(String name) and set(String name, Object value) methods.
This code can be implemented in a common base class. Since there are only a few methods, it is also easy to use delegation if you need to extend something else.
To avoid casting problems, you can use a type-safe map of objects :
TypedMap map = new TypedMap(); String expected = "Hallo"; map.set( KEY1, expected ); String value = map.get( KEY1 );
The trick here is the key that contains the type information:
TypedMapKey<String> KEY1 = new TypedMapKey<String>( "key1" ); TypedMapKey<List<String>> KEY2 = new TypedMapKey<List<String>>( "key2" );
Performance will be fine.
Reuse of fields is carried out using the same type of values or by expanding the class of keys of the map such as safe objects with additional functionality.
The calculated fields can be implemented with a second map, which stores Future instances that perform the calculation.
Since all manipulations occur in only two (or at least several) methods, the starting signals are simple and can be done in any way.
To implement automatic processing of a parent / child element, set the signal listener to the "set parent" signal of the child element, and then add it to the new parent element (and remove it from the old one, if necessary).
Since no framework is used and no tricks are required, the resulting code should be fairly clean and understandable. Not using String as keys has the added benefit that people will not litter code with string literals.
Aaron digulla
source share