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.springframework.util.Assert;
26 import org.springmodules.validation.valang.functions.Function;
27
28 /**
29 * <p>AbstractPropertyPredicate deals with extracting values from properties and as such
30 * is a utilty base class.
31 *
32 * @author Steven Devijver
33 * @since 23-04-2005
34 */
35 public abstract class AbstractPropertyPredicate implements Predicate {
36
37 private final Function leftFunction;
38 private final Operator operator;
39 private final Function rightFunction;
40 private final int line;
41 private final int column;
42
43 /**
44 * Constructor
45 */
46 public AbstractPropertyPredicate(Function leftFunction, Operator operator, Function rightFunction,
47 int line, int column) {
48 Assert.notNull(leftFunction, "Left function parameter should not be null!.");
49 Assert.notNull(operator, "Operator parameter should not be null!.");
50
51 this.leftFunction = leftFunction;
52 this.operator = operator;
53 this.rightFunction = rightFunction;
54 this.line = line;
55 this.column = column;
56 }
57
58 /**
59 * Gets left function.
60 */
61 public final Function getLeftFunction() {
62 return this.leftFunction;
63 }
64
65 /**
66 * Gets operator.
67 */
68 public final Operator getOperator() {
69 return this.operator;
70 }
71
72 /**
73 * Gets right function.
74 */
75 public final Function getRightFunction() {
76 return this.rightFunction;
77 }
78
79 /**
80 * Gets line.
81 */
82 public int getLine() {
83 return line;
84 }
85
86 /**
87 * Sets line.
88 */
89 public int getColumn() {
90 return column;
91 }
92
93 /**
94 * Gets <code>Iterator</code>.
95 */
96 protected final Iterator getIterator(Object literal) {
97 if (literal instanceof Collection) {
98 return ((Collection) literal).iterator();
99 } else if (literal instanceof Iterator) {
100 return (Iterator) literal;
101 } else if (literal instanceof Enumeration) {
102 return IteratorUtils.asIterator((Enumeration) literal);
103 } else if (literal instanceof Object[]) {
104 return IteratorUtils.arrayIterator((Object[]) literal);
105 } else {
106 throw new ClassCastException("Could not convert literal value to iterator!");
107 }
108 }
109
110 /**
111 * Gets array.
112 */
113 protected final Object[] getArray(Object literal) {
114 if (literal instanceof Collection) {
115 return ((Collection) literal).toArray();
116 } else if (literal instanceof Iterator) {
117 return IteratorUtils.toArray((Iterator) literal);
118 } else if (literal instanceof Enumeration) {
119 return IteratorUtils.toArray(IteratorUtils.asIterator((Enumeration) literal));
120 } else if (literal instanceof Object[]) {
121 return (Object[]) literal;
122 } else {
123 throw new ClassCastException("Could not convert literal value to array!");
124 }
125 }
126
127 /**
128 * Evaluate expression.
129 */
130 public abstract boolean evaluate(Object target);
131
132
133
134 }