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 java.util.List;
19  
20  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
21  import org.kuali.rice.krms.api.engine.expression.ComparisonOperatorService;
22  import org.kuali.rice.krms.framework.engine.Function;
23  
24  /**
25   * An implementation of {@link Expression} which invokes a {@link Function} with the results of the invocation of the given
26   * List of {@link Expression}s (of the given {@link ExecutionEnvironment}).
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   * 
29   */
30  public final class FunctionExpression implements Expression<Object> {
31  
32  	private final Function function;
33      private final String [] parameterTypes;
34  	private final List<Expression<? extends Object>> parameters;
35      private final ComparisonOperatorService comparisonOperatorService;
36  
37      /**
38       * Create a FunctionExpression with the given values.
39       * @param function {@link Function} to be invoked using the invoked results of the given List of {@link Expression}s
40       * @param parameterTypes the full class names for the function's parameter types in sequential order
41       * @param parameters List of {@link Expression}s to be invoked whose results (of the given {@link ExecutionEnvironment})
42       * @param comparisonOperatorService -- TODO:
43       * will be used to invoke the given {@link Function}.
44       */
45  	public FunctionExpression(Function function, String[] parameterTypes,
46  			List<Expression<? extends Object>> parameters,
47              ComparisonOperatorService comparisonOperatorService) {
48  		this.function = function;
49          this.parameterTypes = parameterTypes;
50  		this.parameters = parameters;
51          this.comparisonOperatorService = comparisonOperatorService;
52  	}
53  
54  	@Override
55  	public Object invoke(ExecutionEnvironment environment) {
56          Object[] argumentValues = new Object[parameters.size()];
57  
58          int argValIndex = 0;
59  
60  		for (Expression<? extends Object> argument : parameters) {
61  			Object argumentValue = argument.invoke(environment);
62              String expectedArgumentType = parameterTypes[argValIndex];
63  
64              argumentValue = ComparisonOperatorServiceUtils.coerceIfNeeded(argumentValue, expectedArgumentType, comparisonOperatorService);
65  
66              argumentValues[argValIndex] = argumentValue;
67              argValIndex += 1;
68  		}
69  
70  		return function.invoke(argumentValues);
71  	}
72  
73  
74  }