/** * 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 CheckedFunction1<T1, R> memoized() { if (isMemoized()) { return this; } else { final Map<T1, R> cache = new HashMap<>(); return (CheckedFunction1<T1, R> & Memoized) (t1) -> { synchronized (cache) { if (cache.containsKey(t1)) { return cache.get(t1); } else { final R value = apply(t1); cache.put(t1, 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 CheckedFunction1<T1, R> memoized() { if (isMemoized()) { return this; } else { final Map<T1, R> cache = new HashMap<>(); return (CheckedFunction1<T1, R> & Memoized) (t1) -> { synchronized (cache) { if (cache.containsKey(t1)) { return cache.get(t1); } else { final R value = apply(t1); cache.put(t1, value); return value; } } }; } }