1 | package com.hammurapi.extract; |
2 | |
3 | import java.util.concurrent.TimeUnit; |
4 | |
5 | |
6 | public class NotEqual<T, V, C> extends ComparisonPredicate<T, V, C> { |
7 | |
8 | private boolean identity; |
9 | |
10 | public NotEqual( |
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 com.hammurapi.extract.ComparisonResult compareTo(Extractor<T, Boolean, C> otherPredicate) { |
23 | if (otherPredicate instanceof Equal) { |
24 | Equal<T, V, C> op = (Equal<T, V, C>) otherPredicate; |
25 | return (operandsAreEqual(op) || operandsAreReverseEqual(op)) ? ComparisonResult.OPPOSITE_NM: ComparisonResult.NOT_EQUAL_NM; |
26 | } else if (equals(otherPredicate) || (otherPredicate instanceof NotEqual && operandsAreReverseEqual((NotEqual) otherPredicate))) { |
27 | return ComparisonResult.EQUAL_NM; |
28 | } else if (otherPredicate instanceof Not) { |
29 | Not<T, C> op = (Not<T, C>) otherPredicate; |
30 | ComparisonResult cr = compareTo(op.getPredicate()); |
31 | if (cr.isOneToOneMapping()) { |
32 | switch (cr.getType()) { |
33 | case EQUAL: |
34 | return ComparisonResult.EQUAL_NM; |
35 | case OPPOSITE: |
36 | return ComparisonResult.OPPOSITE_NM; |
37 | default: |
38 | return ComparisonResult.NOT_EQUAL_NM; |
39 | } |
40 | } |
41 | } |
42 | /** |
43 | * TODO Implement comparisons. |
44 | * a * b ? c * d, * - can be <, <=, ==, !=, =>, > |
45 | * 36 permutations. Each permutation - compare a/b, c/d |
46 | */ |
47 | |
48 | return super.compareTo(otherPredicate); |
49 | } |
50 | |
51 | @Override |
52 | protected boolean compare(int n1, int n2) { |
53 | return n1!=n2; |
54 | } |
55 | |
56 | @Override |
57 | protected boolean compare(Object o1, Object o2) { |
58 | if (identity) { |
59 | return o1!=o2; |
60 | } |
61 | |
62 | if (o1==null) { |
63 | return o2!=null; |
64 | } |
65 | |
66 | return !o1.equals(o2); |
67 | } |
68 | |
69 | @Override |
70 | protected boolean compare(long n1, long n2) { |
71 | return n1!=n2; |
72 | } |
73 | |
74 | @Override |
75 | protected boolean compare(float n1, float n2) { |
76 | return n1!=n2; |
77 | } |
78 | |
79 | @Override |
80 | protected boolean compare(double n1, double n2) { |
81 | return n1!=n2; |
82 | } |
83 | |
84 | @Override |
85 | protected Extractor<T, Boolean, C> newInstance(Extractor<T, V, C> leftExtractor, Extractor<T, V, C> rightExtractor) { |
86 | return new NotEqual<T,V,C>(initialCost, costUnit, leftExtractor, rightExtractor, identity); |
87 | } |
88 | |
89 | public boolean isIdentity() { |
90 | return identity; |
91 | } |
92 | } |