/** * Map the value stored in this Immutable Closed Value from one Value to another * If this is an unitiatilised ImmutableClosedValue, an uninitialised closed value will be returned instead * * @param fn Mapper function * @return new ImmutableClosedValue with new mapped value */ @Override public <R> LazyImmutable<R> map(final Function<? super T, ? extends R> fn) { final T val = get(); if (val == UNSET) return (LazyImmutable<R>) this; else return LazyImmutable.of(fn.apply(val)); }
@Test public void associativity(){ int a = 10; LazyImmutable<Integer> m = LazyImmutable.of(a); Function<Integer,LazyImmutable<Integer> >f = i->LazyImmutable.of(i+10); Function<Integer,LazyImmutable<Integer> >g = i->LazyImmutable.of(i*10); assertThat(m.flatMap(f).flatMap(g).get(), equalTo( m.flatMap(x -> f.apply(x).flatMap(g)).get())); }
@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)))); } }
@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 testRightIdentity(){ int a = 10; LazyImmutable<Integer> m = LazyImmutable.of(a); assertThat(m.flatMap(LazyImmutable::of).get(), equalTo( m.get())); } @Test
@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
/** * Map the value stored in this Immutable Closed Value from one Value to another * If this is an unitiatilised ImmutableClosedValue, an uninitialised closed value will be returned instead * * @param fn Mapper function * @return new ImmutableClosedValue with new mapped value */ @Override public <R> LazyImmutable<R> map(final Function<? super T, ? extends R> fn) { final T val = get(); if (val == UNSET) return (LazyImmutable<R>) this; else return LazyImmutable.of(fn.apply(val)); }