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.load.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.kotlin.load.java.structure.JavaAnnotation;
023    import org.jetbrains.kotlin.load.java.structure.JavaArrayType;
024    import org.jetbrains.kotlin.load.java.structure.JavaType;
025    import org.jetbrains.kotlin.name.FqName;
026    
027    import java.util.Collection;
028    
029    public abstract class JavaTypeImpl<Psi extends PsiType> implements JavaType, JavaAnnotationOwnerImpl {
030        private final Psi psiType;
031    
032        public JavaTypeImpl(@NotNull Psi psiType) {
033            this.psiType = psiType;
034        }
035    
036        @NotNull
037        public Psi getPsi() {
038            return psiType;
039        }
040    
041        @Nullable
042        @Override
043        public PsiAnnotationOwner getAnnotationOwnerPsi() {
044            return getPsi();
045        }
046    
047        @NotNull
048        public static JavaTypeImpl<?> create(@NotNull PsiType psiType) {
049            return psiType.accept(new PsiTypeVisitor<JavaTypeImpl<?>>() {
050                @Nullable
051                @Override
052                public JavaTypeImpl<?> visitType(@NotNull PsiType type) {
053                    throw new UnsupportedOperationException("Unsupported PsiType: " + type);
054                }
055    
056                @Nullable
057                @Override
058                public JavaTypeImpl<?> visitPrimitiveType(@NotNull PsiPrimitiveType primitiveType) {
059                    return new JavaPrimitiveTypeImpl(primitiveType);
060                }
061    
062                @Nullable
063                @Override
064                public JavaTypeImpl<?> visitArrayType(@NotNull PsiArrayType arrayType) {
065                    return new JavaArrayTypeImpl(arrayType);
066                }
067    
068                @Nullable
069                @Override
070                public JavaTypeImpl<?> visitClassType(@NotNull PsiClassType classType) {
071                    return new JavaClassifierTypeImpl(classType);
072                }
073    
074                @Nullable
075                @Override
076                public JavaTypeImpl<?> visitWildcardType(@NotNull PsiWildcardType wildcardType) {
077                    return new JavaWildcardTypeImpl(wildcardType);
078                }
079            });
080        }
081    
082        @NotNull
083        @Override
084        public JavaArrayType createArrayType() {
085            return new JavaArrayTypeImpl(getPsi().createArrayType());
086        }
087    
088        @NotNull
089        @Override
090        public Collection<JavaAnnotation> getAnnotations() {
091            return JavaElementUtil.getAnnotations(this);
092        }
093    
094        @Nullable
095        @Override
096        public JavaAnnotation findAnnotation(@NotNull FqName fqName) {
097            return JavaElementUtil.findAnnotation(this, fqName);
098        }
099    
100        @Override
101        public boolean isDeprecatedInJavaDoc() {
102            return false;
103        }
104    
105        @Override
106        public int hashCode() {
107            return getPsi().hashCode();
108        }
109    
110        @Override
111        public boolean equals(Object obj) {
112            return obj instanceof JavaTypeImpl && getPsi().equals(((JavaTypeImpl) obj).getPsi());
113        }
114    
115        @Override
116        public String toString() {
117            return getClass().getSimpleName() + ": " + getPsi();
118        }
119    }