Skip to content

Read Only Mode Adapter

Jan Wiemer edited this page Dec 30, 2020 · 3 revisions

Read Only Mode Adapter

in the previous chapter (Object Adapter) it was explained that the read only mode only makes sense for certain object adapter implementations where the committed versions of the objects are stored in the same format as the external representation (the object class). This is specially the case for the default object adapter based on cloning (JacisCloningObjectAdapter). For this object adapter it is possible to set a read only mode adapter (JacisStoreEntryReadOnlyModeAdapter) by passing it to the constructor of the JacisCloningObjectAdapter. The default constructor uses the DefaultJacisStoreEntryReadOnlyModeAdapter read only mode adapter already described in the chapter Read Only Objects. This default implementation works for objects implementing the JacisReadonlyModeSupport interface.

If a read only mode adapter is set and applicable for the object type, all committed objects are stored in read only mode. Every time a writable object (new or modified) is cloned back from the TX-View to the store the object adapter uses the read only mode adapter to switch the object to read only mode (switchToReadOnlyMode). If a normal access method without the …​ReadOnly suffix is used to get objects from the store they are cloned and the clone is switched to read-write mode (switchToReadWriteMode). If one of the methods with the suffix …​ReadOnly is used to get objects in read only mode the object adapter simply returns the object in the store that is already in read only mode (without cloning it).

One question that still has to be answered is what happens if a method with the …​ReadOnly suffix is called when there is no read only mode adapter set, or it is not applicable for the stored objects. This is the case if we are using the default adapter but the objects are not implementing the JacisReadonlyModeSupport interface. By default the object adapter will clone and return the requested objects in this case. This way the caller can work with the objects without getting an error. The drawback is that we may not notice that we are working with read only objects but anyway clone al objects because we e.g. forgot to let the objects implement the required interface. Therefore the object adapter can be switched to a more strict mode where it will throw an exception (ReadOnlyModeNotSupportedException) if the request for read only objects can not be fulfilled. This is done by calling the setThrowIfMissingReadOnlyModeDetected at the JacisCloningObjectAdapter.

Custom implementations of the JacisStoreEntryReadOnlyModeAdapter have to implement the following methods (the type parameter V refers the value type of the stored objects):

boolean isApplicableTo(V value);

Returns if the read only mode adapter can switch the mode for the passed object.

boolean isReadOnly(V value);

Returns if the passed object is in read only mode.

V switchToReadOnlyMode(V value);

Switch the passed object to read only mode and return the switched object.

V switchToReadWriteMode(V value);

Switch the passed object to read write mode and return the switched object.

Note that the switch…​ methods return the switched object and this returned object is used by the object adapter calling the method. This enables the read only mode adapter to implement the read only mode by wrapping the object in a "read only wrapper" (similar to the java.util.Collections.unmodifiableList method).

Next Chapter: Custom Dirty Check