001    /*
002     * Copyright 2010-2014 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.jet.lang.types;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
021    import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
022    import org.jetbrains.jet.lang.resolve.DescriptorUtils;
023    import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
024    
025    public abstract class AbstractClassTypeConstructor implements TypeConstructor {
026        @Override
027        public final int hashCode() {
028            return hashCode(this);
029        }
030    
031        @Override
032        @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
033        public boolean equals(Object obj) {
034            return equals(this, obj);
035        }
036    
037        public static boolean equals(@NotNull TypeConstructor me, Object other) {
038            if (!(other instanceof TypeConstructor)) return false;
039    
040            ClassifierDescriptor myDescriptor = me.getDeclarationDescriptor();
041            ClassifierDescriptor otherDescriptor = ((TypeConstructor) other).getDeclarationDescriptor();
042    
043            // All error types have the same descriptor
044            if (myDescriptor != null && ErrorUtils.isError(myDescriptor)
045                || otherDescriptor != null && ErrorUtils.isError(otherDescriptor)) {
046                return me == other;
047            }
048    
049            if (myDescriptor == otherDescriptor) return true;
050    
051            if (myDescriptor instanceof ClassDescriptor && otherDescriptor instanceof ClassDescriptor) {
052                FqNameUnsafe otherFqName = DescriptorUtils.getFqName(otherDescriptor);
053                FqNameUnsafe myFqName = DescriptorUtils.getFqName(myDescriptor);
054                return myFqName.equals(otherFqName);
055            }
056    
057            return false;
058        }
059    
060        public static int hashCode(@NotNull TypeConstructor me) {
061            ClassifierDescriptor descriptor = me.getDeclarationDescriptor();
062            if (descriptor instanceof ClassDescriptor && !ErrorUtils.isError(descriptor)) {
063                ClassDescriptor classDescriptor = (ClassDescriptor) descriptor;
064                return DescriptorUtils.getFqName(classDescriptor).hashCode();
065            }
066            return System.identityHashCode(me);
067        }
068    }