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