001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.storage;
018    
019    import jet.Function0;
020    import jet.Function1;
021    import jet.Unit;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.annotations.Nullable;
024    
025    public interface StorageManager {
026        /**
027         * Given a function compute: K -> V create a memoized version of it that computes a value only once for each key
028         * @param compute the function to be memoized
029         * @param valuesReferenceKind how to store the memoized values
030         *
031         * NOTE: if compute() has side-effects the WEAK reference kind is dangerous: the side-effects will be repeated if
032         *       the value gets collected and then re-computed
033         */
034        @NotNull
035        <K, V> MemoizedFunctionToNotNull<K, V> createMemoizedFunction(@NotNull Function1<K, V> compute);
036        @NotNull
037        <K, V> MemoizedFunctionToNullable<K, V> createMemoizedFunctionWithNullableValues(@NotNull Function1<K, V> compute);
038    
039        @NotNull
040        <T> NotNullLazyValue<T> createLazyValue(@NotNull Function0<T> computable);
041    
042        @NotNull
043        <T> NotNullLazyValue<T> createRecursionTolerantLazyValue(@NotNull Function0<T> computable, @NotNull T onRecursiveCall);
044    
045        /**
046         * @param onRecursiveCall is called if the computation calls itself recursively.
047         *                        The parameter to it is {@code true} for the first call, {@code false} otherwise.
048         *                        If {@code onRecursiveCall} is {@code null}, an exception will be thrown on a recursive call,
049         *                        otherwise it's executed and its result is returned
050         * @param postCompute is called after the value is computed, but before any other thread sees it
051         */
052        @NotNull
053        <T> NotNullLazyValue<T> createLazyValueWithPostCompute(
054                @NotNull Function0<T> computable,
055                @Nullable Function1<Boolean, T> onRecursiveCall,
056                @NotNull Function1<T, Unit> postCompute
057        );
058    
059        @NotNull
060        <T> NullableLazyValue<T> createNullableLazyValue(@NotNull Function0<T> computable);
061    
062        @NotNull
063        <T> NullableLazyValue<T> createRecursionTolerantNullableLazyValue(@NotNull Function0<T> computable, @Nullable T onRecursiveCall);
064    
065        /**
066         * {@code postCompute} is called after the value is computed, but before any other thread sees it (the current thread may
067         * see it in between)
068         */
069        @NotNull
070        <T> NullableLazyValue<T> createNullableLazyValueWithPostCompute(@NotNull Function0<T> computable, @NotNull Function1<T, Unit> postCompute);
071    
072        <T> T compute(@NotNull Function0<T> computable);
073    }