Save This Page
Home » tapestry-src-5.0.19 » org.apache.tapestry5.ioc.internal.util » [javadoc | source]
    1   // Copyright 2006 The Apache Software Foundation
    2   //
    3   // Licensed under the Apache License, Version 2.0 (the "License");
    4   // you may not use this file except in compliance with the License.
    5   // You may obtain a copy of the License at
    6   //
    7   //     http://www.apache.org/licenses/LICENSE-2.0
    8   //
    9   // Unless required by applicable law or agreed to in writing, software
   10   // distributed under the License is distributed on an "AS IS" BASIS,
   11   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12   // See the License for the specific language governing permissions and
   13   // limitations under the License.
   14   
   15   package org.apache.tapestry5.ioc.internal.util;
   16   
   17   /**
   18    * Static utility methods for defensive programming.
   19    */
   20   public final class Defense
   21   {
   22       private Defense()
   23       {
   24       }
   25   
   26       /**
   27        * Checks that a method parameter value is not null, and returns it.
   28        *
   29        * @param <T>           the value type
   30        * @param value         the value (which is checked to ensure non-nullness)
   31        * @param parameterName the name of the parameter, used for exception messages
   32        * @return the value
   33        * @throws IllegalArgumentException if the value is null
   34        */
   35       public static <T> T notNull(T value, String parameterName)
   36       {
   37           if (value == null) throw new IllegalArgumentException(UtilMessages.parameterWasNull(parameterName));
   38   
   39           return value;
   40       }
   41   
   42       /**
   43        * Checks that a parameter value is not null and not empty.
   44        *
   45        * @param value         value to check (which is returned)
   46        * @param parameterName the name of the parameter, used for exception messages
   47        * @return the value, trimmed, if non-blank
   48        * @throws IllegalArgumentException if the value is null or empty
   49        */
   50       public static String notBlank(String value, String parameterName)
   51       {
   52           if (value != null)
   53           {
   54               String trimmedValue = value.trim();
   55   
   56               if (!trimmedValue.equals("")) return trimmedValue;
   57           }
   58   
   59           throw new IllegalArgumentException(UtilMessages.parameterWasBlank(parameterName));
   60       }
   61   
   62       /**
   63        * Checks that the provided value is not null, and may be cast to the desired type.
   64        *
   65        * @param <T>
   66        * @param parameterValue
   67        * @param type
   68        * @param parameterName
   69        * @return the casted value
   70        * @throws IllegalArgumentException if the value is null, or is not assignable to the indicated type
   71        */
   72       public static <T> T cast(Object parameterValue, Class<T> type, String parameterName)
   73       {
   74           notNull(parameterValue, parameterName);
   75   
   76           if (!type.isInstance(parameterValue))
   77               throw new IllegalArgumentException(UtilMessages.badCast(parameterName, parameterValue, type));
   78   
   79           return type.cast(parameterValue);
   80       }
   81   }

Save This Page
Home » tapestry-src-5.0.19 » org.apache.tapestry5.ioc.internal.util » [javadoc | source]