1 /**
2 * Copyright 2005-2013 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.KrmsApiServiceLocator;
19 import org.kuali.rice.krms.api.engine.expression.ComparisonOperatorService;
20
21 /**
22 * Contains utility methods for working with the ComparisonOperatorService
23 */
24 public final class ComparisonOperatorServiceUtils {
25
26 // don't allow instances
27 private ComparisonOperatorServiceUtils() {
28 throw new UnsupportedOperationException();
29 }
30
31 /**
32 * <P>checks if the value needs to be coerced from a String to another type, and if so it looks for an
33 * applicable StringCoercionExtension.</p>
34 *
35 * @param value the value of the argument that may or may not need coercion
36 * @param expectedType the name of the type that the function is expecting for the argument
37 * @param comparisonOperatorService the ComparisonOperatorService instance to use (if needed) for finding a StringCoercionExtension
38 * @return the coerced value, or the unchanged value if (1) no coercion needs to be done or (2) an applicable
39 * StringCoercionExtension can't be found.
40 */
41 public static Object coerceIfNeeded(Object value, String expectedType, ComparisonOperatorService comparisonOperatorService) {
42
43 Object result = value;
44
45 if (value instanceof String && !"java.lang.String".equals(expectedType)) {
46 String valueString = value.toString();
47 StringCoercionExtension coercionExtension =
48 comparisonOperatorService.findStringCoercionExtension(expectedType, valueString);
49 if (coercionExtension != null) {
50 result = coercionExtension.coerce(expectedType, valueString);
51 }
52 }
53
54 return result;
55 }
56 }