1 package com.github.javaparser.symbolsolver.resolution.typeinference; 2 3 import java.util.Arrays; 4 import java.util.LinkedList; 5 import java.util.List; 6 7 /** 8 * Constraint formulas are assertions of compatibility or subtyping that may involve inference variables. 9 * 10 * @author Federico Tomassetti 11 */ 12 public abstract class ConstraintFormula { 13 14 public static class ReductionResult { 15 private BoundSet boundSet; 16 private List<ConstraintFormula> constraintFormulas; 17 getBoundSet()18 public BoundSet getBoundSet() { 19 return boundSet; 20 } 21 getConstraintFormulas()22 public List<ConstraintFormula> getConstraintFormulas() { 23 return constraintFormulas; 24 } 25 empty()26 public static ReductionResult empty() { 27 return new ReductionResult(); 28 } 29 withConstraint(ConstraintFormula constraintFormula)30 public ReductionResult withConstraint(ConstraintFormula constraintFormula) { 31 ReductionResult newInstance = new ReductionResult(); 32 newInstance.boundSet = this.boundSet; 33 newInstance.constraintFormulas = new LinkedList<>(); 34 newInstance.constraintFormulas.addAll(this.constraintFormulas); 35 newInstance.constraintFormulas.add(constraintFormula); 36 return newInstance; 37 } 38 withBound(Bound bound)39 public ReductionResult withBound(Bound bound) { 40 ReductionResult newInstance = new ReductionResult(); 41 newInstance.boundSet = this.boundSet.withBound(bound); 42 newInstance.constraintFormulas = this.constraintFormulas; 43 return newInstance; 44 } 45 ReductionResult()46 private ReductionResult() { 47 this.boundSet = BoundSet.empty(); 48 this.constraintFormulas = new LinkedList<>(); 49 } 50 trueResult()51 public static ReductionResult trueResult() { 52 return empty(); 53 } 54 falseResult()55 public static ReductionResult falseResult() { 56 return empty().withBound(Bound.falseBound()); 57 } 58 59 @Override equals(Object o)60 public boolean equals(Object o) { 61 if (this == o) return true; 62 if (o == null || getClass() != o.getClass()) return false; 63 64 ReductionResult that = (ReductionResult) o; 65 66 if (!boundSet.equals(that.boundSet)) return false; 67 return constraintFormulas.equals(that.constraintFormulas); 68 } 69 70 @Override hashCode()71 public int hashCode() { 72 int result = boundSet.hashCode(); 73 result = 31 * result + constraintFormulas.hashCode(); 74 return result; 75 } 76 77 @Override toString()78 public String toString() { 79 return "ReductionResult{" + 80 "boundSet=" + boundSet + 81 ", constraintFormulas=" + constraintFormulas + 82 '}'; 83 } 84 getConstraint(int index)85 public ConstraintFormula getConstraint(int index) { 86 if (constraintFormulas.size() <= index) { 87 throw new IllegalArgumentException("Constraint with index " + index + " is not available as there are " + constraintFormulas.size() + " constraints"); 88 } 89 return constraintFormulas.get(index); 90 } 91 oneConstraint(ConstraintFormula constraintFormula)92 public static ReductionResult oneConstraint(ConstraintFormula constraintFormula) { 93 return empty().withConstraint(constraintFormula); 94 } 95 withConstraints(ConstraintFormula... constraints)96 public static ReductionResult withConstraints(ConstraintFormula... constraints) { 97 return withConstraints(Arrays.asList(constraints)); 98 } 99 oneBound(Bound bound)100 public static ReductionResult oneBound(Bound bound) { 101 return empty().withBound(bound); 102 } 103 withConstraints(List<ConstraintFormula> constraints)104 public static ReductionResult withConstraints(List<ConstraintFormula> constraints) { 105 ReductionResult reductionResult = new ReductionResult(); 106 reductionResult.constraintFormulas.addAll(constraints); 107 return reductionResult; 108 } 109 bounds(BoundSet bounds)110 public static ReductionResult bounds(BoundSet bounds) { 111 ReductionResult reductionResult = new ReductionResult(); 112 reductionResult.boundSet = bounds; 113 return reductionResult; 114 } 115 } 116 117 /** 118 * A formula is reduced to one or both of: 119 * i) A bound or bound set, which is to be incorporated with the "current" bound set. Initially, the current bound 120 * set is empty. 121 * ii) Further constraint formulas, which are to be reduced recursively. 122 */ reduce(BoundSet currentBoundSet)123 public abstract ReductionResult reduce(BoundSet currentBoundSet); 124 125 } 126