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
017package org.jetbrains.jet.lang.resolve.lazy.storage;
018
019import com.intellij.openapi.util.Computable;
020import com.intellij.util.Consumer;
021import com.intellij.util.Function;
022import org.jetbrains.annotations.NotNull;
023import org.jetbrains.jet.lang.resolve.BindingTrace;
024
025public 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 Function<K, V> compute, @NotNull ReferenceKind valuesReferenceKind);
036    @NotNull
037    <K, V> MemoizedFunctionToNullable<K, V> createMemoizedFunctionWithNullableValues(@NotNull Function<K, V> compute, @NotNull ReferenceKind valuesReferenceKind);
038
039    @NotNull
040    <T> NotNullLazyValue<T> createLazyValue(@NotNull Computable<T> computable);
041
042    /**
043     * {@code postCompute} is called after the value is computed, but before any other thread sees it (the current thread may
044     * see it in between)
045     */
046    @NotNull
047    <T> NotNullLazyValue<T> createLazyValueWithPostCompute(@NotNull Computable<T> computable, @NotNull Consumer<T> postCompute);
048
049    @NotNull
050    <T> NullableLazyValue<T> createNullableLazyValue(@NotNull Computable<T> computable);
051
052    /**
053     * {@code postCompute} is called after the value is computed, but before any other thread sees it (the current thread may
054     * see it in between)
055     */
056    @NotNull
057    <T> NullableLazyValue<T> createNullableLazyValueWithPostCompute(@NotNull Computable<T> computable, @NotNull Consumer<T> postCompute);
058
059    @NotNull
060    BindingTrace createSafeTrace(@NotNull BindingTrace originalTrace);
061
062    enum ReferenceKind {
063        STRONG,
064        WEAK
065    }
066}