    001package com.hammurapi.extract;
    002
    003import java.util.Map;
    004import java.util.Set;
    005
    006import com.hammurapi.extract.ComparisonResult.Type;
    007
    008public class Not<T, C> implements Predicate<T, C>, FacadeExtractor<T, Boolean, C> {
    009
    010        private Predicate<T, C> predicate;
    011
    012        public Not(Predicate<T, C> predicate) {
    013                this.predicate = predicate;
    014        }
    015
    016        public ComparisonResult compareTo(Extractor<T, Boolean, C> otherPredicate) {
    017                ComparisonResult cr = predicate.compareTo(otherPredicate);
    018                switch (cr.getType()) {
    019                case EQUAL:
    020                        return new ComparisonResult(Type.OPPOSITE, cr.getIndexMap());
    021                case LESS_RESTRICTIVE:
    022                        return new ComparisonResult(Type.OPPOSITE_LESS_RESTRICTIVE, cr.getIndexMap());
    023                case MORE_RESTRICTIVE:
    024                        return new ComparisonResult(Type.OPPOSITE_MORE_RESTRICTIVE, cr.getIndexMap());
    025                case NOT_EQUAL:
    026                        return new ComparisonResult(Type.NOT_EQUAL, cr.getIndexMap());
    027                case OPPOSITE:
    028                        return new ComparisonResult(Type.EQUAL, cr.getIndexMap());
    029                case OPPOSITE_LESS_RESTRICTIVE:
    030                        return new ComparisonResult(Type.LESS_RESTRICTIVE, cr.getIndexMap());
    031                case OPPOSITE_MORE_RESTRICTIVE:
    032                        return new ComparisonResult(Type.MORE_RESTRICTIVE, cr.getIndexMap());
    033                default :
    034                        throw new ExtractorException("Invalid comparison result");
    035                }
    036        }
    037
    038        public Predicate<T, C> getPredicate() {
    039                return predicate;
    040        }
    041
    042        public Boolean extract(
    043                        C context,
    044                        Map<C, Map<Extractor<T, ? super Boolean, C>, ? super Boolean>> cache,
    045                        T... obj) {
    046                return !predicate.extract(context, cache, obj);
    047        }
    048
    049        public Set<Integer> parameterIndices() {
    050                return predicate.parameterIndices();
    051        }
    052
    053
    054        public boolean isContextDependent() {
    055                return predicate.isContextDependent();
    056        }
    057
    058        @Override
    059        public double getCost() {
    060                return predicate.getCost();
    061        }
    062
    063}