Clear computable when value is calculated and CallOnceFunction is added
Relates to #KT-35256
This commit is contained in:
@@ -305,7 +305,10 @@ public class LockBasedStorageManager implements StorageManager {
|
||||
|
||||
private static class LockBasedLazyValue<T> implements NullableLazyValue<T> {
|
||||
private final LockBasedStorageManager storageManager;
|
||||
private final Function0<? extends T> computable;
|
||||
/**
|
||||
* Computable function is <code>null</code> when value has been calculated
|
||||
*/
|
||||
private Function0<? extends T> computable;
|
||||
|
||||
@Nullable
|
||||
private volatile Object value = NotValue.NOT_COMPUTED;
|
||||
@@ -359,6 +362,7 @@ public class LockBasedStorageManager implements StorageManager {
|
||||
postCompute(typedValue);
|
||||
|
||||
value = typedValue;
|
||||
computable = null;
|
||||
return typedValue;
|
||||
}
|
||||
catch (Throwable throwable) {
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.utils
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
|
||||
class CallOnceFunction<F, T>(private val defaultValue: T, delegate: Function1<F, T>) : Function1<F, T> {
|
||||
private val functionRef: AtomicReference<Function1<F, T>?> = AtomicReference(delegate)
|
||||
|
||||
override fun invoke(p1: F): T = functionRef.getAndSet(null)?.invoke(p1) ?: defaultValue
|
||||
}
|
||||
Reference in New Issue
Block a user