View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.util;
21  
22  /**
23   * Provides methods to check preconditions.
24   * 
25   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
26   */
27  public class Assert {
28      /**
29       * This class is not intended to be instantiated! It has no state and
30       * provides only static methods
31       */
32      private Assert() {
33      }
34  
35      /**
36       * Returns <code>value</code> if it is not <code>null</code>. Otherwise a
37       * {@link IllegalArgumentException} will be thrown, the given
38       * <code>parameterName</code> is included in the Message. Eg. <br>
39       * <br>
40       * <b>Example:</b>
41       * If <code>null</code> will be passed, the message of the exception is <i>"Parameter >value< must not be null !"</i>
42       * <pre>
43       * public void myMethod(Integer value){
44       *     <b>assertNotNull(value,"value");</b>
45       *     ...
46       * }
47       * </pre>
48       * @param <T>
49       *            Type of the value
50       * @param value
51       *            the value to check
52       * @param parameterName
53       *            name of the parameter to be included in a possible
54       *            {@link IllegalArgumentException}
55       * @return parameter <code>value</code>
56       * @exception IllegalArgumentException if <code>value==null</code>
57       */
58      public static <T> T assertNotNull(T value, String parameterName) {
59          if (parameterName == null) {
60              throw new IllegalArgumentException("You must provide a parameter name!");
61          }
62  
63          if (value == null) {
64              throw new IllegalArgumentException("Parameter >" + parameterName + "< must not be null!");
65          }
66  
67          return value;
68      }
69  }