01: /**
02: * Copyright 2007 Jens Dietrich Licensed under the Apache License, Version 2.0 (the "License");
03: * you may not use this file except in compliance with the License.
04: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
05: * Unless required by applicable law or agreed to in writing, software distributed under the
06: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
07: * either express or implied. See the License for the specific language governing permissions
08: * and limitations under the License.
09: */package nz.org.take;
10:
11: /**
12: * Predicate backed by a Java method returning a boolean.
13: * @author <a href="http://www-ist.massey.ac.nz/JBDietrich/">Jens Dietrich</a>
14: */
15:
16: public class JPredicate extends AbstractPredicate {
17:
18: private java.lang.reflect.Method method = null;
19:
20: @Override
21: public int hashCode() {
22: final int PRIME = 31;
23: int result = 1;
24: result = PRIME * result
25: + ((method == null) ? 0 : method.hashCode());
26: result = PRIME * result + (negated ? 1231 : 1237);
27: return result;
28: }
29:
30: @Override
31: public boolean equals(Object obj) {
32: if (this == obj)
33: return true;
34: if (obj == null)
35: return false;
36: if (getClass() != obj.getClass())
37: return false;
38: final JPredicate other = (JPredicate) obj;
39: if (method == null) {
40: if (other.method != null)
41: return false;
42: } else if (!method.equals(other.method))
43: return false;
44: if (negated != other.negated)
45: return false;
46: return true;
47: }
48:
49: public String getName() {
50: return method == null ? null : method.getName();
51: }
52:
53: public Class[] getSlotTypes() {
54: Class[] types = new Class[method.getParameterTypes().length + 1];
55: types[0] = method.getDeclaringClass();
56: for (int i = 1; i < types.length; i++)
57: types[i] = method.getParameterTypes()[i - 1];
58: return types;
59: }
60:
61: public java.lang.reflect.Method getMethod() {
62: return method;
63: }
64:
65: public void setMethod(java.lang.reflect.Method method) {
66: this .method = method;
67: }
68:
69: public String toString() {
70: return (negated ? "!" : "") + method.getName();
71: }
72:
73: }
|