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;
18  
19  /**
20   * A base class for all type specific conditions. A type specific instantCondition can only checkCalendar specific object types.
21   *
22   * @author Uri Boness
23   */
24  public abstract class TypeSpecificCondition extends AbstractCondition {
25  
26      private Class[] types;
27  
28      /**
29       * Construts a new TypeSpecificCondition with the given supported type. Sub classes should call this constructor
30       * if they only support on object type.
31       *
32       * @param type The object type this condition supports.
33       */
34      public TypeSpecificCondition(Class type) {
35          this(new Class[]{type});
36      }
37  
38      /**
39       * Constructs a new TypeSpecificCondition with a the give list of supported types. Sub-classes should call this
40       * constructor if they support more then one object type.
41       *
42       * @param types The object types this condition supports.
43       */
44      public TypeSpecificCondition(Class[] types) {
45          this.types = types;
46      }
47  
48      /**
49       * Checks whether the checked object is of one of the types supported by this condition.
50       *
51       * @param object The object to be checked.
52       * @throws IllegalArgumentException if the checked object type is not supported by this condition.
53       */
54      protected void beforeObjectChecked(Object object) {
55  
56          boolean foundMatchingType = false;
57  
58          for (int i = 0; i < types.length; i++) {
59              if (types[i].isAssignableFrom(object.getClass())) {
60                  foundMatchingType = true;
61                  break;
62              }
63          }
64  
65          if (!foundMatchingType) {
66              StringBuffer message = new StringBuffer(getClass().getName());
67              message.append("can only validation values of the following types: ");
68              for (int j = 0; j < types.length; j++) {
69                  if (j != 0) {
70                      message.append(", ");
71                  }
72                  message.append(types[j].getName());
73              }
74              throw new IllegalArgumentException(message.toString());
75          }
76      }
77  
78  }