1 | package com.hammurapi.extract; |
2 | |
3 | import java.util.Map; |
4 | import java.util.Set; |
5 | |
6 | import com.hammurapi.extract.ComparisonResult.Type; |
7 | |
8 | public class Not<T, C> implements Predicate<T, C>, FacadeExtractor<T, Boolean, C> { |
9 | |
10 | private Predicate<T, C> predicate; |
11 | |
12 | public Not(Predicate<T, C> predicate) { |
13 | this.predicate = predicate; |
14 | } |
15 | |
16 | public ComparisonResult compareTo(Extractor<T, Boolean, C> otherPredicate) { |
17 | ComparisonResult cr = predicate.compareTo(otherPredicate); |
18 | switch (cr.getType()) { |
19 | case EQUAL: |
20 | return new ComparisonResult(Type.OPPOSITE, cr.getIndexMap()); |
21 | case LESS_RESTRICTIVE: |
22 | return new ComparisonResult(Type.OPPOSITE_LESS_RESTRICTIVE, cr.getIndexMap()); |
23 | case MORE_RESTRICTIVE: |
24 | return new ComparisonResult(Type.OPPOSITE_MORE_RESTRICTIVE, cr.getIndexMap()); |
25 | case NOT_EQUAL: |
26 | return new ComparisonResult(Type.NOT_EQUAL, cr.getIndexMap()); |
27 | case OPPOSITE: |
28 | return new ComparisonResult(Type.EQUAL, cr.getIndexMap()); |
29 | case OPPOSITE_LESS_RESTRICTIVE: |
30 | return new ComparisonResult(Type.LESS_RESTRICTIVE, cr.getIndexMap()); |
31 | case OPPOSITE_MORE_RESTRICTIVE: |
32 | return new ComparisonResult(Type.MORE_RESTRICTIVE, cr.getIndexMap()); |
33 | default : |
34 | throw new ExtractorException("Invalid comparison result"); |
35 | } |
36 | } |
37 | |
38 | public Predicate<T, C> getPredicate() { |
39 | return predicate; |
40 | } |
41 | |
42 | public Boolean extract( |
43 | C context, |
44 | Map<C, Map<Extractor<T, ? super Boolean, C>, ? super Boolean>> cache, |
45 | T... obj) { |
46 | return !predicate.extract(context, cache, obj); |
47 | } |
48 | |
49 | public Set<Integer> parameterIndices() { |
50 | return predicate.parameterIndices(); |
51 | } |
52 | |
53 | |
54 | public boolean isContextDependent() { |
55 | return predicate.isContextDependent(); |
56 | } |
57 | |
58 | @Override |
59 | public double getCost() { |
60 | return predicate.getCost(); |
61 | } |
62 | |
63 | } |