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 True<T, C> implements Predicate<T, C> { |
17 | |
18 | private True() { |
19 | // Singleton |
20 | } |
21 | |
22 | private static True<?, ?> INSTANCE = new True<Object, Object>(); |
23 | |
24 | @SuppressWarnings("unchecked") |
25 | public static <T, C> True<T, C> getInstance() { |
26 | return (True<T, C>) INSTANCE; |
27 | } |
28 | |
29 | public ComparisonResult compareTo(Extractor<T, Boolean, C> otherPredicate) { |
30 | if (otherPredicate instanceof True) { |
31 | return ComparisonResult.EQUAL_NM; |
32 | } |
33 | |
34 | if (otherPredicate instanceof False) { |
35 | return ComparisonResult.OPPOSITE_NM; |
36 | } |
37 | |
38 | |
39 | if (otherPredicate == null) { |
40 | return ComparisonResult.NOT_EQUAL_NM; |
41 | } |
42 | |
43 | if (otherPredicate instanceof Constant) { |
44 | if (Boolean.TRUE.equals(((Constant) otherPredicate).getValue())) { |
45 | return ComparisonResult.EQUAL_NM; |
46 | } |
47 | if (Boolean.FALSE.equals(((Constant) otherPredicate).getValue())) { |
48 | return ComparisonResult.OPPOSITE_NM; |
49 | } |
50 | } |
51 | |
52 | ComparisonResult cr = otherPredicate.compareTo(this); |
53 | switch (cr.getType()) { |
54 | case EQUAL: |
55 | case NOT_EQUAL: |
56 | case OPPOSITE: |
57 | return cr; |
58 | case LESS_RESTRICTIVE: |
59 | return ComparisonResult.MORE_RESTRICTIVE_NM; |
60 | case MORE_RESTRICTIVE: |
61 | return ComparisonResult.LESS_RESTRICTIVE_NM; |
62 | case OPPOSITE_LESS_RESTRICTIVE: |
63 | return ComparisonResult.OPPOSITE_MORE_RESTRICTIVE_NM; |
64 | case OPPOSITE_MORE_RESTRICTIVE: |
65 | return ComparisonResult.OPPOSITE_LESS_RESTRICTIVE_NM; |
66 | default: |
67 | throw new IllegalArgumentException("Unexpected comparison type: "+cr.getType()); |
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 | } |