/** * Create a Mutable variable, which can be mutated inside a Closure * * e.g. * <pre>{@code * MutableFloat num = MutableFloat.of(true); * * num.mutate(n->!n)) * * System.out.println(num.getAsFloat()); * //prints false * } </pre> * * @param var Initial value of Mutable * @return New Mutable instance */ public static <T> MutableFloat of(final float var) { return new MutableFloat( var); }
@Override public R get() { return fn.apply(host.get()); }
@Override public void accept(final Float t) { set(t); }
@Test public void inClosure2(){ MutableFloat myInt = new MutableFloat(zero); BiFunction<Float,Float,MutableFloat> fn = (i,j)-> myInt.set(new Float((float)(i*j))); fn.apply(ten,twenty); assertThat(myInt.getAsFloat(), is((float)200)); }
@Test public void testClosedVarHashCode() { assertThat(new MutableFloat(ten).hashCode(),equalTo(new MutableFloat(ten).hashCode())); } @Test
@Test public void externalGet(){ value = 100; MutableFloat ext = MutableFloat.fromExternal(()->value,v->this.value=v); assertThat(ext.get(),equalTo((float)100)); } @Test
@Test public void testClosedVar() { assertThat(new MutableFloat(ten).getAsFloat(),equalTo(ten)); } @Test
@Test public void externalSet(){ value = 0; MutableFloat ext = MutableFloat.fromExternal(()->value,v->this.value=v); ext.set(ten); assertThat(value,equalTo((float)10)); }
@Test public void testMutate(){ MutableFloat num = MutableFloat.of(twenty); Stream.of(1,2,3,4).map(i->i*10).peek(i-> num.mutate(n->new Float((float)(n+i)))).forEach(System.out::println); assertThat(num.getAsFloat(),is((float)120)); } @Test
@Test public void externalMapOutputToObj(){ value = 200; Mutable<Float> ext = MutableFloat.fromExternal(()->value,v->this.value=v) .mapOutputToObj(s->s*2); assertThat(ext.get(),equalTo(400f)); }
@Test public void externalMapInputObj(){ value = 0; Mutable<Float> ext = MutableFloat.fromExternal(()->value, v->this.value=v) .mapInputToObj(s->new Float((float)(s+ten))); ext.set((float)50); assertThat(value,equalTo((float)60)); }
@Test public void testSet() { assertThat(new MutableFloat().set(twenty).getAsFloat(),is(twenty)); }
@Test public void testClosedVarHashCodeFalse() { assertThat(new MutableFloat(ten).hashCode(),not(equalTo(new MutableFloat(twenty).hashCode()))); } float value = 0;
@Test public void inClosure(){ MutableFloat myInt = new MutableFloat(zero); Function<Integer,Function<Integer,MutableFloat>> fn = ((Integer i)-> (Integer j)-> myInt.set(new Float((float)(i*j)))); fn.apply(10).apply(20); assertThat(myInt.getAsFloat(), is((float)200)); } @Test
return new MutableFloat() { @Override public float getAsFloat() {
@Override public Mutable<T1> set(final T1 value) { host.set(fn.apply(value)); return this; }
@Override public R get() { return fn.apply(host.get()); }
@Test public void testClosedVarEqualsFalse() { assertThat(new MutableFloat(ten),not(equalTo(new MutableFloat(twenty)))); } @Test
@Override public void accept(final Float t) { set(t); }
@Test public void testClosedVarEquals() { assertThat(new MutableFloat(ten),equalTo(new MutableFloat(ten))); } @Test