/** * @return a defined, but unitialised LazyImmutable */ public static <T> LazyImmutable<T> def() { return new LazyImmutable<>(); }
/** * Create an intermediate unbound (or unitialised) ImmutableClosedValue) * * @return unitialised ImmutableClosedValue */ public static <T> LazyImmutable<T> unbound() { return new LazyImmutable<>(); }
/** * @param value Create an initialised ImmutableClosedValue with specified value * @return Initialised ImmutableClosedValue */ public static <T> LazyImmutable<T> of(final T value) { final LazyImmutable<T> v = new LazyImmutable<>(); v.setOnce(value); return v; }
@Test public void testEqualsFalse(){ LazyImmutable<Integer> value = new LazyImmutable<Integer>(); value.setOnce(10); LazyImmutable<Integer> value2 = new LazyImmutable<Integer>(); value2.setOnce(20); assertThat(value,not(equalTo(value2))); } @Test
@Test public void testHashcode(){ LazyImmutable<Integer> value = new LazyImmutable<Integer>(); value.setOnce(10); LazyImmutable<Integer> value2 = new LazyImmutable<Integer>(); value2.setOnce(10); assertThat(value.get().hashCode(),equalTo(value2.get().hashCode())); } @Test
@Test public void testRightIdentityUninitialised(){ LazyImmutable<Integer> m = new LazyImmutable<Integer>(); assertThat(m.<Integer>flatMap(LazyImmutable::of), equalTo( m)); } @Test
@Test public void testHashcodeFalse(){ LazyImmutable<Integer> value = new LazyImmutable<Integer>(); value.setOnce(10); LazyImmutable<Integer> value2 = new LazyImmutable<Integer>(); value2.setOnce(20); assertThat(value.hashCode(),not(equalTo(value2.hashCode()))); }
@Test public void testMapUninitialised(){ LazyImmutable<Integer> value = new LazyImmutable<Integer>(); LazyImmutable<Integer> value2 = value.map(i->i+10); assertThat(value,equalTo(value2)); } @Test
@Test public void testEqualsTrue(){ LazyImmutable<Integer> value = new LazyImmutable<Integer>(); value.setOnce(10); LazyImmutable<Integer> value2 = new LazyImmutable<Integer>(); value2.setOnce(10); assertThat(value.get(),equalTo(value2.get())); } @Test
@Test public void setOnceLazy(){ LazyImmutable<Integer> value = new LazyImmutable<>(); Supplier s= () -> value.computeIfAbsent(()->10); assertThat(s.get(),is(10)); assertThat(value.computeIfAbsent(()->20),is(10)); }
@Test public void testFlatMapUninitialised(){ LazyImmutable<Integer> value = new LazyImmutable<Integer>(); LazyImmutable<? extends Integer> value2 = value.flatMap(i -> LazyImmutable.of(i + 10)); assertThat(value,equalTo(value2)); } @Test
@Test public void testSetOnce() { LazyImmutable<Integer> value = new LazyImmutable<>(); Supplier s= () -> value.setOnce(10).get(); assertThat(s.get(),is(10)); assertThat(value.get(),is(10)); } @Test
@Test public void testSetOnce2Attempts() { LazyImmutable<Integer> value = new LazyImmutable<>(); Supplier s= () -> value.setOnce(10); value.setOnce(20); //first time set s.get(); assertThat(value.get(),is(20)); }
@Test public void testMap2(){ LazyImmutable<Integer> value = new LazyImmutable<Integer>(); value.setOnce(10); LazyImmutable<Integer> value2 = value.map(i->i+10); assertThat(value2.get(),equalTo(20)); } @Test
/** * @return a defined, but unitialised LazyImmutable */ public static <T> LazyImmutable<T> def() { return new LazyImmutable<>(); }
@Test public void testFlatMap2(){ LazyImmutable<Integer> value = new LazyImmutable<Integer>(); value.setOnce(10); LazyImmutable<? extends Integer> value2 = value.flatMap(i -> LazyImmutable.of(i + 10)); assertThat(value2.get(),equalTo(20)); } @Test
/** * Create an intermediate unbound (or unitialised) ImmutableClosedValue) * * @return unitialised ImmutableClosedValue */ public static <T> LazyImmutable<T> unbound() { return new LazyImmutable<>(); }
@Test public void associativityUninitialised(){ int a = 10; LazyImmutable<Integer> m = new LazyImmutable<Integer>(); Function<Integer,LazyImmutable<Integer> >f = i->LazyImmutable.of(i+10); Function<Integer,LazyImmutable<Integer> >g = i->LazyImmutable.of(i*10); assertThat(m.<Integer>flatMap(f).<Integer>flatMap(g), equalTo( m.<Integer>flatMap(x -> f.apply(x).flatMap(g)))); } }
/** * @param value Create an initialised ImmutableClosedValue with specified value * @return Initialised ImmutableClosedValue */ public static <T> LazyImmutable<T> of(final T value) { final LazyImmutable<T> v = new LazyImmutable<>(); v.setOnce(value); return v; }