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