001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.types;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
022 import org.jetbrains.kotlin.descriptors.ClassDescriptor;
023 import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
024 import org.jetbrains.kotlin.name.FqNameUnsafe;
025 import org.jetbrains.kotlin.resolve.DescriptorUtils;
026 import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
027
028 public abstract class AbstractClassTypeConstructor implements TypeConstructor {
029 private boolean hashCodeComputed;
030 private int hashCode;
031
032 @Override
033 public final int hashCode() {
034 if (!hashCodeComputed) {
035 hashCodeComputed = true;
036 hashCode = hashCode(this);
037 }
038 return hashCode;
039 }
040
041 @NotNull
042 @Override
043 public abstract ClassifierDescriptor getDeclarationDescriptor();
044
045 @NotNull
046 @Override
047 public KotlinBuiltIns getBuiltIns() {
048 return DescriptorUtilsKt.getBuiltIns(getDeclarationDescriptor());
049 }
050
051 @Override
052 @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
053 public boolean equals(Object obj) {
054 return equals(this, obj);
055 }
056
057 public static boolean equals(@NotNull TypeConstructor me, Object other) {
058 if (!(other instanceof TypeConstructor)) return false;
059
060 // performance optimization: getFqName is slow method
061 if (other.hashCode() != me.hashCode()) return false;
062
063 ClassifierDescriptor myDescriptor = me.getDeclarationDescriptor();
064 ClassifierDescriptor otherDescriptor = ((TypeConstructor) other).getDeclarationDescriptor();
065
066 // descriptor for type is created once per module
067 if (myDescriptor == otherDescriptor) return true;
068
069 // All error types have the same descriptor
070 if (myDescriptor != null && !hasMeaningfulFqName(myDescriptor) ||
071 otherDescriptor != null && !hasMeaningfulFqName(otherDescriptor)) {
072 return me == other;
073 }
074
075 if (myDescriptor instanceof ClassDescriptor && otherDescriptor instanceof ClassDescriptor) {
076 FqNameUnsafe otherFqName = DescriptorUtils.getFqName(otherDescriptor);
077 FqNameUnsafe myFqName = DescriptorUtils.getFqName(myDescriptor);
078 return myFqName.equals(otherFqName);
079 }
080
081 return false;
082 }
083
084 public static int hashCode(@NotNull TypeConstructor me) {
085 ClassifierDescriptor descriptor = me.getDeclarationDescriptor();
086 if (descriptor instanceof ClassDescriptor && hasMeaningfulFqName(descriptor)) {
087 return DescriptorUtils.getFqName(descriptor).hashCode();
088 }
089 return System.identityHashCode(me);
090 }
091
092 private static boolean hasMeaningfulFqName(@NotNull ClassifierDescriptor descriptor) {
093 return !ErrorUtils.isError(descriptor) &&
094 !DescriptorUtils.isLocal(descriptor);
095 }
096 }