Pavel has posted 1 posts at DZone. View Full User Profile

Key-Value Coding in Java

05.10.2014
| 6204 views |
  • submit to reddit
logoThe guide to the ujo-core module of the Ujorm framework

Introduction

Ujorm is a library for developing Java applications built on a key-value architecture of domain objects that access their attributes using objects called keys only. A Key consists of an immutable static object that is part of a domain class and provides several services. A Key in Ujorm never contains business data - unlike a similar framework, Joda Beans by author Stephen Colebourne, where attributes are contained in the Property object type.

The architercure of Ujo objects was described in the original documentation, this article demonstrates some new or interesting uses of Ujo on short examples that are based on the ujo-core module and therefore are not related with database queries. The core module can be loaded into your project using the Maven dependency:

        <dependency>
            <groupId>org.ujorm</groupId>
            <artifactId>ujo-core</artifactId>
            <version>1.45</version>
        </dependency>

Code samples are based on two entities, Employee and Company, of the Ujo type, while each of them contains (generated) setters and getters so that the object meets the JavaBeans specification.

key-value-model

Writing and reading values

We can skip the description of reading and writing attributes to JavaBeans here and go straight to reading and writing values ​​using the API of the Ujorm framework.

        Employee person = new Employee();

        // Write:
        person.set(ID, 7L);
        person.set(NAME, "Pavel");
        person.set(WAGE, 20.00);
        person.set(COMPANY, new Company());

        // Read:
        Long id = person.get(ID);
        String name = person.get(NAME);
        Double wage = person.get(WAGE);
        Company company = person.get(COMPANY);

        assert id == 7L;
        assert name == "Pavel";
        assert wage == 20.00;
        assert company != null;

To shorten the code in the above examples, the necessary static keys of the used Ujo objects were imported in the head of the class by writing:

        import static org.Company.CITY;
        import static org.Employee.*;

The above example explains how to use the key to access the related object attributes, however it does not now seem that the keys would have any benefits in comparison with POJO. I will try to convince you about the benefits of this solution in the following sections.

Restoring default values

Each Key contains its default value, which can be obtained using the Key.getDefault() method. What is more interesting is reading an attribute with a NULL value, because the Key replaces NULL attributes with its default value automatically. If the default value is NULL too, the replacement is insignificant. To restore all default values ​​it is therefore sufficient to assign each attribute with a NULL value this way:

        Employee employee = getEmployee();

        for (Key key : employee.readKeys()) {
            employee.set(key, null);
        }

        assert employee.getWage() == 0.0
               : "Default value is zero";
        assert employee.getWage() == WAGE.getDefault()
               : "Check the default value";

Another version of the previous example restores the default values ​​for numeric attributes only:

        Employee employee = getEmployee();

        for (Key key : employee.readKeys()) {
            if (key.isTypeOf(Number.class)) {
                employee.set(key, null);
            }
        }

The Ujo.readKeys() method returns all the direct keys of the current class, including parent classes, so restoring default values ​will affect all parent attributes, if there are any. For comparison of the type of the key value, the appropriate method is Key.isTypeOf(Class).

Published at DZone with permission of its author, Pavel Ponec.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)