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