/** * Returns a memoizing version of this function, which computes the return value for given arguments only one time. * On subsequent calls given the same arguments the memoized value is returned. * <p> * Please note that memoizing functions do not permit {@code null} as single argument or return value. * * @return a memoizing function equivalent to this. */ default CheckedFunction4<T1, T2, T3, T4, R> memoized() { if (isMemoized()) { return this; } else { final Map<Tuple4<T1, T2, T3, T4>, R> cache = new HashMap<>(); return (CheckedFunction4<T1, T2, T3, T4, R> & Memoized) (t1, t2, t3, t4) -> { final Tuple4<T1, T2, T3, T4> key = Tuple.of(t1, t2, t3, t4); synchronized (cache) { if (cache.containsKey(key)) { return cache.get(key); } else { final R value = tupled().apply(key); cache.put(key, value); return value; } } }; } }
/** * Returns a memoizing version of this function, which computes the return value for given arguments only one time. * On subsequent calls given the same arguments the memoized value is returned. * <p> * Please note that memoizing functions do not permit {@code null} as single argument or return value. * * @return a memoizing function equivalent to this. */ default CheckedFunction4<T1, T2, T3, T4, R> memoized() { if (isMemoized()) { return this; } else { final Map<Tuple4<T1, T2, T3, T4>, R> cache = new HashMap<>(); return (CheckedFunction4<T1, T2, T3, T4, R> & Memoized) (t1, t2, t3, t4) -> { final Tuple4<T1, T2, T3, T4> key = Tuple.of(t1, t2, t3, t4); synchronized (cache) { if (cache.containsKey(key)) { return cache.get(key); } else { final R value = tupled().apply(key); cache.put(key, value); return value; } } }; } }