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.PsiSubstitutor;
020    import com.intellij.psi.PsiType;
021    import com.intellij.psi.PsiTypeParameter;
022    import com.intellij.psi.impl.PsiSubstitutorImpl;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.annotations.Nullable;
025    import org.jetbrains.jet.lang.resolve.java.structure.JavaType;
026    import org.jetbrains.jet.lang.resolve.java.structure.JavaTypeParameter;
027    import org.jetbrains.jet.lang.resolve.java.structure.JavaTypeSubstitutor;
028    
029    import java.util.HashMap;
030    import java.util.Map;
031    
032    public class JavaTypeSubstitutorImpl implements JavaTypeSubstitutor {
033        private final PsiSubstitutor psiSubstitutor;
034        private Map<JavaTypeParameter, JavaType> substitutionMap;
035    
036        public JavaTypeSubstitutorImpl(@NotNull PsiSubstitutor psiSubstitutor) {
037            this.psiSubstitutor = psiSubstitutor;
038        }
039    
040        public JavaTypeSubstitutorImpl(@NotNull PsiSubstitutor psiSubstitutor, @NotNull Map<JavaTypeParameter, JavaType> substitutionMap) {
041            this(psiSubstitutor);
042            this.substitutionMap = substitutionMap;
043        }
044    
045        @NotNull
046        public static JavaTypeSubstitutor create(@NotNull Map<JavaTypeParameter, JavaType> substitutionMap) {
047            Map<PsiTypeParameter, PsiType> psiMap = new HashMap<PsiTypeParameter, PsiType>();
048            for (Map.Entry<JavaTypeParameter, JavaType> entry : substitutionMap.entrySet()) {
049                JavaTypeImpl value = ((JavaTypeImpl) entry.getValue());
050                psiMap.put(((JavaTypeParameterImpl) entry.getKey()).getPsi(), value == null ? null : value.getPsi());
051            }
052            PsiSubstitutor psiSubstitutor = PsiSubstitutorImpl.createSubstitutor(psiMap);
053            return new JavaTypeSubstitutorImpl(psiSubstitutor, substitutionMap);
054        }
055    
056        @Override
057        @NotNull
058        public JavaType substitute(@NotNull JavaType type) {
059            return JavaTypeImpl.create(psiSubstitutor.substitute(((JavaTypeImpl) type).getPsi()));
060        }
061    
062        @Override
063        @Nullable
064        public JavaType substitute(@NotNull JavaTypeParameter typeParameter) {
065            PsiType psiType = psiSubstitutor.substitute(((JavaTypeParameterImpl) typeParameter).getPsi());
066            return psiType == null ? null : JavaTypeImpl.create(psiType);
067        }
068    
069        @Override
070        @NotNull
071        public Map<JavaTypeParameter, JavaType> getSubstitutionMap() {
072            if (substitutionMap == null) {
073                Map<PsiTypeParameter, PsiType> psiMap = psiSubstitutor.getSubstitutionMap();
074                substitutionMap = new HashMap<JavaTypeParameter, JavaType>();
075                for (Map.Entry<PsiTypeParameter, PsiType> entry : psiMap.entrySet()) {
076                    PsiType value = entry.getValue();
077                    substitutionMap.put(new JavaTypeParameterImpl(entry.getKey()), value == null ? null : JavaTypeImpl.create(value));
078                }
079            }
080    
081            return substitutionMap;
082        }
083    
084        @Override
085        public int hashCode() {
086            return psiSubstitutor.hashCode();
087        }
088    
089        @Override
090        public boolean equals(Object obj) {
091            return obj instanceof JavaTypeSubstitutorImpl && psiSubstitutor.equals(((JavaTypeSubstitutorImpl) obj).psiSubstitutor);
092        }
093    
094        @Override
095        public String toString() {
096            return getClass().getSimpleName() + ": " + psiSubstitutor;
097        }
098    }