The {@link PersistenceEngine} allows you to specify {@link NodeBuilder}s that are responsible for converting an object into the semantic model, and a semantic model back into an object. By default, a set of node builders are created and added to the {@link PersistenceEngine} at construction. However, through the {@link #addNodeBuilder(Class,NodeBuilder)} method, you can specify which node builder to use for which class.For example, for and instance of the {@link PersistenceEngine}, {@code engine} you can map a {@link Map} to a {@link MapNodeBuilder}, so that all {@link Map} objects use the {@link MapNodeBuilder} to convert between the semantic model and an object, by
{@code engine.addNodeBuilder( Map.class, new MapNodeBuilder() );}
Importantly, in this case, the {@link PersistenceEngine} will use the {@link MapNodeBuilder} for all objects that implement {@link Map}. That means that a {@link LinkedHashMap} will also use {@link MapNodeBuilder}. Suppose you want to have the {@link LinkedHashMap} used a different {@link NodeBuilder}, say {@code LinkedMapNodeBuilder} (whichdoesn't exist, but you could write one), then you would perform the mapping as above:
{@code engine.addNodeBuilder( LinkedHashMap.class, new LinkedMapNodeBuilder() );}
And now, {@link HashMap} would use the {@link MapNodeBuilder}, but {@link LinkedHashMap} would use the {@code LinkedMapNodeBuilder}.
To customize the behavior of any {@link NodeBuilder} you write, you may also define annotations that works inconjunction with your {@link NodeBuilder}. For example, the {@link org.freezedry.persistence.annotations.PersistCollection} annotation works inconjunction with the {@link CollectionNodeBuilder}. The field annotation argument {@code elementPersistName}allows you to define the name that is used to persist each element, and the {@code elementType} allows you to definethe type ( {@link Class}) of each element. Or as another example, the {@link org.freezedry.persistence.annotations.PersistDateAs} annotation allowsyou to define the format with which a date is persisted. By default, class constants (i.e. static final fields) are not persisted. Use the {@link #setPersistClassConstants(boolean)}to change the default behavior. Note that class constants are not set when reconstructing the class from the persisted form. In other words, the class constants always take their value from the class source code value. When parsing the semantic model, this class will attempt to instantiate an object of the specified class by calling the constructor with the smallest number of arguments. If a no-argument constructor exists, then that is what will be called. Constructors that have arguments will be passed null objects, and then the fields will be set reflectively through the {@link Field#set(Object,Object)} method. This means that if the constructor performs checks against null, you will have a problem. (TODO allow passing default values to the constructor so that the instantiation passes its checks). @see InfoNode @see NodeBuilder @see PersistenceWriter @see PersistenceReader @see Persist @author Robert Philipp
|
|
|
|
|
|