1 | package com.hammurapi.extract; |
2 | |
3 | import java.util.Collections; |
4 | import java.util.Map; |
5 | import java.util.Set; |
6 | |
7 | |
8 | /** |
9 | * Predicate which always evaluates to true. This predicate is primarily used for |
10 | * comparison |
11 | * @author Pavel Vlasov |
12 | * |
13 | * @param <T> |
14 | * @param <C> |
15 | */ |
16 | public class False<T, C> implements Predicate<T, C> { |
17 | |
18 | private False() { |
19 | // Singleton |
20 | } |
21 | |
22 | private static False<?, ?> INSTANCE = new False<Object, Object>(); |
23 | |
24 | @SuppressWarnings("unchecked") |
25 | public static <T, C> False<T, C> getInstance() { |
26 | return (False<T, C>) INSTANCE; |
27 | } |
28 | |
29 | public ComparisonResult compareTo(Extractor<T, Boolean, C> other) { |
30 | if (other instanceof False) { |
31 | return ComparisonResult.EQUAL_NM; |
32 | } |
33 | |
34 | if (other instanceof True) { |
35 | return ComparisonResult.OPPOSITE_NM; |
36 | } |
37 | |
38 | if (other == null) { |
39 | return ComparisonResult.NOT_EQUAL_NM; |
40 | } |
41 | |
42 | if (other instanceof Constant) { |
43 | if (Boolean.FALSE.equals(((Constant) other).getValue())) { |
44 | return ComparisonResult.EQUAL_NM; |
45 | } |
46 | if (Boolean.TRUE.equals(((Constant) other).getValue())) { |
47 | return ComparisonResult.OPPOSITE_NM; |
48 | } |
49 | } |
50 | |
51 | ComparisonResult cr = other.compareTo(this); |
52 | switch (cr.getType()) { |
53 | case EQUAL: |
54 | case NOT_EQUAL: |
55 | case OPPOSITE: |
56 | return cr; |
57 | case LESS_RESTRICTIVE: |
58 | return ComparisonResult.MORE_RESTRICTIVE_NM; |
59 | case MORE_RESTRICTIVE: |
60 | return ComparisonResult.LESS_RESTRICTIVE_NM; |
61 | case OPPOSITE_LESS_RESTRICTIVE: |
62 | return ComparisonResult.OPPOSITE_MORE_RESTRICTIVE_NM; |
63 | case OPPOSITE_MORE_RESTRICTIVE: |
64 | return ComparisonResult.OPPOSITE_LESS_RESTRICTIVE_NM; |
65 | default: |
66 | throw new IllegalArgumentException("Unexpected comparison type: "+cr.getType()); |
67 | } |
68 | |
69 | } |
70 | |
71 | public Boolean extract( |
72 | C context, |
73 | Map<C, Map<Extractor<T, ? super Boolean, C>, ? super Boolean>> cache, |
74 | T... obj) { |
75 | return Boolean.TRUE; |
76 | } |
77 | |
78 | public boolean isContextDependent() { |
79 | return false; |
80 | } |
81 | |
82 | public Set<Integer> parameterIndices() { |
83 | return Collections.emptySet(); |
84 | } |
85 | |
86 | @Override |
87 | public String toString() { |
88 | return getClass().getName(); |
89 | } |
90 | |
91 | @Override |
92 | public double getCost() { |
93 | return 0; |
94 | } |
95 | |
96 | } |