NullableLazyValue and NotNullLazyValue introduced

This commit is contained in:
Andrey Breslav
2013-02-12 15:10:25 +04:00
parent d276837833
commit 6e61ada816
13 changed files with 78 additions and 46 deletions
@@ -27,7 +27,7 @@ import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.ImportPath;
import org.jetbrains.jet.lang.resolve.TemporaryBindingTrace;
import org.jetbrains.jet.lang.resolve.lazy.descriptors.LazyClassDescriptor;
import org.jetbrains.jet.lang.resolve.lazy.storage.LazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.scopes.ChainedScope;
import org.jetbrains.jet.lang.resolve.scopes.InnerClassesScopeWrapper;
@@ -43,7 +43,7 @@ public class ScopeProvider {
private final Function<JetFile, JetScope> fileScopes;
private final LazyValue<JetScope> defaultImportsScope;
private final NotNullLazyValue<JetScope> defaultImportsScope;
public ScopeProvider(@NotNull ResolveSession resolveSession) {
this.resolveSession = resolveSession;
@@ -25,7 +25,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ClassKind;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.lazy.storage.LazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.name.FqName;
@@ -33,7 +33,7 @@ import java.util.List;
public class FilteringClassLikeInfo implements JetClassLikeInfo {
private final JetClassLikeInfo delegate;
private final LazyValue<List<JetDeclaration>> filteredDeclarations;
private final NotNullLazyValue<List<JetDeclaration>> filteredDeclarations;
public FilteringClassLikeInfo(
@NotNull StorageManager storageManager,
@@ -23,7 +23,7 @@ import com.google.common.collect.Multimap;
import com.intellij.openapi.util.Computable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.lazy.storage.LazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -68,7 +68,7 @@ public abstract class AbstractPsiBasedDeclarationProvider implements Declaration
}
private final LazyValue<Index> index;
private final NotNullLazyValue<Index> index;
public AbstractPsiBasedDeclarationProvider(@NotNull StorageManager storageManager) {
index = storageManager.createLazyValue(new Computable<Index>() {
@@ -28,7 +28,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamespaceHeader;
import org.jetbrains.jet.lang.resolve.lazy.storage.LazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo;
import org.jetbrains.jet.lang.resolve.name.FqName;
@@ -46,7 +46,7 @@ public class FileBasedDeclarationProviderFactory implements DeclarationProviderF
private final Predicate<FqName> isPackageDeclaredExternally;
private final StorageManager storageManager;
private final LazyValue<Index> index;
private final NotNullLazyValue<Index> index;
private final Function<FqName, PackageMemberDeclarationProvider> packageDeclarationProviders;
@@ -19,7 +19,7 @@ package org.jetbrains.jet.lang.resolve.lazy.declarations;
import com.intellij.openapi.util.Computable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.lazy.storage.LazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -31,7 +31,7 @@ public class FileBasedPackageMemberDeclarationProvider extends AbstractPsiBasedD
private final FqName fqName;
private final FileBasedDeclarationProviderFactory factory;
private final Collection<JetFile> allFiles;
private final LazyValue<Collection<FqName>> allDeclaredPackages;
private final NotNullLazyValue<Collection<FqName>> allDeclaredPackages;
/*package*/ FileBasedPackageMemberDeclarationProvider(
@@ -25,7 +25,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.lazy.storage.LazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassInfoUtil;
@@ -58,7 +58,7 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
private final Collection<ClassDescriptor> objects = Sets.newLinkedHashSet();
}
private final LazyValue<AllDescriptors> allDescriptors;
private final NotNullLazyValue<AllDescriptors> allDescriptors;
protected AbstractLazyMemberScope(
@NotNull ResolveSession resolveSession,
@@ -32,12 +32,16 @@ import org.jetbrains.jet.lang.resolve.AnnotationResolver;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.DescriptorResolver;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.lazy.*;
import org.jetbrains.jet.lang.resolve.lazy.ForceResolveUtil;
import org.jetbrains.jet.lang.resolve.lazy.LazyDescriptor;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
import org.jetbrains.jet.lang.resolve.lazy.ScopeProvider;
import org.jetbrains.jet.lang.resolve.lazy.data.FilteringClassLikeInfo;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassInfoUtil;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo;
import org.jetbrains.jet.lang.resolve.lazy.declarations.ClassMemberDeclarationProvider;
import org.jetbrains.jet.lang.resolve.lazy.storage.LazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.NullableLazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.*;
@@ -73,16 +77,16 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDesc
private final ClassKind kind;
private final boolean isInner;
private final LazyValue<ReceiverParameterDescriptor> thisAsReceiverParameter;
private final LazyValue<List<AnnotationDescriptor>> annotations;
private final LazyValue<ClassDescriptor> classObjectDescriptor;
private final NotNullLazyValue<ReceiverParameterDescriptor> thisAsReceiverParameter;
private final NotNullLazyValue<List<AnnotationDescriptor>> annotations;
private final NullableLazyValue<ClassDescriptor> classObjectDescriptor;
private final LazyClassMemberScope unsubstitutedMemberScope;
private final JetScope unsubstitutedInnerClassesScope;
private final LazyValue<JetScope> scopeForClassHeaderResolution;
private final LazyValue<JetScope> scopeForMemberDeclarationResolution;
private final LazyValue<JetScope> scopeForPropertyInitializerResolution;
private final NotNullLazyValue<JetScope> scopeForClassHeaderResolution;
private final NotNullLazyValue<JetScope> scopeForMemberDeclarationResolution;
private final NotNullLazyValue<JetScope> scopeForPropertyInitializerResolution;
public LazyClassDescriptor(
@@ -394,7 +398,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDesc
}
private class LazyClassTypeConstructor implements LazyDescriptor, TypeConstructor {
private final LazyValue<Collection<JetType>> supertypes = resolveSession.getStorageManager().createLazyValueWithPostCompute(
private final NotNullLazyValue<Collection<JetType>> supertypes = resolveSession.getStorageManager().createLazyValueWithPostCompute(
new Computable<Collection<JetType>>() {
@Override
public Collection<JetType> compute() {
@@ -424,7 +428,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDesc
}
});
private final LazyValue<List<TypeParameterDescriptor>> parameters = resolveSession.getStorageManager().createLazyValue(new Computable<List<TypeParameterDescriptor>>() {
private final NotNullLazyValue<List<TypeParameterDescriptor>> parameters = resolveSession.getStorageManager().createLazyValue(new Computable<List<TypeParameterDescriptor>>() {
@Override
public List<TypeParameterDescriptor> compute() {
JetClassLikeInfo classInfo = declarationProvider.getOwnerInfo();
@@ -25,10 +25,10 @@ import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.*;
import org.jetbrains.jet.lang.resolve.lazy.storage.LazyValue;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo;
import org.jetbrains.jet.lang.resolve.lazy.declarations.ClassMemberDeclarationProvider;
import org.jetbrains.jet.lang.resolve.lazy.storage.NullableLazyValue;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.DeferredType;
@@ -68,7 +68,7 @@ public class LazyClassMemberScope extends AbstractLazyMemberScope<LazyClassDescr
Collection<T> extract(@NotNull JetType extractFrom, @NotNull Name name);
}
private final LazyValue<ConstructorDescriptor> primaryConstructor;
private final NullableLazyValue<ConstructorDescriptor> primaryConstructor;
public LazyClassMemberScope(
@NotNull ResolveSession resolveSession,
@@ -27,7 +27,7 @@ import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.lazy.LazyDescriptor;
import org.jetbrains.jet.lang.resolve.lazy.storage.LazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -54,10 +54,10 @@ public class LazyTypeParameterDescriptor implements TypeParameterDescriptor, Laz
private final LazyClassDescriptor containingDeclaration;
private final Name name;
private final LazyValue<TypeConstructor> typeConstructor;
private final LazyValue<JetType> defaultType;
private final LazyValue<Set<JetType>> upperBounds;
private final LazyValue<JetType> upperBoundsAsType;
private final NotNullLazyValue<TypeConstructor> typeConstructor;
private final NotNullLazyValue<JetType> defaultType;
private final NotNullLazyValue<Set<JetType>> upperBounds;
private final NotNullLazyValue<JetType> upperBoundsAsType;
public LazyTypeParameterDescriptor(
@NotNull ResolveSession resolveSession,
@@ -73,11 +73,11 @@ public class LockBasedStorageManager implements StorageManager {
@NotNull final Function<K, V> compute, @NotNull final ReferenceKind valuesReferenceKind
) {
return new Function<K, V>() {
private final ConcurrentMap<K, LazyValue<V>> cache = createConcurrentMap(valuesReferenceKind);
private final ConcurrentMap<K, NullableLazyValue<V>> cache = createConcurrentMap(valuesReferenceKind);
@Override
public V fun(@NotNull final K input) {
LazyValue<V> lazyValue = cache.get(input);
NullableLazyValue<V> lazyValue = cache.get(input);
if (lazyValue != null) return lazyValue.compute();
lazyValue = createNullableLazyValue(new Computable<V>() {
@@ -87,7 +87,7 @@ public class LockBasedStorageManager implements StorageManager {
}
});
LazyValue<V> oldValue = cache.putIfAbsent(input, lazyValue);
NullableLazyValue<V> oldValue = cache.putIfAbsent(input, lazyValue);
if (oldValue != null) return oldValue.compute();
return lazyValue.compute();
@@ -101,14 +101,14 @@ public class LockBasedStorageManager implements StorageManager {
@NotNull
@Override
public <T> LazyValue<T> createLazyValue(@NotNull Computable<T> computable) {
return new LockBasedLazyValue<T>(lock, computable);
public <T> NotNullLazyValue<T> createLazyValue(@NotNull Computable<T> computable) {
return new LockBasedNotNullLazyValue<T>(lock, computable);
}
@NotNull
@Override
public <T> LazyValue<T> createLazyValueWithPostCompute(@NotNull Computable<T> computable, @NotNull final Consumer<T> postCompute) {
return new LockBasedLazyValue<T>(lock, computable) {
public <T> NotNullLazyValue<T> createLazyValueWithPostCompute(@NotNull Computable<T> computable, @NotNull final Consumer<T> postCompute) {
return new LockBasedNotNullLazyValue<T>(lock, computable) {
@Override
protected void postCompute(@NotNull T value) {
postCompute.consume(value);
@@ -118,13 +118,13 @@ public class LockBasedStorageManager implements StorageManager {
@NotNull
@Override
public <T> LazyValue<T> createNullableLazyValue(@NotNull Computable<T> computable) {
public <T> NullableLazyValue<T> createNullableLazyValue(@NotNull Computable<T> computable) {
return new LockBasedNullableLazyValue<T>(lock, computable);
}
@NotNull
@Override
public <T> LazyValue<T> createNullableLazyValueWithPostCompute(
public <T> NullableLazyValue<T> createNullableLazyValueWithPostCompute(
@NotNull Computable<T> computable,
@NotNull final Consumer<T> postCompute
) {
@@ -144,14 +144,14 @@ public class LockBasedStorageManager implements StorageManager {
return new LockProtectedTrace(lock, originalTrace);
}
private static class LockBasedLazyValue<T> implements LazyValue<T> {
private static class LockBasedNotNullLazyValue<T> implements NotNullLazyValue<T> {
private final Object lock;
private final Computable<T> computable;
@Nullable
private volatile T value;
public LockBasedLazyValue(@NotNull Object lock, @NotNull Computable<T> computable) {
public LockBasedNotNullLazyValue(@NotNull Object lock, @NotNull Computable<T> computable) {
this.lock = lock;
this.computable = computable;
}
@@ -180,7 +180,7 @@ public class LockBasedStorageManager implements StorageManager {
}
}
private static class LockBasedNullableLazyValue<T> implements LazyValue<T> {
private static class LockBasedNullableLazyValue<T> implements NullableLazyValue<T> {
private final Object lock;
private final Computable<T> computable;
@@ -17,8 +17,10 @@
package org.jetbrains.jet.lang.resolve.lazy.storage;
import com.intellij.openapi.util.Computable;
import org.jetbrains.annotations.NotNull;
public interface LazyValue<T> extends Computable<T> {
public interface NotNullLazyValue<T> extends Computable<T> {
@Override
@NotNull
T compute();
}
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.lazy.storage;
import com.intellij.openapi.util.Computable;
import org.jetbrains.annotations.Nullable;
public interface NullableLazyValue<T> extends Computable<T> {
@Override
@Nullable
T compute();
}
@@ -37,24 +37,24 @@ public interface StorageManager {
<K, V> Function<K, V> createMemoizedFunctionWithNullableValues(@NotNull Function<K, V> compute, @NotNull ReferenceKind valuesReferenceKind);
@NotNull
<T> LazyValue<T> createLazyValue(@NotNull Computable<T> computable);
<T> NotNullLazyValue<T> createLazyValue(@NotNull Computable<T> computable);
/**
* {@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> LazyValue<T> createLazyValueWithPostCompute(@NotNull Computable<T> computable, @NotNull Consumer<T> postCompute);
<T> NotNullLazyValue<T> createLazyValueWithPostCompute(@NotNull Computable<T> computable, @NotNull Consumer<T> postCompute);
@NotNull
<T> LazyValue<T> createNullableLazyValue(@NotNull Computable<T> computable);
<T> NullableLazyValue<T> createNullableLazyValue(@NotNull Computable<T> computable);
/**
* {@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> LazyValue<T> createNullableLazyValueWithPostCompute(@NotNull Computable<T> computable, @NotNull Consumer<T> postCompute);
<T> NullableLazyValue<T> createNullableLazyValueWithPostCompute(@NotNull Computable<T> computable, @NotNull Consumer<T> postCompute);
@NotNull
BindingTrace createSafeTrace(@NotNull BindingTrace originalTrace);