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.descriptors.TypeParameterDescriptor;
025    import org.jetbrains.kotlin.descriptors.annotations.AnnotatedImpl;
026    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
027    import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
028    
029    import java.util.ArrayList;
030    import java.util.Collection;
031    import java.util.Collections;
032    import java.util.List;
033    
034    public abstract class TypeConstructorImpl extends AnnotatedImpl implements TypeConstructor {
035    
036        @NotNull
037        public static TypeConstructorImpl createForClass(
038                @NotNull ClassDescriptor classDescriptor,
039                @NotNull Annotations annotations,
040                boolean isFinal,
041                @NotNull String debugName,
042                @NotNull List<? extends TypeParameterDescriptor> parameters,
043                @NotNull Collection<KotlinType> supertypes
044        ) {
045            return new TypeConstructorImpl(classDescriptor, annotations, isFinal, debugName, parameters, supertypes) {
046                @Override
047                public int hashCode() {
048                    return AbstractClassTypeConstructor.hashCode(this);
049                }
050    
051                @Override
052                @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
053                public boolean equals(Object obj) {
054                    return AbstractClassTypeConstructor.equals(this, obj);
055                }
056            };
057        }
058    
059        @NotNull
060        public static TypeConstructorImpl createForTypeParameter(
061                @NotNull TypeParameterDescriptor typeParameterDescriptor,
062                @NotNull Annotations annotations,
063                boolean isFinal,
064                @NotNull String debugName,
065                @NotNull List<? extends TypeParameterDescriptor> parameters,
066                @NotNull Collection<KotlinType> supertypes
067        ) {
068            return new TypeConstructorImpl(typeParameterDescriptor, annotations, isFinal, debugName, parameters, supertypes) {
069                @Override
070                public int hashCode() {
071                    return System.identityHashCode(this);
072                }
073    
074                @Override
075                public boolean equals(Object obj) {
076                    return this == obj;
077                }
078            };
079        }
080    
081        private final List<TypeParameterDescriptor> parameters;
082        private final Collection<KotlinType> supertypes;
083        private final String debugName;
084        private final boolean isFinal;
085    
086        private final ClassifierDescriptor classifierDescriptor;
087    
088        private TypeConstructorImpl(
089                @NotNull ClassifierDescriptor classifierDescriptor,
090                @NotNull Annotations annotations,
091                boolean isFinal,
092                @NotNull String debugName,
093                @NotNull List<? extends TypeParameterDescriptor> parameters,
094                @NotNull Collection<KotlinType> supertypes) {
095            super(annotations);
096            this.classifierDescriptor = classifierDescriptor;
097            this.isFinal = isFinal;
098            this.debugName = debugName;
099            this.parameters = Collections.unmodifiableList(new ArrayList<TypeParameterDescriptor>(parameters));
100            this.supertypes = Collections.unmodifiableCollection(supertypes);
101        }
102    
103        @Override
104        @NotNull
105        public List<TypeParameterDescriptor> getParameters() {
106            return parameters;
107        }
108    
109        @Override
110        @NotNull
111        public Collection<KotlinType> getSupertypes() {
112            return supertypes;
113        }
114    
115        @Override
116        public String toString() {
117            return debugName;
118        }
119    
120        @Override
121        public boolean isFinal() {
122            return isFinal;
123        }
124    
125        @Override
126        public boolean isDenotable() {
127            return true;
128        }
129    
130        @Override
131        @Nullable
132        public ClassifierDescriptor getDeclarationDescriptor() {
133            return classifierDescriptor;
134        }
135    
136        @NotNull
137        @Override
138        public KotlinBuiltIns getBuiltIns() {
139            return DescriptorUtilsKt.getBuiltIns(classifierDescriptor);
140        }
141    
142        @Override
143        public abstract int hashCode();
144    
145        @Override
146        public abstract boolean equals(Object obj);
147    }