001    /*
002     * Copyright 2010-2013 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.annotations.AnnotatedImpl;
022    import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
023    import org.jetbrains.jet.lang.resolve.scopes.JetScope;
024    import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
025    
026    import java.util.Collections;
027    import java.util.List;
028    
029    public final class JetTypeImpl extends AnnotatedImpl implements JetType {
030    
031        private final TypeConstructor constructor;
032        private final List<TypeProjection> arguments;
033        private final boolean nullable;
034        private final JetScope memberScope;
035    
036        public JetTypeImpl(List<AnnotationDescriptor> annotations, TypeConstructor constructor, boolean nullable, @NotNull List<TypeProjection> arguments, JetScope memberScope) {
037            super(annotations);
038    
039            if (memberScope instanceof ErrorUtils.ErrorScope) {
040                throw new IllegalStateException();
041            }
042    
043            this.constructor = constructor;
044            this.nullable = nullable;
045            this.arguments = arguments;
046            this.memberScope = memberScope;
047        }
048    
049        public JetTypeImpl(TypeConstructor constructor, JetScope memberScope) {
050            this(Collections.<AnnotationDescriptor>emptyList(), constructor, false, Collections.<TypeProjection>emptyList(), memberScope);
051        }
052    
053        public JetTypeImpl(@NotNull ClassDescriptor classDescriptor) {
054            this(Collections.<AnnotationDescriptor>emptyList(),
055                    classDescriptor.getTypeConstructor(),
056                    false,
057                    Collections.<TypeProjection>emptyList(),
058                    classDescriptor.getMemberScope(Collections.<TypeProjection>emptyList()));
059        }
060    
061        @NotNull
062        @Override
063        public TypeConstructor getConstructor() {
064            return constructor;
065        }
066    
067        @NotNull
068        @Override
069        public List<TypeProjection> getArguments() {
070            return arguments;
071        }
072    
073        @Override
074        public boolean isNullable() {
075            return nullable;
076        }
077    
078        @NotNull
079        @Override
080        public JetScope getMemberScope() {
081            return memberScope;
082        }
083    
084        @Override
085        public String toString() {
086            return TypeUtils.toString(this);
087        }
088    
089        @Override
090        public boolean equals(Object o) {
091            if (this == o) return true;
092            if (!(o instanceof JetType)) return false;
093    
094            JetType type = (JetType) o;
095    
096            return nullable == type.isNullable() && JetTypeChecker.INSTANCE.equalTypes(this, type);
097        }
098    
099        @Override
100        public int hashCode() {
101            int result = constructor != null ? constructor.hashCode() : 0;
102            result = 31 * result + arguments.hashCode();
103            result = 31 * result + (nullable ? 1 : 0);
104            return result;
105        }
106    }