/** * 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); }
return new MutableFloat() { @Override public float getAsFloat() {
@Test public void testClosedVarEqualsFalse() { assertThat(new MutableFloat(ten),not(equalTo(new MutableFloat(twenty)))); } @Test
@Test public void testClosedVarEquals() { assertThat(new MutableFloat(ten),equalTo(new MutableFloat(ten))); } @Test
@Test public void testClosedVarHashCodeFalse() { assertThat(new MutableFloat(ten).hashCode(),not(equalTo(new MutableFloat(twenty).hashCode()))); } float value = 0;
@Test public void testClosedVarHashCode() { assertThat(new MutableFloat(ten).hashCode(),equalTo(new MutableFloat(ten).hashCode())); } @Test
@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
@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 testClosedVar() { assertThat(new MutableFloat(ten).getAsFloat(),equalTo(ten)); } @Test
@Test public void testSet() { assertThat(new MutableFloat().set(twenty).getAsFloat(),is(twenty)); }
/** * 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); }
return new MutableFloat() { @Override public float getAsFloat() {