Home | History | Annotate | Download | only in typeinference
      1 package com.github.javaparser.symbolsolver.resolution.typeinference;
      2 
      3 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
      4 
      5 import java.util.LinkedList;
      6 import java.util.List;
      7 
      8 /**
      9  * @author Federico Tomassetti
     10  */
     11 public class ConstraintFormulaSet {
     12     private List<ConstraintFormula> constraintFormulas;
     13 
     14     public ConstraintFormulaSet withConstraint(ConstraintFormula constraintFormula) {
     15         ConstraintFormulaSet newInstance = new ConstraintFormulaSet();
     16         newInstance.constraintFormulas.addAll(this.constraintFormulas);
     17         newInstance.constraintFormulas.add(constraintFormula);
     18         return newInstance;
     19     }
     20 
     21     private static final ConstraintFormulaSet EMPTY = new ConstraintFormulaSet();
     22 
     23     public static ConstraintFormulaSet empty() {
     24         return EMPTY;
     25     }
     26 
     27     private ConstraintFormulaSet() {
     28         constraintFormulas = new LinkedList<>();
     29     }
     30 
     31     /**
     32      * Takes a compatibility assertion about an expression or type, called a constraint formula, and reduces it to a
     33      * set of bounds on inference variables. Often, a constraint formula reduces to other constraint formulas,
     34      * which must be recursively reduced. A procedure is followed to identify these additional constraint formulas and,
     35      * ultimately, to express via a bound set the conditions under which the choices for inferred types would render
     36      * each constraint formula true.
     37      */
     38     public BoundSet reduce(TypeSolver typeSolver) {
     39         List<ConstraintFormula> constraints = new LinkedList<>(constraintFormulas);
     40         BoundSet boundSet = BoundSet.empty();
     41         while (constraints.size() > 0) {
     42             ConstraintFormula constraintFormula = constraints.remove(0);
     43             ConstraintFormula.ReductionResult reductionResult = constraintFormula.reduce(boundSet);
     44             constraints.addAll(reductionResult.getConstraintFormulas());
     45             boundSet.incorporate(reductionResult.getBoundSet(), typeSolver);
     46         }
     47         return boundSet;
     48     }
     49 
     50     public boolean isEmpty() {
     51         return constraintFormulas.isEmpty();
     52     }
     53 }
     54