/** * 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); }
/** * Convert a Predicate into one that caches it's result * * @param p Predicate to memoise * @param cache Cachable to store the results * @return Memoised Predicate */ public static <T> Predicate<T> memoizePredicate(final Predicate<T> p, final Cacheable<Boolean> cache) { final Function<T, Boolean> memoised = memoizeFunction((Function<T, Boolean>) t -> p.test(t), cache); LazyImmutable<Boolean> nullR = LazyImmutable.def(); return (t) -> t==null? nullR.computeIfAbsent(()->p.test(null)) : memoised.apply(t); }
/** * Convert a Predicate into one that caches it's result * * @param p Predicate to memoise * @return Memoised Predicate */ public static <T> Predicate<T> memoizePredicate(final Predicate<T> p) { final Function<T, Boolean> memoised = memoizeFunction((Function<T, Boolean>) t -> p.test(t)); LazyImmutable<Boolean> nullR = LazyImmutable.def(); return (t) -> t==null? nullR.computeIfAbsent(()->p.test(null)) : memoised.apply(t); }
/** * Convert a Function into one that caches it's result * * @param fn Function to memoise * @param cache Cachable to store the results * @return Memoised Function */ public static <T, R> Function1<T, R> memoizeFunction(final Function<T, R> fn, final Cacheable<R> cache) { LazyImmutable<R> nullR = LazyImmutable.def(); return t -> t==null? nullR.computeIfAbsent(()->fn.apply(null)) : (R)cache.soften() .computeIfAbsent(t, (Function) fn); }
@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)); }
/** * 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); }
/** * Convert a Predicate into one that caches it's result * * @param p Predicate to memoise * @return Memoised Predicate */ public static <T> Predicate<T> memoizePredicate(final Predicate<T> p) { final Function<T, Boolean> memoised = memoizeFunction((Function<T, Boolean>) t -> p.test(t)); LazyImmutable<Boolean> nullR = LazyImmutable.def(); return (t) -> t==null? nullR.computeIfAbsent(()->p.test(null)) : memoised.apply(t); }
/** * Convert a Predicate into one that caches it's result * * @param p Predicate to memoise * @param cache Cachable to store the results * @return Memoised Predicate */ public static <T> Predicate<T> memoizePredicate(final Predicate<T> p, final Cacheable<Boolean> cache) { final Function<T, Boolean> memoised = memoizeFunction((Function<T, Boolean>) t -> p.test(t), cache); LazyImmutable<Boolean> nullR = LazyImmutable.def(); return (t) -> t==null? nullR.computeIfAbsent(()->p.test(null)) : memoised.apply(t); }
/** * Convert a Function into one that caches it's result * * @param fn Function to memoise * @param cache Cachable to store the results * @return Memoised Function */ public static <T, R> Function1<T, R> memoizeFunction(final Function<T, R> fn, final Cacheable<R> cache) { LazyImmutable<R> nullR = LazyImmutable.def(); return t -> t==null? nullR.computeIfAbsent(()->fn.apply(null)) : (R)cache.soften() .computeIfAbsent(t, (Function) fn); }