StorageManager rewritten to Kotlin
This commit is contained in:
@@ -56,13 +56,13 @@ public class LockBasedStorageManager implements StorageManager {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@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>());
|
return createMemoizedFunction(compute, new ConcurrentHashMap<K, Object>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
protected <K, V> MemoizedFunctionToNotNull<K, V> createMemoizedFunction(
|
protected <K, V> MemoizedFunctionToNotNull<K, V> createMemoizedFunction(
|
||||||
@NotNull Function1<K, V> compute,
|
@NotNull Function1<? super K, ? extends V> compute,
|
||||||
@NotNull ConcurrentMap<K, Object> map
|
@NotNull ConcurrentMap<K, Object> map
|
||||||
) {
|
) {
|
||||||
return new MapBasedMemoizedFunctionToNotNull<K, V>(map, compute);
|
return new MapBasedMemoizedFunctionToNotNull<K, V>(map, compute);
|
||||||
@@ -70,13 +70,13 @@ public class LockBasedStorageManager implements StorageManager {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@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>());
|
return createMemoizedFunctionWithNullableValues(compute, new ConcurrentHashMap<K, Object>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
protected <K, V> MemoizedFunctionToNullable<K, V> createMemoizedFunctionWithNullableValues(
|
protected <K, V> MemoizedFunctionToNullable<K, V> createMemoizedFunctionWithNullableValues(
|
||||||
@NotNull Function1<K, V> compute,
|
@NotNull Function1<? super K, ? extends V> compute,
|
||||||
@NotNull ConcurrentMap<K, Object> map
|
@NotNull ConcurrentMap<K, Object> map
|
||||||
) {
|
) {
|
||||||
return new MapBasedMemoizedFunction<K, V>(map, compute);
|
return new MapBasedMemoizedFunction<K, V>(map, compute);
|
||||||
@@ -84,14 +84,14 @@ public class LockBasedStorageManager implements StorageManager {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@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);
|
return new LockBasedNotNullLazyValue<T>(computable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public <T> NotNullLazyValue<T> createRecursionTolerantLazyValue(
|
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) {
|
return new LockBasedNotNullLazyValue<T>(computable) {
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -105,9 +105,9 @@ public class LockBasedStorageManager implements StorageManager {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public <T> NotNullLazyValue<T> createLazyValueWithPostCompute(
|
public <T> NotNullLazyValue<T> createLazyValueWithPostCompute(
|
||||||
@NotNull Function0<T> computable,
|
@NotNull Function0<? extends T> computable,
|
||||||
final Function1<Boolean, T> onRecursiveCall,
|
final Function1<? super Boolean, ? extends T> onRecursiveCall,
|
||||||
@NotNull final Function1<T, Unit> postCompute
|
@NotNull final Function1<? super T, ? extends Unit> postCompute
|
||||||
) {
|
) {
|
||||||
return new LockBasedNotNullLazyValue<T>(computable) {
|
return new LockBasedNotNullLazyValue<T>(computable) {
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -128,13 +128,13 @@ public class LockBasedStorageManager implements StorageManager {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@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);
|
return new LockBasedLazyValue<T>(computable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@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) {
|
return new LockBasedLazyValue<T>(computable) {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
@@ -147,7 +147,7 @@ public class LockBasedStorageManager implements StorageManager {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public <T> NullableLazyValue<T> createNullableLazyValueWithPostCompute(
|
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) {
|
return new LockBasedLazyValue<T>(computable) {
|
||||||
@Override
|
@Override
|
||||||
@@ -158,7 +158,7 @@ public class LockBasedStorageManager implements StorageManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T compute(@NotNull Function0<T> computable) {
|
public <T> T compute(@NotNull Function0<? extends T> computable) {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
return computable.invoke();
|
return computable.invoke();
|
||||||
@@ -216,12 +216,12 @@ public class LockBasedStorageManager implements StorageManager {
|
|||||||
|
|
||||||
private class LockBasedLazyValue<T> implements NullableLazyValue<T> {
|
private class LockBasedLazyValue<T> implements NullableLazyValue<T> {
|
||||||
|
|
||||||
private final Function0<T> computable;
|
private final Function0<? extends T> computable;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private volatile Object value = NotValue.NOT_COMPUTED;
|
private volatile Object value = NotValue.NOT_COMPUTED;
|
||||||
|
|
||||||
public LockBasedLazyValue(@NotNull Function0<T> computable) {
|
public LockBasedLazyValue(@NotNull Function0<? extends T> computable) {
|
||||||
this.computable = computable;
|
this.computable = computable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,7 +291,7 @@ public class LockBasedStorageManager implements StorageManager {
|
|||||||
|
|
||||||
private class LockBasedNotNullLazyValue<T> extends LockBasedLazyValue<T> implements NotNullLazyValue<T> {
|
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);
|
super(computable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,9 +306,9 @@ public class LockBasedStorageManager implements StorageManager {
|
|||||||
|
|
||||||
private class MapBasedMemoizedFunction<K, V> implements MemoizedFunctionToNullable<K, V> {
|
private class MapBasedMemoizedFunction<K, V> implements MemoizedFunctionToNullable<K, V> {
|
||||||
private final ConcurrentMap<K, Object> cache;
|
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.cache = map;
|
||||||
this.compute = compute;
|
this.compute = compute;
|
||||||
}
|
}
|
||||||
@@ -361,7 +361,7 @@ public class LockBasedStorageManager implements StorageManager {
|
|||||||
|
|
||||||
public MapBasedMemoizedFunctionToNotNull(
|
public MapBasedMemoizedFunctionToNotNull(
|
||||||
@NotNull ConcurrentMap<K, Object> map,
|
@NotNull ConcurrentMap<K, Object> map,
|
||||||
@NotNull Function1<K, V> compute
|
@NotNull Function1<? super K, ? extends V> compute
|
||||||
) {
|
) {
|
||||||
super(map, compute);
|
super(map, compute);
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-31
@@ -14,16 +14,10 @@
|
|||||||
* limitations under the License.
|
* 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
|
* 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
|
* @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
|
* 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
|
* the value gets collected and then re-computed
|
||||||
*/
|
*/
|
||||||
@NotNull
|
fun createMemoizedFunction<K, V: Any>(compute: (K) -> V): MemoizedFunctionToNotNull<K, V>
|
||||||
<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);
|
|
||||||
|
|
||||||
@NotNull
|
fun createMemoizedFunctionWithNullableValues<K, V: Any>(compute: (K) -> V?): MemoizedFunctionToNullable<K, V>
|
||||||
<T> NotNullLazyValue<T> createLazyValue(@NotNull Function0<T> computable);
|
|
||||||
|
|
||||||
@NotNull
|
fun createLazyValue<T: Any>(computable: () -> T): NotNullLazyValue<T>
|
||||||
<T> NotNullLazyValue<T> createRecursionTolerantLazyValue(@NotNull Function0<T> computable, @NotNull T onRecursiveCall);
|
|
||||||
|
fun createRecursionTolerantLazyValue<T: Any>(computable: () -> T, onRecursiveCall: T): NotNullLazyValue<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param onRecursiveCall is called if the computation calls itself recursively.
|
* @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
|
* 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
|
* @param postCompute is called after the value is computed, but before any other thread sees it
|
||||||
*/
|
*/
|
||||||
@NotNull
|
fun createLazyValueWithPostCompute<T: Any>(computable: () -> T, onRecursiveCall: ((Boolean) -> T)?, postCompute: (T) -> Unit): NotNullLazyValue<T>
|
||||||
<T> NotNullLazyValue<T> createLazyValueWithPostCompute(
|
|
||||||
@NotNull Function0<T> computable,
|
|
||||||
@Nullable Function1<Boolean, T> onRecursiveCall,
|
|
||||||
@NotNull Function1<T, Unit> postCompute
|
|
||||||
);
|
|
||||||
|
|
||||||
@NotNull
|
fun createNullableLazyValue<T: Any>(computable: () -> T?): NullableLazyValue<T>
|
||||||
<T> NullableLazyValue<T> createNullableLazyValue(@NotNull Function0<T> computable);
|
|
||||||
|
|
||||||
@NotNull
|
fun createRecursionTolerantNullableLazyValue<T: Any>(computable: () -> T?, onRecursiveCall: T?): NullableLazyValue<T>
|
||||||
<T> NullableLazyValue<T> createRecursionTolerantNullableLazyValue(@NotNull Function0<T> computable, @Nullable T onRecursiveCall);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@code postCompute} is called after the value is computed, but before any other thread sees it (the current thread may
|
* {@code postCompute} is called after the value is computed, but before any other thread sees it (the current thread may
|
||||||
* see it in between)
|
* see it in between)
|
||||||
*/
|
*/
|
||||||
@NotNull
|
fun createNullableLazyValueWithPostCompute<T: Any>(computable: () -> T?, postCompute: (T?) -> Unit): NullableLazyValue<T>
|
||||||
<T> NullableLazyValue<T> createNullableLazyValueWithPostCompute(@NotNull Function0<T> computable, @NotNull Function1<T, Unit> postCompute);
|
|
||||||
|
|
||||||
<T> T compute(@NotNull Function0<T> computable);
|
fun compute<T>(computable: () -> T): T
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user