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 org.seasar.tuigwaa.util.functor;
17  
18  import java.util.Collection;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import com.isenshi.util.functor.BinaryFunction;
24  import com.isenshi.util.functor.BinaryPredicate;
25  import com.isenshi.util.functor.UnaryFunction;
26  
27  public class Op {
28  
29  	public static final BinaryFunction SUM = new BinaryFunction(){
30  		public Object evaluate(Object left, Object right) {
31  			if(left instanceof Long && right instanceof Long){
32  				return new Long(((Long)left).longValue() + ((Long)right).longValue());
33  			}else if(left instanceof Double && right instanceof Double){
34  				return new Double(((Double)left).doubleValue() + ((Double)right).doubleValue());
35  			}else if(left instanceof Integer && right instanceof Integer){
36  				return new Integer(((Integer)left).intValue() + ((Integer)right).intValue());
37  			}else if(left instanceof Float && right instanceof Float){
38  				return new Float(((Float)left).floatValue() + ((Float)right).floatValue());
39  			}
40  			return null;//"" + left + right;
41  		}
42  	};
43  	
44  	public static final BinaryFunction MAX = new BinaryFunction(){
45  		public Object evaluate(Object left, Object right) {
46  			return (GT.test(left, right))?left:right;
47  		}
48  	};
49  		
50  	public static final BinaryPredicate EQ = new BinaryPredicate() {
51  		public boolean test(Object left, Object right) {
52  			return (null == left ? null == right : left.equals(right));
53  		}
54  	};
55  
56  	public static final BinaryPredicate NE = new BinaryPredicate() {
57  		public boolean test(Object left, Object right) {
58  			return (null == left ? null != right : !left.equals(right));
59  		}
60  	};
61  
62  	public static final BinaryPredicate LT = new BinaryPredicate() {
63  		public boolean test(Object left, Object right) {
64  			if (left == null) {
65  				return false;
66  			}
67  			return ((Comparable) left).compareTo(right) < 0;
68  		}
69  	};
70  
71  	public static final BinaryPredicate LE = new BinaryPredicate() {
72  		public boolean test(Object left, Object right) {
73  			if (left == null) {
74  				return false;
75  			}
76  			return ((Comparable) left).compareTo(right) <= 0;
77  		}
78  	};
79  
80  	public static final BinaryPredicate GT = new BinaryPredicate() {
81  		public boolean test(Object left, Object right) {
82  			if (left == null) {
83  				return false;
84  			}
85  			return ((Comparable) left).compareTo(right) > 0;
86  		}
87  	};
88  
89  	public static final BinaryPredicate GE = new BinaryPredicate() {
90  		public boolean test(Object left, Object right) {
91  			if (left == null) {
92  				return false;
93  			}
94  			return ((Comparable) left).compareTo(right) >= 0;
95  		}
96  	};
97  	
98  	public static Map OP_MAP;
99  
100 	static{
101 		Map tmp = new HashMap();
102 		tmp.put("eq", EQ);
103 		tmp.put("ne", NE);
104 		tmp.put("lt", LT);
105 		tmp.put("le", LE);
106 		tmp.put("gt", GT);
107 		tmp.put("ge", GE);
108 		OP_MAP = Collections.unmodifiableMap(tmp);
109 	}
110 	
111 	public static BinaryPredicate getInstance(String opcode){
112 		return (BinaryPredicate)OP_MAP.get(opcode);
113 	}
114 	
115 	public static final BinaryFunction NOTHING_BINARY_FUNCTION = new BinaryFunction(){
116 		public Object evaluate(Object left, Object right) {
117 			return left;
118 		}
119 	};
120 	
121 	public static final UnaryFunction COLLECTIONSIZE_FUNCTION = new UnaryFunction(){
122 	
123 		public Object evaluate(Object obj) {
124 			Collection col = (Collection)obj;
125 			if(col == null){
126 				return new Integer(0);
127 			}
128 			return new Integer(col.size());
129 		}
130 	};
131 }