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.util.Collection;
20  
21  import org.springmodules.validation.util.condition.NonNullAcceptingCondition;
22  
23  /**
24   * A base class for all collection conditions.
25   *
26   * @author Uri Boness
27   */
28  public abstract class AbstractCollectionCondition extends NonNullAcceptingCondition {
29  
30      /**
31       * See {@link NonNullAcceptingCondition#beforeObjectChecked(Object)}.
32       * <p/>
33       * Also verifies the given object is of a {@link Collection} or an array.
34       */
35      protected void beforeObjectChecked(Object object) {
36          super.beforeObjectChecked(object);
37          if (!(object instanceof Collection) && !object.getClass().isArray()) {
38              throw new IllegalArgumentException(getClass().getName() + " can only checkCalendar collection, iterators, or arrays");
39          }
40      }
41  
42      /**
43       * See {@link org.springmodules.validation.util.condition.AbstractCondition#doCheck(Object)}.<br/>
44       * <p/>
45       * If the given object is an array, the call is delegated to {@link #checkArray(Object)}.<br/>
46       * <p/>
47       * If the given object is of a {@link Collection} type, the call is delegated to
48       * {@link #checkCollection(java.util.Collection)}.
49       */
50      public final boolean doCheck(Object object) {
51          if (object.getClass().isArray()) {
52              return checkArray(object);
53          }
54          return checkCollection((Collection) object);
55      }
56  
57      /**
58       * Checks whether the given array adheres to this Condition.
59       *
60       * @param array The array to be checked.
61       * @return <code>true</code> if the given array adheres to this condition, <code>false</code> otherwise.
62       */
63      protected abstract boolean checkArray(Object array);
64  
65      /**
66       * Checks whether the given collection adheres to this Condition.
67       *
68       * @param collection The collection to be checked.
69       * @return <code>true</code> if the given collection adheres to this condition, <code>false</code> otherwise.
70       */
71      protected abstract boolean checkCollection(Collection collection);
72  
73  }