diff --git a/compiler/frontend/src/org/jetbrains/jet/storage/LockBasedLazyResolveStorageManager.kt b/compiler/frontend/src/org/jetbrains/jet/storage/LockBasedLazyResolveStorageManager.kt index e0f738e5aaa..03bbfa7af06 100644 --- a/compiler/frontend/src/org/jetbrains/jet/storage/LockBasedLazyResolveStorageManager.kt +++ b/compiler/frontend/src/org/jetbrains/jet/storage/LockBasedLazyResolveStorageManager.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * Copyright 2010-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,268 +14,59 @@ * limitations under the License. */ -package org.jetbrains.jet.storage; +package org.jetbrains.jet.storage -import com.google.common.collect.ImmutableMap; -import com.intellij.util.containers.ConcurrentWeakValueHashMap; -import kotlin.Function0; -import kotlin.Function1; -import kotlin.Unit; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.annotations.TestOnly; -import org.jetbrains.jet.lang.diagnostics.Diagnostic; -import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.jet.lang.resolve.BindingTrace; -import org.jetbrains.jet.lang.resolve.Diagnostics; -import org.jetbrains.jet.util.slicedmap.ReadOnlySlice; -import org.jetbrains.jet.util.slicedmap.WritableSlice; +import com.intellij.util.containers.ConcurrentWeakValueHashMap +import org.jetbrains.annotations.TestOnly +import org.jetbrains.jet.lang.diagnostics.Diagnostic +import org.jetbrains.jet.lang.resolve.BindingContext +import org.jetbrains.jet.lang.resolve.BindingTrace +import org.jetbrains.jet.lang.resolve.Diagnostics +import org.jetbrains.jet.util.slicedmap.ReadOnlySlice +import org.jetbrains.jet.util.slicedmap.WritableSlice -import java.util.Collection; -import java.util.concurrent.ConcurrentMap; +public class LockBasedLazyResolveStorageManager(private val storageManager: StorageManager): StorageManager by storageManager, LazyResolveStorageManager { + override fun createWeaklyRetainedMemoizedFunction(compute: Function1) = + storageManager.createMemoizedFunction(compute, ConcurrentWeakValueHashMap()) -// This class is kept under the same package as LockBasedStorageManager to get access to its protected members -// Otherwise wed have to expose the lock which is worse than have such a hackish class placement -public class LockBasedLazyResolveStorageManager implements LazyResolveStorageManager { + override fun createWeaklyRetainedMemoizedFunctionWithNullableValues(compute: Function1) = + storageManager.createMemoizedFunctionWithNullableValues(compute, ConcurrentWeakValueHashMap()) - private final StorageManager storageManager; + // It seems safe to have a separate lock for traces: + // no other locks will be acquired inside the trace operations + override fun createSafeTrace(originalTrace: BindingTrace) = + LockProtectedTrace(storageManager, originalTrace) - public LockBasedLazyResolveStorageManager(@NotNull StorageManager storageManager) { - this.storageManager = storageManager; + private class LockProtectedContext(private val storageManager: StorageManager, private val context: BindingContext) : BindingContext { + override fun getDiagnostics(): Diagnostics = storageManager.compute { context.getDiagnostics() } + + override fun get(slice: ReadOnlySlice, key: K) = storageManager.compute { context.get(slice, key) } + + override fun getKeys(slice: WritableSlice) = storageManager.compute { context.getKeys(slice) } + + TestOnly + override fun getSliceContents(slice: ReadOnlySlice) = storageManager.compute { context.getSliceContents(slice) } } - @Override - @NotNull - public MemoizedFunctionToNotNull createWeaklyRetainedMemoizedFunction( - @NotNull Function1 compute - ) { - return storageManager.createMemoizedFunction(compute, new ConcurrentWeakValueHashMap()); - } + private class LockProtectedTrace(private val storageManager: StorageManager, private val trace: BindingTrace) : BindingTrace { + private val context: BindingContext = LockProtectedContext(storageManager, trace.getBindingContext()) - @NotNull - @Override - public MemoizedFunctionToNullable createWeaklyRetainedMemoizedFunctionWithNullableValues( - @NotNull Function1 compute - ) { - return storageManager.createMemoizedFunctionWithNullableValues(compute, new ConcurrentWeakValueHashMap()); - } + override fun getBindingContext() = context - @NotNull - @Override - public BindingTrace createSafeTrace(@NotNull BindingTrace originalTrace) { - // It seems safe to have a separate lock for traces: - // no other locks will be acquired inside the trace operations - - return new LockProtectedTrace(storageManager, originalTrace); - } - - @NotNull - @Override - public MemoizedFunctionToNullable createMemoizedFunctionWithNullableValues( - @NotNull Function1 compute, @NotNull ConcurrentMap map - ) { - return storageManager.createMemoizedFunctionWithNullableValues(compute, map); - } - - @NotNull - @Override - public MemoizedFunctionToNotNull createMemoizedFunction( - @NotNull Function1 compute, @NotNull ConcurrentMap map - ) { - return storageManager.createMemoizedFunction(compute, map); - } - - @NotNull - @Override - public MemoizedFunctionToNotNull createMemoizedFunction(@NotNull Function1 compute) { - return storageManager.createMemoizedFunction(compute); - } - - @NotNull - @Override - public MemoizedFunctionToNullable createMemoizedFunctionWithNullableValues(@NotNull Function1 compute) { - return storageManager.createMemoizedFunctionWithNullableValues(compute); - } - - @NotNull - @Override - public NotNullLazyValue createLazyValue(@NotNull Function0 computable) { - return storageManager.createLazyValue(computable); - } - - @NotNull - @Override - public NotNullLazyValue createRecursionTolerantLazyValue( - @NotNull Function0 computable, - @NotNull T onRecursiveCall - ) { - return storageManager.createRecursionTolerantLazyValue(computable, onRecursiveCall); - } - - @NotNull - @Override - public NotNullLazyValue createLazyValueWithPostCompute( - @NotNull Function0 computable, - @Nullable Function1 onRecursiveCall, - @NotNull Function1 postCompute - ) { - return storageManager.createLazyValueWithPostCompute(computable, onRecursiveCall, postCompute); - } - - @NotNull - @Override - public NullableLazyValue createNullableLazyValue(@NotNull Function0 computable) { - return storageManager.createNullableLazyValue(computable); - } - - @NotNull - @Override - public NullableLazyValue createRecursionTolerantNullableLazyValue( - @NotNull Function0 computable, - T onRecursiveCall - ) { - return storageManager.createRecursionTolerantNullableLazyValue(computable, onRecursiveCall); - } - - @NotNull - @Override - public NullableLazyValue createNullableLazyValueWithPostCompute( - @NotNull Function0 computable, - @NotNull Function1 postCompute - ) { - return storageManager.createNullableLazyValueWithPostCompute(computable, postCompute); - } - - @Override - public T compute(@NotNull Function0 computable) { - return storageManager.compute(computable); - } - - private static class LockProtectedContext implements BindingContext { - private final StorageManager storageManager; - private final BindingContext context; - - private LockProtectedContext(StorageManager storageManager, BindingContext context) { - this.storageManager = storageManager; - this.context = context; + override fun record(slice: WritableSlice, key: K, value: V) { + storageManager.compute { trace.record(slice, key, value) } } - @NotNull - @Override - public Diagnostics getDiagnostics() { - return storageManager.compute(new Function0() { - @Override - public Diagnostics invoke() { - return context.getDiagnostics(); - } - }); + override fun record(slice: WritableSlice, key: K) { + storageManager.compute { trace.record(slice, key) } } - @Nullable - @Override - public V get(final ReadOnlySlice slice, final K key) { - return storageManager.compute(new Function0() { - @Override - public V invoke() { - return context.get(slice, key); - } - }); - } + override fun get(slice: ReadOnlySlice, key: K): V = storageManager.compute { trace.get(slice, key) } - @NotNull - @Override - public Collection getKeys(final WritableSlice slice) { - return storageManager.compute(new Function0>() { - @Override - public Collection invoke() { - return context.getKeys(slice); - } - }); - } + override fun getKeys(slice: WritableSlice): Collection = storageManager.compute { trace.getKeys(slice) } - @NotNull - @Override - @TestOnly - public ImmutableMap getSliceContents(@NotNull final ReadOnlySlice slice) { - return storageManager.compute(new Function0>() { - @Override - public ImmutableMap invoke() { - return context.getSliceContents(slice); - } - }); - } - } - - private static class LockProtectedTrace implements BindingTrace { - private final BindingTrace trace; - private final BindingContext context; - private final StorageManager storageManager; - - public LockProtectedTrace(@NotNull StorageManager storageManager, @NotNull BindingTrace trace) { - this.storageManager = storageManager; - this.trace = trace; - this.context = new LockProtectedContext(storageManager, trace.getBindingContext()); - } - - @NotNull - @Override - public BindingContext getBindingContext() { - return context; - } - - @Override - public void record(final WritableSlice slice, final K key, final V value) { - storageManager.compute(new Function0() { - @Override - public Unit invoke() { - trace.record(slice, key, value); - return Unit.INSTANCE$; - } - }); - } - - @Override - public void record(final WritableSlice slice, final K key) { - storageManager.compute(new Function0() { - @Override - public Unit invoke() { - trace.record(slice, key); - return Unit.INSTANCE$; - } - }); - } - - @Override - @Nullable - public V get(final ReadOnlySlice slice, final K key) { - return storageManager.compute(new Function0() { - @Override - public V invoke() { - return trace.get(slice, key); - } - }); - } - - @Override - @NotNull - public Collection getKeys(final WritableSlice slice) { - return storageManager.compute(new Function0>() { - @Override - public Collection invoke() { - return trace.getKeys(slice); - } - }); - } - - @Override - public void report(@NotNull final Diagnostic diagnostic) { - storageManager.compute(new Function0() { - @Override - public Unit invoke() { - trace.report(diagnostic); - return Unit.INSTANCE$; - } - }); + override fun report(diagnostic: Diagnostic) { + storageManager.compute { trace.report(diagnostic) } } } } \ No newline at end of file