StorageManager rewritten to Kotlin

This commit is contained in:
Andrey Breslav
2013-11-21 14:21:55 +04:00
parent 81069cd213
commit 0f9b9ed245
2 changed files with 31 additions and 50 deletions
@@ -56,13 +56,13 @@ public class LockBasedStorageManager implements StorageManager {
@NotNull
@Override
public <K, V> MemoizedFunctionToNotNull<K, V> createMemoizedFunction(@NotNull Function1<K, V> compute) {
public <K, V> MemoizedFunctionToNotNull<K, V> createMemoizedFunction(@NotNull Function1<? super K, ? extends V> compute) {
return createMemoizedFunction(compute, new ConcurrentHashMap<K, Object>());
}
@NotNull
protected <K, V> MemoizedFunctionToNotNull<K, V> createMemoizedFunction(
@NotNull Function1<K, V> compute,
@NotNull Function1<? super K, ? extends V> compute,
@NotNull ConcurrentMap<K, Object> map
) {
return new MapBasedMemoizedFunctionToNotNull<K, V>(map, compute);
@@ -70,13 +70,13 @@ public class LockBasedStorageManager implements StorageManager {
@NotNull
@Override
public <K, V> MemoizedFunctionToNullable<K, V> createMemoizedFunctionWithNullableValues(@NotNull Function1<K, V> compute) {
public <K, V> MemoizedFunctionToNullable<K, V> createMemoizedFunctionWithNullableValues(@NotNull Function1<? super K, ? extends V> compute) {
return createMemoizedFunctionWithNullableValues(compute, new ConcurrentHashMap<K, Object>());
}
@NotNull
protected <K, V> MemoizedFunctionToNullable<K, V> createMemoizedFunctionWithNullableValues(
@NotNull Function1<K, V> compute,
@NotNull Function1<? super K, ? extends V> compute,
@NotNull ConcurrentMap<K, Object> map
) {
return new MapBasedMemoizedFunction<K, V>(map, compute);
@@ -84,14 +84,14 @@ public class LockBasedStorageManager implements StorageManager {
@NotNull
@Override
public <T> NotNullLazyValue<T> createLazyValue(@NotNull Function0<T> computable) {
public <T> NotNullLazyValue<T> createLazyValue(@NotNull Function0<? extends T> computable) {
return new LockBasedNotNullLazyValue<T>(computable);
}
@NotNull
@Override
public <T> NotNullLazyValue<T> createRecursionTolerantLazyValue(
@NotNull Function0<T> computable, @NotNull final T onRecursiveCall
@NotNull Function0<? extends T> computable, @NotNull final T onRecursiveCall
) {
return new LockBasedNotNullLazyValue<T>(computable) {
@NotNull
@@ -105,9 +105,9 @@ public class LockBasedStorageManager implements StorageManager {
@NotNull
@Override
public <T> NotNullLazyValue<T> createLazyValueWithPostCompute(
@NotNull Function0<T> computable,
final Function1<Boolean, T> onRecursiveCall,
@NotNull final Function1<T, Unit> postCompute
@NotNull Function0<? extends T> computable,
final Function1<? super Boolean, ? extends T> onRecursiveCall,
@NotNull final Function1<? super T, ? extends Unit> postCompute
) {
return new LockBasedNotNullLazyValue<T>(computable) {
@NotNull
@@ -128,13 +128,13 @@ public class LockBasedStorageManager implements StorageManager {
@NotNull
@Override
public <T> NullableLazyValue<T> createNullableLazyValue(@NotNull Function0<T> computable) {
public <T> NullableLazyValue<T> createNullableLazyValue(@NotNull Function0<? extends T> computable) {
return new LockBasedLazyValue<T>(computable);
}
@NotNull
@Override
public <T> NullableLazyValue<T> createRecursionTolerantNullableLazyValue(@NotNull Function0<T> computable, final T onRecursiveCall) {
public <T> NullableLazyValue<T> createRecursionTolerantNullableLazyValue(@NotNull Function0<? extends T> computable, final T onRecursiveCall) {
return new LockBasedLazyValue<T>(computable) {
@NotNull
@Override
@@ -147,7 +147,7 @@ public class LockBasedStorageManager implements StorageManager {
@NotNull
@Override
public <T> NullableLazyValue<T> createNullableLazyValueWithPostCompute(
@NotNull Function0<T> computable, @NotNull final Function1<T, Unit> postCompute
@NotNull Function0<? extends T> computable, @NotNull final Function1<? super T, ? extends Unit> postCompute
) {
return new LockBasedLazyValue<T>(computable) {
@Override
@@ -158,7 +158,7 @@ public class LockBasedStorageManager implements StorageManager {
}
@Override
public <T> T compute(@NotNull Function0<T> computable) {
public <T> T compute(@NotNull Function0<? extends T> computable) {
lock.lock();
try {
return computable.invoke();
@@ -216,12 +216,12 @@ public class LockBasedStorageManager implements StorageManager {
private class LockBasedLazyValue<T> implements NullableLazyValue<T> {
private final Function0<T> computable;
private final Function0<? extends T> computable;
@Nullable
private volatile Object value = NotValue.NOT_COMPUTED;
public LockBasedLazyValue(@NotNull Function0<T> computable) {
public LockBasedLazyValue(@NotNull Function0<? extends T> computable) {
this.computable = computable;
}
@@ -291,7 +291,7 @@ public class LockBasedStorageManager implements StorageManager {
private class LockBasedNotNullLazyValue<T> extends LockBasedLazyValue<T> implements NotNullLazyValue<T> {
public LockBasedNotNullLazyValue(@NotNull Function0<T> computable) {
public LockBasedNotNullLazyValue(@NotNull Function0<? extends T> computable) {
super(computable);
}
@@ -306,9 +306,9 @@ public class LockBasedStorageManager implements StorageManager {
private class MapBasedMemoizedFunction<K, V> implements MemoizedFunctionToNullable<K, V> {
private final ConcurrentMap<K, Object> cache;
private final Function1<K, V> compute;
private final Function1<? super K, ? extends V> compute;
public MapBasedMemoizedFunction(@NotNull ConcurrentMap<K, Object> map, @NotNull Function1<K, V> compute) {
public MapBasedMemoizedFunction(@NotNull ConcurrentMap<K, Object> map, @NotNull Function1<? super K, ? extends V> compute) {
this.cache = map;
this.compute = compute;
}
@@ -361,7 +361,7 @@ public class LockBasedStorageManager implements StorageManager {
public MapBasedMemoizedFunctionToNotNull(
@NotNull ConcurrentMap<K, Object> map,
@NotNull Function1<K, V> compute
@NotNull Function1<? super K, ? extends V> compute
) {
super(map, compute);
}
@@ -14,16 +14,10 @@
* limitations under the License.
*/
package org.jetbrains.jet.storage;
package org.jetbrains.jet.storage
import jet.Function0;
import jet.Function1;
import jet.Unit;
import jet.runtime.typeinfo.KotlinSignature;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface StorageManager {
public trait StorageManager {
/**
* Given a function compute: K -> V create a memoized version of it that computes a value only once for each key
* @param compute the function to be memoized
@@ -32,18 +26,13 @@ public interface StorageManager {
* NOTE: if compute() has side-effects the WEAK reference kind is dangerous: the side-effects will be repeated if
* the value gets collected and then re-computed
*/
@NotNull
<K, V> MemoizedFunctionToNotNull<K, V> createMemoizedFunction(@NotNull Function1<K, V> compute);
@KotlinSignature(
"fun <K, V> createMemoizedFunctionWithNullableValues(compute: (K) -> V?): MemoizedFunctionToNullable<K, V>")
@NotNull
<K, V> MemoizedFunctionToNullable<K, V> createMemoizedFunctionWithNullableValues(@NotNull Function1<K, V> compute);
fun createMemoizedFunction<K, V: Any>(compute: (K) -> V): MemoizedFunctionToNotNull<K, V>
@NotNull
<T> NotNullLazyValue<T> createLazyValue(@NotNull Function0<T> computable);
fun createMemoizedFunctionWithNullableValues<K, V: Any>(compute: (K) -> V?): MemoizedFunctionToNullable<K, V>
@NotNull
<T> NotNullLazyValue<T> createRecursionTolerantLazyValue(@NotNull Function0<T> computable, @NotNull T onRecursiveCall);
fun createLazyValue<T: Any>(computable: () -> T): NotNullLazyValue<T>
fun createRecursionTolerantLazyValue<T: Any>(computable: () -> T, onRecursiveCall: T): NotNullLazyValue<T>
/**
* @param onRecursiveCall is called if the computation calls itself recursively.
@@ -52,25 +41,17 @@ public interface StorageManager {
* otherwise it's executed and its result is returned
* @param postCompute is called after the value is computed, but before any other thread sees it
*/
@NotNull
<T> NotNullLazyValue<T> createLazyValueWithPostCompute(
@NotNull Function0<T> computable,
@Nullable Function1<Boolean, T> onRecursiveCall,
@NotNull Function1<T, Unit> postCompute
);
fun createLazyValueWithPostCompute<T: Any>(computable: () -> T, onRecursiveCall: ((Boolean) -> T)?, postCompute: (T) -> Unit): NotNullLazyValue<T>
@NotNull
<T> NullableLazyValue<T> createNullableLazyValue(@NotNull Function0<T> computable);
fun createNullableLazyValue<T: Any>(computable: () -> T?): NullableLazyValue<T>
@NotNull
<T> NullableLazyValue<T> createRecursionTolerantNullableLazyValue(@NotNull Function0<T> computable, @Nullable T onRecursiveCall);
fun createRecursionTolerantNullableLazyValue<T: Any>(computable: () -> T?, onRecursiveCall: T?): NullableLazyValue<T>
/**
* {@code postCompute} is called after the value is computed, but before any other thread sees it (the current thread may
* see it in between)
*/
@NotNull
<T> NullableLazyValue<T> createNullableLazyValueWithPostCompute(@NotNull Function0<T> computable, @NotNull Function1<T, Unit> postCompute);
fun createNullableLazyValueWithPostCompute<T: Any>(computable: () -> T?, postCompute: (T?) -> Unit): NullableLazyValue<T>
<T> T compute(@NotNull Function0<T> computable);
fun compute<T>(computable: () -> T): T
}