/** * 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); }
@Override public R get() { return fn.apply(host.get()); }
@Override public Long get() { return getAsLong(); }
@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 testClosedVar() { assertThat(new MutableLong(10).getAsLong(),equalTo(10l)); } @Test
@Override public void accept(final long value) { set(value); } }
@Test public void externalMapOutput(){ value = 200; MutableLong ext = MutableLong.fromExternal(()->value,v->this.value=v) .mapOutput(s->s*2); assertThat(ext.get(),equalTo(400l)); } }
@Test public void externalMapInput(){ value = 0; MutableLong ext = MutableLong.fromExternal(()->value,v->this.value=v) .mapInput(s->s+10); ext.set(50); assertThat(value,equalTo(60l)); }
@Test public void testClosedVarHashCode() { assertThat(new MutableLong(10).hashCode(),equalTo(new MutableLong(10).hashCode())); } @Test
@Test public void externalSet(){ value = 0; MutableLong ext = MutableLong.fromExternal(()->value,v->this.value=v); ext.set(10l); assertThat(value,equalTo(10l)); }
@Test public void externalGet(){ value = 100; MutableLong ext = MutableLong.fromExternal(()->value,v->this.value=v); assertThat(ext.get(),equalTo(100L)); } @Test
@Test public void externalMapInputObj(){ value = 0; Mutable<Long> ext = MutableLong.fromExternal(()->value, v->this.value=v) .mapInputToObj(s->s+10); ext.set(50l); assertThat(value,equalTo(60l)); }
@Test public void externalMapOutputToObj(){ value = 200; Mutable<Long> ext = MutableLong.fromExternal(()->value,v->this.value=v) .mapOutputToObj(s->s*2); assertThat(ext.get(),equalTo(400l)); } @Test
@Test public void testSet() { assertThat(new MutableLong().set(1000).getAsLong(),is(1000l)); }
@Override public Mutable<T1> set(final T1 value) { host.set(fn.apply(value)); return this; }
@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
/** * 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; } }; }
@Override public MutableLong set(final long value) { host.set(fn.applyAsLong(value)); return this; }