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