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