T
- Type held inside closed varpublic class MutableDouble extends java.lang.Object implements To<MutableDouble>, java.util.function.DoubleSupplier, java.util.function.DoubleConsumer, java.util.function.Supplier<java.lang.Double>
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 |
---|
MutableDouble() |
Modifier and Type | Method and Description |
---|---|
void |
accept(double value) |
static MutableDouble |
fromExternal(java.util.function.DoubleSupplier s,
java.util.function.DoubleConsumer c)
Construct a MutableDouble that gets and sets an external value using the provided Supplier and Consumer
e.g.
|
java.lang.Double |
get() |
double |
getAsDouble() |
MutableDouble |
mapInput(java.util.function.DoubleUnaryOperator fn)
Use the supplied function to perform a lazy map operation when get is called
|
<T1> Mutable<T1> |
mapInputToObj(java.util.function.Function<T1,java.lang.Double> fn)
Use the supplied function to perform a lazy map operation when get is called
|
MutableDouble |
mapOutput(java.util.function.DoubleUnaryOperator fn)
Use the supplied function to perform a lazy map operation when get is called
|
<R> Mutable<R> |
mapOutputToObj(java.util.function.Function<java.lang.Double,R> fn)
Use the supplied function to perform a lazy map operation when get is called
|
MutableDouble |
mutate(java.util.function.DoubleFunction<java.lang.Double> varFn) |
static <T> MutableDouble |
of(double var)
Create a Mutable variable, which can be mutated inside a Closure
e.g.
|
MutableDouble |
set(double var) |
java.util.stream.DoubleStream |
toDoubleStream() |
java.util.OptionalDouble |
toOptionalDouble() |
public static <T> MutableDouble of(double 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
- Initial value of Mutablepublic static MutableDouble fromExternal(java.util.function.DoubleSupplier s, java.util.function.DoubleConsumer c)
MutableDouble mutable = MutableDouble.fromExternal(()->!this.value,val->!this.value);
s
- Supplier of an external valuec
- Consumer that sets an external valuepublic <R> Mutable<R> mapOutputToObj(java.util.function.Function<java.lang.Double,R> fn)
MutableDouble mutable = MutableDouble.fromExternal(()->!this.value,val->!this.value);
Mutable<Double> withOverride = mutable.mapOutputToObj(b->{
if(override)
return 10.0;
return b;
});
fn
- Map function to be applied to the result when get is calledpublic <T1> Mutable<T1> mapInputToObj(java.util.function.Function<T1,java.lang.Double> fn)
MutableDouble mutable = MutableDouble.fromExternal(()->!this.value,val->!this.value);
Mutable<Double> withOverride = mutable.mapInputToObj(b->{
if(override)
return 10.0;
return b;
});
fn
- Map function to be applied to the input when set is calledpublic MutableDouble mapOutput(java.util.function.DoubleUnaryOperator fn)
MutableDouble mutable = MutableDouble.fromExternal(()->!this.value,val->!this.value);
MutableDouble withOverride = mutable.mapOutput(b->{
if(override)
return 10.0;
return b;
});
fn
- Map function to be applied to the result when get is calledpublic MutableDouble mapInput(java.util.function.DoubleUnaryOperator fn)
MutableDouble mutable = MutableDouble.fromExternal(()->!this.value,val->!this.value);
MutableDouble withOverride = mutable.mapInput(b->{
if(override)
return 10.0;
return b;
});
fn
- Map function to be applied to the input when set is calledpublic double getAsDouble()
getAsDouble
in interface java.util.function.DoubleSupplier
public MutableDouble set(double var)
var
- New valuepublic MutableDouble mutate(java.util.function.DoubleFunction<java.lang.Double> varFn)
varFn
- New valuepublic java.util.OptionalDouble toOptionalDouble()
public java.util.stream.DoubleStream toDoubleStream()
public java.lang.Double get()
get
in interface java.util.function.Supplier<java.lang.Double>
public void accept(double value)
accept
in interface java.util.function.DoubleConsumer