View Javadoc

1   /*
2    * Copyright 2004-2009 the original author or authors.
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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 //    protected abstract Predicate getPredicate(Function leftFunction, Operator operator, Function rightFunction, int line, int column);
133     
134 }