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 CompositeUnaryFunction implements Serializable, UnaryFunction {
24  
25  	/***
26  	 * 
27  	 */
28  	private static final long serialVersionUID = 1L;
29  
30  	private List list = new ArrayList();
31  
32  	public CompositeUnaryFunction() {
33  	}
34  
35  	public CompositeUnaryFunction(UnaryFunction f) {
36  		of(f);
37  	}
38  
39  	public CompositeUnaryFunction(UnaryFunction f, UnaryFunction g) {
40  		of(f);
41  		of(g);
42  	}
43  
44  	// modifiers
45  	// ------------------------------------------------------------------------
46  	public CompositeUnaryFunction of(UnaryFunction f) {
47  		list.add(f);
48  		return this;
49  	}
50  
51  	// predicate interface
52  	// ------------------------------------------------------------------------
53  	public Object evaluate(Object obj) {
54  		Object result = obj;
55  		for (ListIterator iter = list.listIterator(list.size()); iter
56  				.hasPrevious();) {
57  			result = ((UnaryFunction) iter.previous()).evaluate(result);
58  		}
59  		return result;
60  	}
61  
62  }