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.kotlin.descriptors.ClassDescriptor;
021    import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
022    import org.jetbrains.kotlin.name.FqNameUnsafe;
023    import org.jetbrains.kotlin.resolve.DescriptorUtils;
024    
025    public abstract class AbstractClassTypeConstructor implements TypeConstructor {
026        private boolean hashCodeComputed;
027        private int hashCode;
028    
029        @Override
030        public final int hashCode() {
031            if (!hashCodeComputed) {
032                hashCodeComputed = true;
033                hashCode = hashCode(this);
034            }
035            return hashCode;
036        }
037    
038        @Override
039        @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
040        public boolean equals(Object obj) {
041            return equals(this, obj);
042        }
043    
044        public static boolean equals(@NotNull TypeConstructor me, Object other) {
045            if (!(other instanceof TypeConstructor)) return false;
046    
047            // performance optimization: getFqName is slow method
048            if (other.hashCode() != me.hashCode()) return false;
049    
050            ClassifierDescriptor myDescriptor = me.getDeclarationDescriptor();
051            ClassifierDescriptor otherDescriptor = ((TypeConstructor) other).getDeclarationDescriptor();
052    
053            // descriptor for type is created once per module
054            if (myDescriptor == otherDescriptor) return true;
055    
056            // All error types have the same descriptor
057            if (myDescriptor != null && !hasMeaningfulFqName(myDescriptor) ||
058                otherDescriptor != null && !hasMeaningfulFqName(otherDescriptor)) {
059                return me == other;
060            }
061    
062            if (myDescriptor instanceof ClassDescriptor && otherDescriptor instanceof ClassDescriptor) {
063                FqNameUnsafe otherFqName = DescriptorUtils.getFqName(otherDescriptor);
064                FqNameUnsafe myFqName = DescriptorUtils.getFqName(myDescriptor);
065                return myFqName.equals(otherFqName);
066            }
067    
068            return false;
069        }
070    
071        public static int hashCode(@NotNull TypeConstructor me) {
072            ClassifierDescriptor descriptor = me.getDeclarationDescriptor();
073            if (descriptor instanceof ClassDescriptor && hasMeaningfulFqName(descriptor)) {
074                return DescriptorUtils.getFqName(descriptor).hashCode();
075            }
076            return System.identityHashCode(me);
077        }
078    
079        private static boolean hasMeaningfulFqName(@NotNull ClassifierDescriptor descriptor) {
080            return !ErrorUtils.isError(descriptor) &&
081                   !DescriptorUtils.isLocal(descriptor);
082        }
083    }