/** * 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 CheckedFunction5<T1, T2, T3, T4, T5, R> memoized() { if (isMemoized()) { return this; } else { final Map<Tuple5<T1, T2, T3, T4, T5>, R> cache = new HashMap<>(); return (CheckedFunction5<T1, T2, T3, T4, T5, R> & Memoized) (t1, t2, t3, t4, t5) -> { final Tuple5<T1, T2, T3, T4, T5> key = Tuple.of(t1, t2, t3, t4, t5); 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 CheckedFunction5<T1, T2, T3, T4, T5, R> memoized() { if (isMemoized()) { return this; } else { final Map<Tuple5<T1, T2, T3, T4, T5>, R> cache = new HashMap<>(); return (CheckedFunction5<T1, T2, T3, T4, T5, R> & Memoized) (t1, t2, t3, t4, t5) -> { final Tuple5<T1, T2, T3, T4, T5> key = Tuple.of(t1, t2, t3, t4, t5); synchronized (cache) { if (cache.containsKey(key)) { return cache.get(key); } else { final R value = tupled().apply(key); cache.put(key, value); return value; } } }; } }