/** * FlatMap the value stored in Immutable Closed Value from one Value to another * If this is an unitiatilised ImmutableClosedValue, an uninitialised closed value will be returned instead * * @param fn Flat Mapper function * @return new ImmutableClosedValue with new mapped value */ public <R> LazyImmutable<? extends R> flatMap(final Function<? super T, ? extends LazyImmutable<? extends R>> fn) { final T val = get(); if (val == UNSET) return (LazyImmutable<R>) this; else return fn.apply(val); }
/** * Get the current value or set if it has not been set yet * * @param lazy Supplier to generate new value * @return Current value */ public T computeIfAbsent(final Supplier<T> lazy) { final T val = get(); if (val == UNSET) return setOnceFromSupplier(lazy); return val; }
/** * 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 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
@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
/** * FlatMap the value stored in Immutable Closed Value from one Value to another * If this is an unitiatilised ImmutableClosedValue, an uninitialised closed value will be returned instead * * @param fn Flat Mapper function * @return new ImmutableClosedValue with new mapped value */ public <R> LazyImmutable<? extends R> flatMap(final Function<? super T, ? extends LazyImmutable<? extends R>> fn) { final T val = get(); if (val == UNSET) return (LazyImmutable<R>) this; else return fn.apply(val); }
/** * Get the current value or set if it has not been set yet * * @param lazy Supplier to generate new value * @return Current value */ public T computeIfAbsent(final Supplier<T> lazy) { final T val = get(); if (val == UNSET) return setOnceFromSupplier(lazy); return val; }
/** * 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)); }