001package com.hammurapi.extract;
002
003import java.util.concurrent.TimeUnit;
004
005
006public class NotEqual<T, V, C> extends ComparisonPredicate<T, V, C> {
007
008        private boolean identity;
009
010        public NotEqual(
011                        double initialCost, 
012                        TimeUnit costUnit,
013                        Extractor<T, V, C> leftExtractor, 
014                        Extractor<T, V, C> rightExtractor, 
015                        boolean identity) {
016                super(initialCost, costUnit, leftExtractor, rightExtractor);
017                this.identity = identity;
018        }
019
020        @Override
021        @SuppressWarnings("unchecked")
022        public com.hammurapi.extract.ComparisonResult compareTo(Extractor<T, Boolean, C> otherPredicate) {                
023                if (otherPredicate instanceof Equal) {
024                        Equal<T, V, C> op = (Equal<T, V, C>) otherPredicate;                        
025                        return (operandsAreEqual(op) || operandsAreReverseEqual(op)) ? ComparisonResult.OPPOSITE_NM: ComparisonResult.NOT_EQUAL_NM;
026                } else if (equals(otherPredicate) || (otherPredicate instanceof NotEqual && operandsAreReverseEqual((NotEqual) otherPredicate))) {
027                        return ComparisonResult.EQUAL_NM;
028                } else if (otherPredicate instanceof Not) {
029                        Not<T, C> op = (Not<T, C>) otherPredicate;
030                        ComparisonResult cr = compareTo(op.getPredicate());
031                        if (cr.isOneToOneMapping()) {
032                                switch (cr.getType()) {
033                                case EQUAL:
034                                        return ComparisonResult.EQUAL_NM;
035                                case OPPOSITE:
036                                        return ComparisonResult.OPPOSITE_NM;
037                                default:
038                                        return ComparisonResult.NOT_EQUAL_NM;
039                                }
040                        }
041                }
042                /**
043                 * TODO Implement comparisons.
044                 * a * b ? c * d, * - can be <, <=, ==, !=, =>, >
045                 * 36 permutations. Each permutation - compare a/b, c/d 
046                 */
047                
048                return super.compareTo(otherPredicate);
049        }
050
051        @Override
052        protected boolean compare(int n1, int n2) {
053                return n1!=n2;
054        }
055        
056        @Override
057        protected boolean compare(Object o1, Object o2) {
058                if (identity) {
059                        return o1!=o2;
060                }
061                
062                if (o1==null) {
063                        return o2!=null;
064                }
065
066                return !o1.equals(o2);
067        }
068
069        @Override
070        protected boolean compare(long n1, long n2) {
071                return n1!=n2;
072        }
073
074        @Override
075        protected boolean compare(float n1, float n2) {
076                return n1!=n2;
077        }
078
079        @Override
080        protected boolean compare(double n1, double n2) {
081                return n1!=n2;
082        }
083
084        @Override
085        protected Extractor<T, Boolean, C> newInstance(Extractor<T, V, C> leftExtractor, Extractor<T, V, C> rightExtractor) {
086                return new NotEqual<T,V,C>(initialCost, costUnit, leftExtractor, rightExtractor, identity);
087        }
088        
089        public boolean isIdentity() {
090                return identity;
091        }
092}