|
| 1 | +package liquidjava.rj_language; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Objects; |
| 6 | + |
| 7 | +import spoon.reflect.reference.CtTypeReference; |
| 8 | + |
| 9 | +/** |
| 10 | + * Represents a predicate simplified from another predicate. Stores the original predicate and any variables that must |
| 11 | + * be reintroduced as binders when relating the simplified predicate back to its origin. |
| 12 | + * <p> |
| 13 | + * For example, simplifying {@code x == 1 && y > x} with binders {@code x: int, y: int} may produce {@code y > 1}. The |
| 14 | + * origin {@code x == 1 && y > x} and binder {@code x: int} are kept so we can relate the simplified predicate back to |
| 15 | + * the original. |
| 16 | + */ |
| 17 | +public class SimplifiedPredicate extends Predicate { |
| 18 | + |
| 19 | + private final Predicate origin; |
| 20 | + private final List<Binder> binders; |
| 21 | + |
| 22 | + public SimplifiedPredicate(Predicate simplified, Predicate origin) { |
| 23 | + this(simplified, origin, List.of()); |
| 24 | + } |
| 25 | + |
| 26 | + public SimplifiedPredicate(Predicate simplified, Predicate origin, List<Binder> binders) { |
| 27 | + super(simplified.getExpression()); |
| 28 | + this.origin = origin; |
| 29 | + this.binders = new ArrayList<>(binders); |
| 30 | + } |
| 31 | + |
| 32 | + public Predicate getSimplifiedPredicate() { |
| 33 | + return new Predicate(getExpression()); |
| 34 | + } |
| 35 | + |
| 36 | + public Predicate getOrigin() { |
| 37 | + return origin; |
| 38 | + } |
| 39 | + |
| 40 | + public List<Binder> getBinders() { |
| 41 | + return binders; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public SimplifiedPredicate clone() { |
| 46 | + return new SimplifiedPredicate(new Predicate(getExpression().clone()), origin.clone(), binders); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public int hashCode() { |
| 51 | + return Objects.hash(getExpression(), origin, binders); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean equals(Object obj) { |
| 56 | + if (this == obj) |
| 57 | + return true; |
| 58 | + if (obj == null) |
| 59 | + return false; |
| 60 | + if (getClass() != obj.getClass()) |
| 61 | + return false; |
| 62 | + SimplifiedPredicate other = (SimplifiedPredicate) obj; |
| 63 | + return getExpression().equals(other.getExpression()) && origin.equals(other.origin) |
| 64 | + && binders.equals(other.binders); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Represents a variable that must be bound when relating the simplified predicate to its origin |
| 69 | + */ |
| 70 | + public static class Binder { |
| 71 | + private final String name; |
| 72 | + private final String type; |
| 73 | + |
| 74 | + public Binder(String name, String type) { |
| 75 | + this.name = name; |
| 76 | + this.type = type; |
| 77 | + } |
| 78 | + |
| 79 | + public Binder(String name, CtTypeReference<?> type) { |
| 80 | + this(name, type.getQualifiedName()); |
| 81 | + } |
| 82 | + |
| 83 | + public String getName() { |
| 84 | + return name; |
| 85 | + } |
| 86 | + |
| 87 | + public String getType() { |
| 88 | + return type; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public int hashCode() { |
| 93 | + return Objects.hash(name, type); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean equals(Object obj) { |
| 98 | + if (this == obj) |
| 99 | + return true; |
| 100 | + if (obj == null) |
| 101 | + return false; |
| 102 | + if (getClass() != obj.getClass()) |
| 103 | + return false; |
| 104 | + Binder other = (Binder) obj; |
| 105 | + return name.equals(other.name) && type.equals(other.type); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments