| 1 | package com.hammurapi.extract; |
| 2 | |
| 3 | import java.util.concurrent.TimeUnit; |
| 4 | |
| 5 | |
| 6 | public class Equal<T, V, C> extends ComparisonPredicate<T, V, C> { |
| 7 | |
| 8 | private boolean identity; |
| 9 | |
| 10 | public Equal( |
| 11 | double initialCost, |
| 12 | TimeUnit costUnit, |
| 13 | Extractor<T, V, C> leftExtractor, |
| 14 | Extractor<T, V, C> rightExtractor, |
| 15 | boolean identity) { |
| 16 | super(initialCost, costUnit, leftExtractor, rightExtractor); |
| 17 | this.identity = identity; |
| 18 | } |
| 19 | |
| 20 | @Override |
| 21 | @SuppressWarnings("unchecked") |
| 22 | public ComparisonResult compareTo(Extractor<T, Boolean, C> otherPredicate) { |
| 23 | |
| 24 | if (otherPredicate instanceof Equal) { |
| 25 | if (equals(otherPredicate) || operandsAreReverseEqual((Equal) otherPredicate)) { |
| 26 | return ComparisonResult.EQUAL_NM; |
| 27 | } |
| 28 | |
| 29 | ComparisonResult cr = null; |
| 30 | if (leftExtractor.equals(((ComparisonPredicate) otherPredicate).leftExtractor)) { |
| 31 | cr = rightExtractor.compareTo(((ComparisonPredicate) otherPredicate).rightExtractor); |
| 32 | } else if (leftExtractor.equals(((ComparisonPredicate) otherPredicate).rightExtractor)) { |
| 33 | cr = rightExtractor.compareTo(((ComparisonPredicate) otherPredicate).leftExtractor); |
| 34 | } else if (rightExtractor.equals(((ComparisonPredicate) otherPredicate).leftExtractor)) { |
| 35 | cr = leftExtractor.compareTo(((ComparisonPredicate) otherPredicate).rightExtractor); |
| 36 | } else if (rightExtractor.equals(((ComparisonPredicate) otherPredicate).rightExtractor)) { |
| 37 | cr = leftExtractor.compareTo(((ComparisonPredicate) otherPredicate).leftExtractor); |
| 38 | } |
| 39 | |
| 40 | if (cr!=null && cr.isOneToOneMapping()) { |
| 41 | switch (cr.getType()) { |
| 42 | case UNEQUAL: |
| 43 | return ComparisonResult.OPPOSITE_LESS_RESTRICTIVE_NM; |
| 44 | case OPPOSITE: |
| 45 | return ComparisonResult.OPPOSITE_NM; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if (otherPredicate instanceof NotEqual) { |
| 51 | NotEqual<T, V, C> op = (NotEqual<T, V, C>) otherPredicate; |
| 52 | return operandsAreEqual(op) || operandsAreReverseEqual(op) ? ComparisonResult.OPPOSITE_NM : ComparisonResult.NOT_EQUAL_NM; |
| 53 | } |
| 54 | |
| 55 | if (otherPredicate instanceof Not) { |
| 56 | Not<T, C> op = (Not<T, C>) otherPredicate; |
| 57 | ComparisonResult cr = compareTo(op.getPredicate()); |
| 58 | if (cr.isOneToOneMapping()) { |
| 59 | switch (cr.getType()) { |
| 60 | case EQUAL: |
| 61 | return ComparisonResult.OPPOSITE_NM; |
| 62 | case OPPOSITE: |
| 63 | return ComparisonResult.EQUAL_NM; |
| 64 | default: |
| 65 | return ComparisonResult.NOT_EQUAL_NM; |
| 66 | } |
| 67 | } |
| 68 | /** |
| 69 | * TODO Implement comparisons. |
| 70 | * a * b ? c * d, * - can be <, <=, ==, !=, =>, > |
| 71 | * 36 permutations. Each permutation - compare a/b, c/d |
| 72 | */ |
| 73 | |
| 74 | // TODO Address mappings |
| 75 | } |
| 76 | return super.compareTo(otherPredicate); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected boolean compare(int n1, int n2) { |
| 81 | return n1==n2; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected boolean compare(Object o1, Object o2) { |
| 86 | if (identity) { |
| 87 | return o1==o2; |
| 88 | } |
| 89 | |
| 90 | if (o1==null) { |
| 91 | return o2==null; |
| 92 | } |
| 93 | |
| 94 | return o1.equals(o2); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | protected boolean compare(long n1, long n2) { |
| 99 | return n1==n2; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | protected boolean compare(float n1, float n2) { |
| 104 | return n1==n2; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | protected boolean compare(double n1, double n2) { |
| 109 | return n1==n2; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | protected Extractor<T, Boolean, C> newInstance(Extractor<T, V, C> leftExtractor, Extractor<T, V, C> rightExtractor) { |
| 114 | return new Equal<T,V,C>(initialCost, costUnit, leftExtractor, rightExtractor, identity); |
| 115 | } |
| 116 | |
| 117 | public boolean isIdentity() { |
| 118 | return identity; |
| 119 | } |
| 120 | } |