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.util.condition.adapter;
18  
19  import org.apache.commons.collections.Predicate;
20  import org.springframework.util.Assert;
21  import org.springmodules.validation.util.condition.AbstractCondition;
22  
23  /**
24   * A Jakarta Commmons Predicate condition adapter. This condition deligates the call to an associated {@link Predicate).
25   *
26   * @author Uri Boness
27   */
28  public class CommonsPredicateCondition extends AbstractCondition {
29  
30      private Predicate predicate;
31  
32      /**
33       * Constructs a new CommonsPredicateCondition with a given predicate.
34       *
35       * @param predicate The given predicate.
36       */
37      public CommonsPredicateCondition(Predicate predicate) {
38          Assert.notNull(predicate, "Predicate cannot be null");
39          this.predicate = predicate;
40      }
41  
42      /**
43       * Checks the given object against the predicate associated with this condition.
44       *
45       * @param object The object to be checked.
46       * @return The result returned by the associated predicate when evaluting the given object.
47       *         See {@link Predicate#evaluate(Object)}.
48       */
49      public boolean doCheck(Object object) {
50          return predicate.evaluate(object);
51      }
52  
53      //============================================= Setter/Getter ===================================================
54  
55      /**
56       * Returns the predicate associated with this condition.
57       *
58       * @return The predicate associated with this condition.
59       */
60      public Predicate getPredicate() {
61          return predicate;
62      }
63  
64  }