All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rapidpm.frp.memoizer.Memoizer Maven / Gradle / Ivy

There is a newer version: 01.00.07-RPM
Show newest version
/**
 * Copyright © 2017 Sven Ruppert ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.rapidpm.frp.memoizer;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

import org.rapidpm.frp.Transformations;
import org.rapidpm.frp.functions.TriFunction;

/**
 * Copyright (C) 2010 RapidPM
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 

* Created by RapidPM - Team on 10.12.16. * * @author svenruppert * @version $Id: $Id */ public class Memoizer { private final Map memoizationCache = new ConcurrentHashMap<>(); private Supplier doMemoize(final Supplier function) { return new Supplier() { private T value; @Override public T get() { if (value == null) value = function.get(); return value; } }; } private Function doMemoize(final Function function) { return input -> memoizationCache.computeIfAbsent(input, function); } /** *

memoize.

* * @param function a {@link java.util.function.Supplier} object. * @param a T object. * @return a {@link java.util.function.Supplier} object. */ public static Supplier memoize(final Supplier function) { return new Memoizer().doMemoize(function); } /** *

memoize.

* * @param function a {@link java.util.function.Function} object. * @param a T object. * @param a U object. * @return a {@link java.util.function.Function} object. */ public static Function memoize(final Function function) { return new Memoizer().doMemoize(function); } /** *

memoize.

* * @param biFunc a {@link java.util.function.BiFunction} object. * @param a T1 object. * @param a T2 object. * @param a R object. * @return a {@link java.util.function.BiFunction} object. */ public static BiFunction memoize(final BiFunction biFunc) { final Function> transformed = Memoizer.memoize(x -> Memoizer.memoize(y -> biFunc.apply(x, y))); return Transformations .unCurryBiFunction() .apply(transformed); } // public static BiFunction memoize(final BiFunction biFunc) { // final Function> transformed = Memoizer.memoize(x -> Memoizer.memoize(y -> biFunc.apply(x, y))); // return (x, y) -> transformed.apply(x).apply(y); // } /** *

memoize.

* * @param threeFunc a {@link org.rapidpm.frp.functions.TriFunction} object. * @param a T1 object. * @param a T2 object. * @param a T3 object. * @param a R object. * @return a {@link org.rapidpm.frp.functions.TriFunction} object. */ public static TriFunction memoize(final TriFunction threeFunc) { final Function>> transformed = Memoizer.memoize(x -> Memoizer.memoize(y -> Memoizer.memoize(z -> threeFunc.apply(x, y, z)))); return Transformations .unCurryTriFunction() .apply(transformed); // return (x, y, z) -> transformed.apply(x).apply(y).apply(z); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy