View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package com.isenshi.util.functor;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.ListIterator;
22  
23  public class CompositeBinaryFunction implements Serializable, BinaryFunction {
24  
25  	/***
26  	 * 
27  	 */
28  	private static final long serialVersionUID = 1L;
29  
30  	private List list = new ArrayList();
31  
32  	public CompositeBinaryFunction() {
33  	}
34  
35  	public CompositeBinaryFunction(BinaryFunction f) {
36  		of(f);
37  	}
38  
39  	public CompositeBinaryFunction(BinaryFunction f, BinaryFunction g) {
40  		of(f);
41  		of(g);
42  	}
43  
44  	// modifiers
45  	// ------------------------------------------------------------------------
46  	public CompositeBinaryFunction of(BinaryFunction f) {
47  		list.add(f);
48  		return this;
49  	}
50  
51  	// predicate interface
52  	// ------------------------------------------------------------------------
53  	public Object evaluate(Object left, Object right) {
54  		for (ListIterator iter = list.listIterator(list.size()); iter
55  				.hasPrevious();) {
56  			BinaryFunction function = (BinaryFunction) iter.previous();
57  			if(function==null){
58  				continue;
59  			}
60  			left = function.evaluate(left, right);
61  		}
62  		return left;
63  	}
64  
65  }