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.collection;
18  
19  import java.lang.reflect.Array;
20  import java.util.Collection;
21  import java.util.Iterator;
22  
23  import org.springmodules.validation.util.condition.Condition;
24  
25  /**
26   * An {@link AbstractCollectionCondition} implementation that checks whether none of the elements in a collection
27   * or array adhere to a specific condition.
28   *
29   * @author Uri Boness
30   */
31  public class NoneCollectionCondition extends AbstractCollectionElementCondition {
32  
33      /**
34       * Constructs a new NoneCollectionCondition with a given condition.
35       *
36       * @param elementCondition The condition to be checked on the collection/array elements.
37       */
38      public NoneCollectionCondition(Condition elementCondition) {
39          super(elementCondition);
40      }
41  
42      /**
43       * Checks whether none of the object in the given array adheres to the associated condition.
44       *
45       * @param array The array to be checked.
46       * @return <code>true</code> none of the objects in the given array adheres to the associated condition,
47       *         <code>false</code> otherwise.
48       */
49      protected boolean checkArray(Object array) {
50          for (int i = 0; i < Array.getLength(array); i++) {
51              if (getElementCondition().check(Array.get(array, i))) {
52                  return false;
53              }
54          }
55          return true;
56      }
57  
58      /**
59       * Checks whether none of the elements in the given collection adheres to the associated condition.
60       *
61       * @param collection The collection to be checked.
62       * @return <code>true</code> if none of the elements in the given collection adheres to the associated condition,
63       *         <code>false</code> otherwise.
64       */
65      protected boolean checkCollection(Collection collection) {
66          for (Iterator iter = collection.iterator(); iter.hasNext();) {
67              if (getElementCondition().check(iter.next())) {
68                  return false;
69              }
70          }
71          return true;
72      }
73  
74  }