T - Type held inside closed varpublic class MutableInt
extends java.lang.Object
implements java.util.function.IntSupplier
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 |
|---|
MutableInt() |
| Modifier and Type | Method and Description |
|---|---|
int |
getAsInt() |
MutableInt |
mutate(java.util.function.IntFunction<java.lang.Integer> varFn) |
static MutableInt |
of(int var)
Create a Mutable variable, which can be mutated inside a Closure
e.g.
|
MutableInt |
set(int var) |
public static MutableInt of(int var)
MutableInt num = MutableInt.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 - Initial value of Mutablepublic int getAsInt()
getAsInt in interface java.util.function.IntSupplierpublic MutableInt set(int var)
var - New valuepublic MutableInt mutate(java.util.function.IntFunction<java.lang.Integer> varFn)
varFn - New value