public interface Var<T>
The set() method may opt to throw RuntimeException for whatever reason.
These methods on a subclass must have the following semantics
public int hashCode() { return Objects.hashCode(get()); } public boolean equals(Object obj) { return (obj instanceof Var) && Objects.equals(this.get(), ((Var<?>)obj).get()); } public String toString() { return String.valueOf(get()); }
A subclass can simply copy&paste these lines. Unfortunately they cannot be default methods on this interface.
Abstract Methods | |
---|---|
T |
get()
Read the value of this Var.
|
T |
set(T value)
Set the value of this Var, then return the value.
|
Static Methods | |
<T> Var<T> |
of(Supplier<? extends T> getter,
Consumer<? super T> setter)
Create a Var with the getter and setter.
|
<T> Var<T> |
init(T initValue)
Create a simple Var with the initial value.
|
<T> Var<T> |
init(T initValue,
Consumer<? super T> validator)
Create a Var with a validator.
|
T get()
T set(T value) throws RuntimeException
Note that the new value, not the old one, is returned. This is to be consistent with the semantics of Java assignment expression.
RuntimeException
- if the value cannot be set, for whatever reason.static <T> Var<T> of(Supplier<? extends T> getter, Consumer<? super T> setter)
The get() and set() methods of the Var simply forward to the getter and setter.
static <T> Var<T> init(T initValue)
The get() and set() methods of the Var are just like read/write a non-volatile field.
static <T> Var<T> init(T initValue, Consumer<? super T> validator) throws RuntimeException
The set(value) methods of the Var will first call `validator.accept(value)`; if the validator throws RuntimeException, set(value) throws the same exception. Other than that, the get() and set() methods are just like read/write a non-volatile field.
The initValue is also checked by the validator; if the validator throws RuntimeException, this method throws the same exception.
RuntimeException