T - Type held inside closed varpublic class Mutable<T>
extends java.lang.Object
implements java.util.function.Supplier<T>
String var = "hello";
Runnable r = () -> var ="world";
Won't compile because var is treated as if it is final.
This can be 'worked around' by using a wrapping object or array.
e.g.
Mutable<String> var = Mutable.of("hello");
Runnable r = () -> var.set("world");
| Constructor and Description |
|---|
Mutable() |
| Modifier and Type | Method and Description |
|---|---|
T |
get() |
Mutable<T> |
mutate(java.util.function.Function<T,T> varFn) |
static <T> Mutable<T> |
of(T var)
Create a Mutable variable, which can be mutated inside a Closure
e.g.
|
Mutable<T> |
set(T var) |
public static <T> Mutable<T> of(T var)
Mutable<Integer> num = Mutable.of(20);
Stream.of(1,2,3,4).map(i->i*10).peek(i-> num.mutate(n->n+i)).foreach(System.out::println);
System.out.println(num.get());
//prints 120
var - public T get()
get in interface java.util.function.Supplier<T>public Mutable<T> set(T var)
var - New value