1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springmodules.validation.valang.predicates;
18
19 import java.util.Collection;
20 import java.util.Enumeration;
21 import java.util.Iterator;
22
23 import org.apache.commons.collections.IteratorUtils;
24 import org.apache.commons.collections.Predicate;
25 import org.springmodules.validation.valang.functions.Function;
26
27 /**
28 * <p>AbstractPropertyPredicate deals with extracting values from properties and as such
29 * is a utilty base class.
30 *
31 * @author Steven Devijver
32 * @since 23-04-2005
33 */
34 public abstract class AbstractPropertyPredicate implements Predicate {
35
36 private Function leftFunction = null;
37
38 private Operator operator = null;
39
40 private Function rightFunction = null;
41
42 private int line = 0;
43
44 private int column = 0;
45
46 public AbstractPropertyPredicate(Function leftFunction, Operator operator, Function rightFunction, int line, int column) {
47 super();
48 setLeftFunction(leftFunction);
49 setOperator(operator);
50 setRightFunction(rightFunction);
51 setLine(line);
52 setColumn(column);
53 }
54
55 private void setLeftFunction(Function leftFunction) {
56 if (leftFunction == null) {
57 throw new IllegalArgumentException("Left function parameter should not be null!");
58 }
59 this.leftFunction = leftFunction;
60 }
61
62 public final Function getLeftFunction() {
63 return this.leftFunction;
64 }
65
66 private void setOperator(Operator operator) {
67 if (operator == null) {
68 throw new IllegalArgumentException("Operator parameter should not be null!");
69 }
70 this.operator = operator;
71 }
72
73 public final Operator getOperator() {
74 return this.operator;
75 }
76
77 private void setRightFunction(Function rightFunction) {
78 this.rightFunction = rightFunction;
79 }
80
81 public final Function getRightFunction() {
82 return this.rightFunction;
83 }
84
85 public int getLine() {
86 return line;
87 }
88
89 public void setLine(int line) {
90 this.line = line;
91 }
92
93 public int getColumn() {
94 return column;
95 }
96
97 public void setColumn(int column) {
98 this.column = column;
99 }
100
101 protected final Iterator getIterator(Object literal) {
102 if (literal instanceof Collection) {
103 return ((Collection) literal).iterator();
104 } else if (literal instanceof Iterator) {
105 return (Iterator) literal;
106 } else if (literal instanceof Enumeration) {
107 return IteratorUtils.asIterator((Enumeration) literal);
108 } else if (literal instanceof Object[]) {
109 return IteratorUtils.arrayIterator((Object[]) literal);
110 } else {
111 throw new ClassCastException("Could not convert literal value to iterator!");
112 }
113 }
114
115 protected final Object[] getArray(Object literal) {
116 if (literal instanceof Collection) {
117 return ((Collection) literal).toArray();
118 } else if (literal instanceof Iterator) {
119 return IteratorUtils.toArray((Iterator) literal);
120 } else if (literal instanceof Enumeration) {
121 return IteratorUtils.toArray(IteratorUtils.asIterator((Enumeration) literal));
122 } else if (literal instanceof Object[]) {
123 return (Object[]) literal;
124 } else {
125 throw new ClassCastException("Could not convert literal value to array!");
126 }
127 }
128
129 public abstract boolean evaluate(Object target);
130
131 protected abstract Predicate getPredicate(Function leftFunction, Operator operator, Function rightFunction, int line, int column);
132 }