View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * {@link ComparisonOperatorService} Implementation.
26   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * Factory method for getting a {@link ComparisonOperatorService}
38       * @return a {@link ComparisonOperatorService}
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       * Returns the {@link EngineComparatorExtension} that can compare the lhs and rhs objects.  If none, then returns the
66       * {@link DefaultComparisonOperator}
67       * @param lhs left hand side object
68       * @param rhs right hand side object
69       * @return an EngineComparatorExtension that can compare the lhs and rhs
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       * Returns the {@link EngineComparatorExtension} that can coerce the lhs and rhs objects.  If none, then returns the
96       * {@link DefaultComparisonOperator}, which also handles default coercion
97       * @param type class type to attempt to coerce to
98       * @param value value to attempt to coerce the given type with
99       * @return an EngineComparatorExtension that can coerce the type and value
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(); // default coercion is also in the 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 }