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