@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
/** * Convert a Function into one that caches it's result * * @param fn Function to memoise * @return Memoised Function */ public static <T, R> Function1<T, R> memoizeFunction(final Function<T, R> fn) { final Map<T, R> lazy = new ConcurrentHashMap<>(); LazyImmutable<R> nullR = LazyImmutable.def(); return t -> t==null? nullR.computeIfAbsent(()->fn.apply(null)) : lazy.computeIfAbsent(t, fn); }
@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())); }
/** * 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; }
/** * @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; }
/** * 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 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
@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 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 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 setOnceLazy(){ LazyImmutable<Integer> value = new LazyImmutable<>(); Supplier s= () -> value.computeIfAbsent(()->10); assertThat(s.get(),is(10)); assertThat(value.computeIfAbsent(()->20),is(10)); }
/** * @return a defined, but unitialised LazyImmutable */ public static <T> LazyImmutable<T> def() { return new LazyImmutable<>(); }
@Override public void accept(final T t) { setOnce(t); }
@Test public void testRightIdentityUninitialised(){ LazyImmutable<Integer> m = new LazyImmutable<Integer>(); assertThat(m.<Integer>flatMap(LazyImmutable::of), equalTo( m)); } @Test
@Test public void testMapUninitialised(){ LazyImmutable<Integer> value = new LazyImmutable<Integer>(); LazyImmutable<Integer> value2 = value.map(i->i+10); assertThat(value,equalTo(value2)); } @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); }
@Override public Iterator<T> iterator() { return stream().iterator(); }
@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