Home | History | Annotate | Download | only in typesolvers
      1 /*
      2  * Copyright 2016 Federico Tomassetti
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.github.javaparser.symbolsolver.resolution.typesolvers;
     18 
     19 import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
     20 import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
     21 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
     22 
     23 import java.util.HashMap;
     24 import java.util.Map;
     25 
     26 /**
     27  * A TypeSolver which only consider the TypeDeclarations provided to it.
     28  *
     29  * @author Federico Tomassetti
     30  */
     31 public class MemoryTypeSolver implements TypeSolver {
     32 
     33     private TypeSolver parent;
     34     private Map<String, ResolvedReferenceTypeDeclaration> declarationMap = new HashMap<>();
     35 
     36     @Override
     37     public String toString() {
     38         return "MemoryTypeSolver{" +
     39                 "parent=" + parent +
     40                 ", declarationMap=" + declarationMap +
     41                 '}';
     42     }
     43 
     44     @Override
     45     public boolean equals(Object o) {
     46         if (this == o) return true;
     47         if (!(o instanceof MemoryTypeSolver)) return false;
     48 
     49         MemoryTypeSolver that = (MemoryTypeSolver) o;
     50 
     51         if (parent != null ? !parent.equals(that.parent) : that.parent != null) return false;
     52         return !(declarationMap != null ? !declarationMap.equals(that.declarationMap) : that.declarationMap != null);
     53 
     54     }
     55 
     56     @Override
     57     public int hashCode() {
     58         int result = parent != null ? parent.hashCode() : 0;
     59         result = 31 * result + (declarationMap != null ? declarationMap.hashCode() : 0);
     60         return result;
     61     }
     62 
     63     @Override
     64     public TypeSolver getParent() {
     65         return parent;
     66     }
     67 
     68     @Override
     69     public void setParent(TypeSolver parent) {
     70         this.parent = parent;
     71     }
     72 
     73     public void addDeclaration(String name, ResolvedReferenceTypeDeclaration typeDeclaration) {
     74         this.declarationMap.put(name, typeDeclaration);
     75     }
     76 
     77     @Override
     78     public SymbolReference<ResolvedReferenceTypeDeclaration> tryToSolveType(String name) {
     79         if (declarationMap.containsKey(name)) {
     80             return SymbolReference.solved(declarationMap.get(name));
     81         } else {
     82             return SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class);
     83         }
     84     }
     85 
     86 }
     87