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