/** * Create a Mutable variable, which can be mutated inside a Closure * * e.g. * <pre>{@code * MutableChar char = MutableChar.of('c'); * * char.mutate(n->'d')) * * System.out.println(num.getAsChar()); * //prints d * } </pre> * * @param var Initial value of Mutable * @return New Mutable instance */ public static <T> MutableChar of(final char var) { return new MutableChar( var); }
return new MutableChar() { @Override public char getAsChar() {
@Test public void testClosedVarEqualsFalse() { assertThat(new MutableChar(ten),not(equalTo(new MutableChar(twenty)))); } @Test
@Test public void testClosedVarEquals() { assertThat(new MutableChar(ten),equalTo(new MutableChar(ten))); } @Test
@Test public void testClosedVarHashCodeFalse() { assertThat(new MutableChar(ten).hashCode(),not(equalTo(new MutableChar(twenty).hashCode()))); } char value = 0;
@Test public void testClosedVarHashCode() { assertThat(new MutableChar(ten).hashCode(),equalTo(new MutableChar(ten).hashCode())); } @Test
@Test public void inClosure(){ MutableChar myInt = new MutableChar(zero); Function<Integer,Function<Integer,MutableChar>> fn = ((Integer i)-> (Integer j)-> myInt.set(new Character((char)(i*j)))); fn.apply(10).apply(20); assertThat(myInt.getAsChar(), is((char)200)); } @Test
@Test public void inClosure2(){ MutableChar myInt = new MutableChar(zero); BiFunction<Character,Character,MutableChar> fn = (i,j)-> myInt.set(new Character((char)(i*j))); fn.apply(ten,twenty); assertThat(myInt.getAsChar(), is((char)200)); }
@Test public void testClosedVar() { assertThat(new MutableChar(ten).getAsChar(),equalTo(ten)); } @Test
@Test public void testSet() { assertThat(new MutableChar().set(twenty).getAsChar(),is(twenty)); }
/** * Create a Mutable variable, which can be mutated inside a Closure * * e.g. * <pre>{@code * MutableChar char = MutableChar.of('c'); * * char.mutate(n->'d')) * * System.out.println(num.getAsChar()); * //prints d * } </pre> * * @param var Initial value of Mutable * @return New Mutable instance */ public static <T> MutableChar of(final char var) { return new MutableChar( var); }
return new MutableChar() { @Override public char getAsChar() {