Common logic extracted from lazy type implementations

This commit is contained in:
Andrey Breslav
2013-09-27 18:18:08 -07:00
parent 8bfa6f3aeb
commit 51d54661a1
4 changed files with 70 additions and 88 deletions
@@ -28,7 +28,6 @@ import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
import org.jetbrains.jet.storage.MemoizedFunctionToNullable;
import org.jetbrains.jet.storage.NotNullLazyValue;
import org.jetbrains.jet.storage.StorageManager;
@@ -198,7 +197,7 @@ public class TypeDeserializer {
return debugName;
}
private class DeserializedType implements JetType {
private class DeserializedType extends AbstractJetType {
private final ProtoBuf.Type typeProto;
private final NotNullLazyValue<TypeConstructor> constructor;
private final List<TypeProjection> arguments;
@@ -265,28 +264,5 @@ public class TypeDeserializer {
public List<AnnotationDescriptor> getAnnotations() {
return Collections.emptyList();
}
@Override
public String toString() {
return TypeUtils.toString(this);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof JetType)) return false;
JetType type = (JetType) o;
return isNullable() == type.isNullable() && JetTypeChecker.INSTANCE.equalTypes(this, type);
}
@Override
public int hashCode() {
int result = getConstructor().hashCode();
result = 31 * result + getArguments().hashCode();
result = 31 * result + (isNullable() ? 1 : 0);
return result;
}
}
}
@@ -0,0 +1,60 @@
/*
* 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.types;
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
import java.util.Iterator;
import java.util.List;
public abstract class AbstractJetType implements JetType {
@Override
public final int hashCode() {
int result = getConstructor().hashCode();
result = 31 * result + getArguments().hashCode();
result = 31 * result + (isNullable() ? 1 : 0);
return result;
}
@Override
public final boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof JetType)) return false;
JetType type = (JetType) obj;
return isNullable() == type.isNullable() && JetTypeChecker.INSTANCE.equalTypes(this, type);
}
@Override
public final String toString() {
List<TypeProjection> arguments = getArguments();
return getConstructor() + (arguments.isEmpty() ? "" : "<" + argumentsToString(arguments) + ">") + (isNullable() ? "?" : "");
}
private static StringBuilder argumentsToString(List<TypeProjection> arguments) {
StringBuilder stringBuilder = new StringBuilder();
for (Iterator<TypeProjection> iterator = arguments.iterator(); iterator.hasNext();) {
TypeProjection argument = iterator.next();
stringBuilder.append(argument);
if (iterator.hasNext()) {
stringBuilder.append(", ");
}
}
return stringBuilder;
}
}
@@ -18,23 +18,22 @@ package org.jetbrains.jet.lang.types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotatedImpl;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
import java.util.Collections;
import java.util.List;
public final class JetTypeImpl extends AnnotatedImpl implements JetType {
public final class JetTypeImpl extends AbstractJetType {
private final TypeConstructor constructor;
private final List<? extends TypeProjection> arguments;
private final boolean nullable;
private final JetScope memberScope;
private final List<AnnotationDescriptor> annotations;
public JetTypeImpl(List<AnnotationDescriptor> annotations, TypeConstructor constructor, boolean nullable, @NotNull List<? extends TypeProjection> arguments, JetScope memberScope) {
super(annotations);
this.annotations = annotations;
if (memberScope instanceof ErrorUtils.ErrorScope) {
throw new IllegalStateException("JetTypeImpl should not be created for error type: " + memberScope + "\n" + constructor);
@@ -58,6 +57,11 @@ public final class JetTypeImpl extends AnnotatedImpl implements JetType {
classDescriptor.getMemberScope(Collections.<TypeProjection>emptyList()));
}
@Override
public List<AnnotationDescriptor> getAnnotations() {
return annotations;
}
@NotNull
@Override
public TypeConstructor getConstructor() {
@@ -86,27 +90,4 @@ public final class JetTypeImpl extends AnnotatedImpl implements JetType {
public boolean isError() {
return false;
}
@Override
public String toString() {
return TypeUtils.toString(this);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof JetType)) return false;
JetType type = (JetType) o;
return nullable == type.isNullable() && JetTypeChecker.INSTANCE.equalTypes(this, type);
}
@Override
public int hashCode() {
int result = constructor.hashCode();
result = 31 * result + arguments.hashCode();
result = 31 * result + (nullable ? 1 : 0);
return result;
}
}
@@ -26,7 +26,6 @@ import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.ReadOnly;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
@@ -221,24 +220,6 @@ public class TypeUtils {
new ChainedScope(null, scopes)); // TODO : check intersectibility, don't use a chanied scope
}
@NotNull
public static String toString(@NotNull JetType type) {
List<TypeProjection> arguments = type.getArguments();
return type.getConstructor() + (arguments.isEmpty() ? "" : "<" + argumentsToString(arguments) + ">") + (type.isNullable() ? "?" : "");
}
private static StringBuilder argumentsToString(List<TypeProjection> arguments) {
StringBuilder stringBuilder = new StringBuilder();
for (Iterator<TypeProjection> iterator = arguments.iterator(); iterator.hasNext();) {
TypeProjection argument = iterator.next();
stringBuilder.append(argument);
if (iterator.hasNext()) {
stringBuilder.append(", ");
}
}
return stringBuilder;
}
private static class TypeUnifier {
private static class TypeParameterUsage {
private final TypeParameterDescriptor typeParameterDescriptor;
@@ -742,7 +723,7 @@ public class TypeUtils {
});
}
private static abstract class AbstractTypeWithKnownNullability implements JetType {
private static abstract class AbstractTypeWithKnownNullability extends AbstractJetType {
private final JetType delegate;
private AbstractTypeWithKnownNullability(@NotNull JetType delegate) {
@@ -757,7 +738,6 @@ public class TypeUtils {
@Override
@NotNull
@ReadOnly
public List<TypeProjection> getArguments() {
return delegate.getArguments();
}
@@ -781,21 +761,6 @@ public class TypeUtils {
public List<AnnotationDescriptor> getAnnotations() {
return delegate.getAnnotations();
}
@Override
public boolean equals(Object other) {
return TypeUtils.equals(this, other);
}
@Override
public int hashCode() {
return TypeUtils.hashCode(this);
}
@Override
public String toString() {
return TypeUtils.toString(this);
}
}
private static class NullableType extends AbstractTypeWithKnownNullability {