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.resolve.java.structure.impl;
018    
019    import com.intellij.psi.*;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.jet.lang.resolve.java.structure.JavaType;
023    
024    public abstract class JavaTypeImpl<Psi extends PsiType> implements JavaType {
025        private final Psi psiType;
026    
027        public JavaTypeImpl(@NotNull Psi psiType) {
028            this.psiType = psiType;
029        }
030    
031        @NotNull
032        public Psi getPsi() {
033            return psiType;
034        }
035    
036        @NotNull
037        public static JavaTypeImpl<?> create(@NotNull PsiType psiType) {
038            return psiType.accept(new PsiTypeVisitor<JavaTypeImpl<?>>() {
039                @Nullable
040                @Override
041                public JavaTypeImpl<?> visitType(PsiType type) {
042                    throw new UnsupportedOperationException("Unsupported PsiType: " + type);
043                }
044    
045                @Nullable
046                @Override
047                public JavaTypeImpl<?> visitPrimitiveType(PsiPrimitiveType primitiveType) {
048                    return new JavaPrimitiveTypeImpl(primitiveType);
049                }
050    
051                @Nullable
052                @Override
053                public JavaTypeImpl<?> visitArrayType(PsiArrayType arrayType) {
054                    return new JavaArrayTypeImpl(arrayType);
055                }
056    
057                @Nullable
058                @Override
059                public JavaTypeImpl<?> visitClassType(PsiClassType classType) {
060                    return new JavaClassifierTypeImpl(classType);
061                }
062    
063                @Nullable
064                @Override
065                public JavaTypeImpl<?> visitWildcardType(PsiWildcardType wildcardType) {
066                    return new JavaWildcardTypeImpl(wildcardType);
067                }
068            });
069        }
070    
071        @Override
072        public int hashCode() {
073            return getPsi().hashCode();
074        }
075    
076        @Override
077        public boolean equals(Object obj) {
078            return obj instanceof JavaTypeImpl && getPsi().equals(((JavaTypeImpl) obj).getPsi());
079        }
080    
081        @Override
082        public String toString() {
083            return getClass().getSimpleName() + ": " + getPsi();
084        }
085    }