/** * Create a Mutable variable, which can be mutated inside a Closure * * e.g. * <pre>{@code * 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.getValue()); * //prints 120 * } </pre> * * @param var Initial value of Mutable * @return New Mutable instance */ public static MutableInt of(final int var) { return new MutableInt( var); }
/** * Use the supplied function to perform a lazy transform operation when getValue is called * <pre> * {@code * MutableInt mutable = MutableInt.fromExternal(()->!this.value,val->!this.value); * MutableInt withOverride = mutable.mapOutput(b->{ * if(override) * return 10; * return b; * }); * * } * </pre> * * * @param fn Map function to be applied to the result when getValue is called * @return Mutable that lazily applies the provided function when getValue is called to the return value */ public MutableInt mapOutput(final IntUnaryOperator fn) { final MutableInt host = this; return new MutableInt() { @Override public int getAsInt() { return fn.applyAsInt(host.getAsInt()); } }; }
return new MutableInt() { @Override public int getAsInt() {
/** * Use the supplied function to perform a lazy transform operation when getValue is called * <pre> * {@code * MutableInt mutable = MutableInt.fromExternal(()->!this.value,val->!this.value); * MutableInt withOverride = mutable.mapInput(b->{ * if(override) * return 10; * return b; * }); * * } * </pre> * * * @param fn Map function to be applied to the input when set is called * @return Mutable that lazily applies the provided function when set is called to the input value */ public MutableInt mapInput(final IntUnaryOperator fn) { final MutableInt host = this; return new MutableInt() { @Override public MutableInt set(final int value) { host.set(fn.applyAsInt(value)); return this; } }; }
@Test public void testClosedVarEqualsFalse() { assertThat(new MutableInt(10),not(equalTo(new MutableInt(20)))); } @Test
@Test public void testClosedVarEquals() { assertThat(new MutableInt(10),equalTo(new MutableInt(10))); } @Test
@Test public void testClosedVarHashCodeFalse() { assertThat(new MutableInt(10).hashCode(),not(equalTo(new MutableInt(20).hashCode()))); } int value = 0;
@Test public void testClosedVarHashCode() { assertThat(new MutableInt(10).hashCode(),equalTo(new MutableInt(10).hashCode())); } @Test
@Test public void inClosure(){ MutableInt myInt = new MutableInt(0); Function<Integer,Function<Integer,MutableInt>> fn = ((Integer i)-> (Integer j)-> myInt.set(i*j)); fn.apply(10).apply(20); assertThat(myInt.getAsInt(), is(200)); } @Test
@Test public void testClosedVar() { assertThat(new MutableInt(10).getAsInt(),equalTo(10)); } @Test
@Test public void inClosure2(){ MutableInt myInt = new MutableInt(0); BiFunction<Integer,Integer,MutableInt> fn = (i,j)-> myInt.set(i*j); fn.apply(10,20); assertThat(myInt.getAsInt(), is(200)); }
@Test public void testSet() { assertThat(new MutableInt().set(1000).getAsInt(),is(1000)); }
/** * Create a Mutable variable, which can be mutated inside a Closure * * e.g. * <pre>{@code * 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.getValue()); * //prints 120 * } </pre> * * @param var Initial value of Mutable * @return New Mutable instance */ public static MutableInt of(final int var) { return new MutableInt( var); }
/** * Use the supplied function to perform a lazy transform operation when getValue is called * <pre> * {@code * MutableInt mutable = MutableInt.fromExternal(()->!this.value,val->!this.value); * MutableInt withOverride = mutable.mapInput(b->{ * if(override) * return 10; * return b; * }); * * } * </pre> * * * @param fn Map function to be applied to the input when set is called * @return Mutable that lazily applies the provided function when set is called to the input value */ public MutableInt mapInput(final IntUnaryOperator fn) { final MutableInt host = this; return new MutableInt() { @Override public MutableInt set(final int value) { host.set(fn.applyAsInt(value)); return this; } }; }
/** * Use the supplied function to perform a lazy transform operation when getValue is called * <pre> * {@code * MutableInt mutable = MutableInt.fromExternal(()->!this.value,val->!this.value); * MutableInt withOverride = mutable.mapOutput(b->{ * if(override) * return 10; * return b; * }); * * } * </pre> * * * @param fn Map function to be applied to the result when getValue is called * @return Mutable that lazily applies the provided function when getValue is called to the return value */ public MutableInt mapOutput(final IntUnaryOperator fn) { final MutableInt host = this; return new MutableInt() { @Override public int getAsInt() { return fn.applyAsInt(host.getAsInt()); } }; }
return new MutableInt() { @Override public int getAsInt() {