View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.util;
17  
18  import org.seasar.framework.exception.SIllegalArgumentException;
19  import org.seasar.framework.util.StringUtil;
20  import org.seasar.uruma.core.UrumaMessageCodes;
21  
22  /**
23   * アサーションを行うためのユーティリティクラスです。<br />
24   * 
25   * @author y-komori
26   */
27  public class AssertionUtil {
28      private AssertionUtil() {
29  
30      }
31  
32      /**
33       * <code>arg</code> が <code>null</code> でないことをチェックします。<br />
34       * 
35       * @param name
36       *            オブジェクト名称
37       * @param arg
38       *            チェック対象オブジェクト
39       */
40      public static void assertNotNull(final String name, final Object arg) {
41          if (arg == null) {
42              throw new SIllegalArgumentException(UrumaMessageCodes.CANT_BE_NULL,
43                      new Object[] { name });
44          }
45      }
46  
47      /**
48       * <code>arg</code> が <code>null</code> または空文字列ではないことをチェックします。<br />
49       * 
50       * @param name
51       *            オブジェクト名称
52       * @param arg
53       *            チェック対象文字列
54       */
55      public static void assertNotEmpty(final String name, final String arg) {
56          if (StringUtil.isEmpty(arg)) {
57              throw new SIllegalArgumentException(
58                      UrumaMessageCodes.CANT_BE_EMPTY_STRING,
59                      new Object[] { name });
60          }
61      }
62  
63      /**
64       * <code>arg</code> が <code>clazz</code> のサブクラスであることをチェックします。<br />
65       * 
66       * @param name
67       *            オブジェクト名称
68       * @param clazz
69       *            クラス
70       * @param arg
71       *            チェック対象オブジェクト
72       */
73      public static void assertInstanceOf(final String name,
74              final Class<?> clazz, final Object arg) {
75          if (!clazz.isAssignableFrom(arg.getClass())) {
76              throw new SIllegalArgumentException(
77                      UrumaMessageCodes.TYPE_MISS_MATCH, new Object[] { name,
78                              clazz.getClass().getName() });
79          }
80      }
81  }