/** * Create a Mutable variable, which can be mutated inside a Closure * * e.g. * <pre>{@code * MutableBoolean num = MutableBoolean.of(true); * * num.mutate(n->!n)) * * System.out.println(num.getAsBoolean()); * //prints false * } </pre> * * @param var Initial value of Mutable * @return New Mutable instance */ public static <T> MutableBoolean of(final boolean var) { return new MutableBoolean( var); }
return new MutableBoolean() { @Override public boolean getAsBoolean() {
@Test public void testClosedVarEquals() { assertThat(new MutableBoolean(trueB),equalTo(new MutableBoolean(trueB))); } @Test
@Test public void testClosedVarEqualsFalse() { assertThat(new MutableBoolean(trueB),not(equalTo(new MutableBoolean(falseB)))); } @Test
@Test public void testClosedVarHashCodeFalse() { assertThat(new MutableBoolean(trueB).hashCode(),not(equalTo(new MutableBoolean(falseB).hashCode()))); } boolean value = false;
@Test public void testClosedVarHashCode() { assertThat(new MutableBoolean(trueB).hashCode(),equalTo(new MutableBoolean(trueB).hashCode())); } @Test
@Test public void inClosure(){ MutableBoolean myInt = new MutableBoolean(zero); Function<Integer,Function<Integer,MutableBoolean>> fn = ( (Integer i)-> (Integer j)-> myInt.set(true)); fn.apply(10).apply(20); assertThat(myInt.getAsBoolean(), is(true)); } @Test
@Test public void testClosedVar() { assertThat(new MutableBoolean(trueB).getAsBoolean(),equalTo(trueB)); } @Test
@Test public void inClosure2(){ MutableBoolean myInt = new MutableBoolean(zero); BiFunction<Boolean,Boolean,MutableBoolean> fn = (i,j)-> myInt.set(true); fn.apply(trueB,falseB); assertThat(myInt.getAsBoolean(), is(true)); }
@Test public void testSet() { assertThat(new MutableBoolean().set(falseB).getAsBoolean(),is(falseB)); }
/** * Create a Mutable variable, which can be mutated inside a Closure * * e.g. * <pre>{@code * MutableBoolean num = MutableBoolean.of(true); * * num.mutate(n->!n)) * * System.out.println(num.getAsBoolean()); * //prints false * } </pre> * * @param var Initial value of Mutable * @return New Mutable instance */ public static <T> MutableBoolean of(final boolean var) { return new MutableBoolean( var); }
return new MutableBoolean() { @Override public boolean getAsBoolean() {