-
Notifications
You must be signed in to change notification settings - Fork 0
Values
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).
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.
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.
You can also look at unit tests for more detailed examples.
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!");
ComplexValue<Person> personValue = JavaComplexValue.of(new Person());
Person person = personValue.get();
person.setName("a");
assertEquals("a", personValue.get(Person.NAME).get());