1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46 public CompositeBinaryFunction of(BinaryFunction f) {
47 list.add(f);
48 return this;
49 }
50
51
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 }