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 jet.runtime.typeinfo.KotlinSignature; 023 import org.jetbrains.annotations.NotNull; 024 import org.jetbrains.annotations.Nullable; 025 026 public interface StorageManager { 027 /** 028 * Given a function compute: K -> V create a memoized version of it that computes a value only once for each key 029 * @param compute the function to be memoized 030 * @param valuesReferenceKind how to store the memoized values 031 * 032 * NOTE: if compute() has side-effects the WEAK reference kind is dangerous: the side-effects will be repeated if 033 * the value gets collected and then re-computed 034 */ 035 @NotNull 036 <K, V> MemoizedFunctionToNotNull<K, V> createMemoizedFunction(@NotNull Function1<K, V> compute); 037 @KotlinSignature( 038 "fun <K, V> createMemoizedFunctionWithNullableValues(compute: (K) -> V?): MemoizedFunctionToNullable<K, V>") 039 @NotNull 040 <K, V> MemoizedFunctionToNullable<K, V> createMemoizedFunctionWithNullableValues(@NotNull Function1<K, V> compute); 041 042 @NotNull 043 <T> NotNullLazyValue<T> createLazyValue(@NotNull Function0<T> computable); 044 045 @NotNull 046 <T> NotNullLazyValue<T> createRecursionTolerantLazyValue(@NotNull Function0<T> computable, @NotNull T onRecursiveCall); 047 048 /** 049 * @param onRecursiveCall is called if the computation calls itself recursively. 050 * The parameter to it is {@code true} for the first call, {@code false} otherwise. 051 * If {@code onRecursiveCall} is {@code null}, an exception will be thrown on a recursive call, 052 * otherwise it's executed and its result is returned 053 * @param postCompute is called after the value is computed, but before any other thread sees it 054 */ 055 @NotNull 056 <T> NotNullLazyValue<T> createLazyValueWithPostCompute( 057 @NotNull Function0<T> computable, 058 @Nullable Function1<Boolean, T> onRecursiveCall, 059 @NotNull Function1<T, Unit> postCompute 060 ); 061 062 @NotNull 063 <T> NullableLazyValue<T> createNullableLazyValue(@NotNull Function0<T> computable); 064 065 @NotNull 066 <T> NullableLazyValue<T> createRecursionTolerantNullableLazyValue(@NotNull Function0<T> computable, @Nullable T onRecursiveCall); 067 068 /** 069 * {@code postCompute} is called after the value is computed, but before any other thread sees it (the current thread may 070 * see it in between) 071 */ 072 @NotNull 073 <T> NullableLazyValue<T> createNullableLazyValueWithPostCompute(@NotNull Function0<T> computable, @NotNull Function1<T, Unit> postCompute); 074 075 <T> T compute(@NotNull Function0<T> computable); 076 }