logo How to create a read-only UJO ?


A JavaBean object needn't to implement all setters but how to make it in a UJO object?

The read-only solution:

The next solution uses an writing LOCK property. In case the UJO initialization is finished, switch on the write lock flag. The lock throws an UnsupportedOperationException in case, you try to write a value by an Ujo API. See the next sample:
package org;
import org.ujoframework.*;

public class PersonRO extends org.ujoframework.implementation.map.MapUjoExt<PersonRO> {

    public static final UjoProperty<PersonRO,Integer> ID   = newProperty("id"  , Integer.class);
    public static final UjoProperty<PersonRO,String>  NAME = newProperty("name", String.class);
    public static final UjoProperty<PersonRO,Boolean> LOCK = newProperty("LOCK", false);

    public PersonRO(Integer id, String name) {
        set(ID, id);
        set(NAME, name);
        set(LOCK, true);
    }

    @Override
    public void writeValue(UjoProperty property, Object value) {
        if (get(LOCK)) {
            throw new UnsupportedOperationException("Property is read-only: " + property);
        } else {
            super.writeValue(property, value);
        }
    }
}

About Author:


PPone(c) 2007-2009

Valid XHTML 1.0 Strict