1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.framework.engine.expression;
17
18 import org.kuali.rice.krms.api.engine.expression.ComparisonOperatorService;
19
20 import java.util.Iterator;
21 import java.util.LinkedList;
22 import java.util.List;
23
24
25
26
27
28 public class ComparisonOperatorServiceImpl implements ComparisonOperatorService {
29
30 private List<EngineComparatorExtension> operators = new LinkedList<EngineComparatorExtension>();
31
32 private List<StringCoercionExtension> stringCoercionExtensions = new LinkedList<StringCoercionExtension>();
33
34 private ComparisonOperatorServiceImpl() {}
35
36
37
38
39
40 public static ComparisonOperatorService getInstance() {
41 return new ComparisonOperatorServiceImpl();
42 }
43
44 @Override
45 public List<StringCoercionExtension> getStringCoercionExtensions() {
46 return stringCoercionExtensions;
47 }
48
49 @Override
50 public void setStringCoercionExtensions(List<StringCoercionExtension> stringCoercionExtensions) {
51 this.stringCoercionExtensions = stringCoercionExtensions;
52 }
53
54 @Override
55 public List<EngineComparatorExtension> getOperators() {
56 return operators;
57 }
58
59 @Override
60 public void setOperators(List<EngineComparatorExtension> operators) {
61 this.operators = operators;
62 }
63
64
65
66
67
68
69
70
71 @Override
72 public EngineComparatorExtension findComparatorExtension(Object lhs, Object rhs) {
73 EngineComparatorExtension extension;
74 Iterator<EngineComparatorExtension> opIter = operators.iterator();
75 while (opIter.hasNext()) {
76 extension = opIter.next();
77 if (extension.canCompare(lhs, rhs)) {
78 return extension;
79 }
80 }
81 return new DefaultComparisonOperator();
82 }
83
84 @Override
85 public int compare(Object lhs, Object rhs) {
86 return findComparatorExtension(lhs, rhs).compare(lhs, rhs);
87 }
88
89 @Override
90 public boolean canCompare(Object lhs, Object rhs) {
91 return findComparatorExtension(lhs, rhs) != null;
92 }
93
94
95
96
97
98
99
100
101 @Override
102 public StringCoercionExtension findStringCoercionExtension(String type, String value) {
103 StringCoercionExtension extension;
104 Iterator<StringCoercionExtension> opIter = stringCoercionExtensions.iterator();
105 while (opIter.hasNext()) {
106 extension = opIter.next();
107 if (extension.canCoerce(type, value)) {
108 return extension;
109 }
110 }
111 return new DefaultComparisonOperator();
112 }
113
114 @Override
115 public boolean canCoerce(String type, String value) {
116 return findStringCoercionExtension(type, value) != null;
117 }
118
119 @Override
120 public Object coerce(String type, String value) {
121 return findStringCoercionExtension(type, value).coerce(type, value);
122 }
123 }