Skip to content
Lautaro Brasseur edited this page Jun 29, 2018 · 3 revisions

Values

API

The idea behind Value interface is to provide a generic way to read/write instances. It allows accessing to value type and listening for changes on it. ComplexValue add the ability to read/write values from properties. A complex value must have a complex type associated to it.

It provides typed methods for getting and setting the values. This contrasts with Java Bean approach, which is convention name based (get/set prefixes in method names). Of course, a Java Bean could be built to wrap those values (in fact, a code generation approach is planned for some time in the future).

Implementations

Java

JavaValue just wraps an object instance into a value. JavaComplexValue does the same for complex values, using reflection for reading/writing fields.

The JavaValueTests shows some examples of how to use Java types and values.

Dynamic

The DynamicValue is a Map based complex value that allows storing generic data using a dynamic type.

The DynamicValueTests shows some examples of how to use dynamic types and values.

Examples

You can also look at unit tests for more detailed examples.

Java

Simple value

Value<String> value = JavaValue.of("Hi!");
assertEquals("Hi!", value.get());

value.set("Bye!");
assertEquals("Bye!", value.get());

value = JavaValue.of(JavaType.STRING);
assertNull(value.get());

value.set("Not null");
assertEquals("Not null", value.get());

You can listen for changes:

Value<String> value = JavaValue.of("Hi!");
value.addObserver(() ->
        assertEquals("Bye!", value.get())
);
value.set("Bye!");

Complex value

ComplexValue<Person> personValue = JavaComplexValue.of(new Person());

Person person = personValue.get();

person.setName("a");
assertEquals("a", personValue.get(Person.NAME).get());
Clone this wiki locally