View Javadoc
1   package org.apache.commons.beanutils2;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static java.lang.String.format;
23  
24  /**
25   * Code partially extracted from Google Collections
26   */
27  final class Assertions
28  {
29  
30      /**
31       * Hidden constructor, this class cannot be instantiated directly.
32       */
33      private Assertions()
34      {
35          // do nothing
36      }
37  
38      /**
39       * Ensures the truth of an expression involving one or more parameters to the
40       * calling method.
41       *
42       * @param expression a boolean expression
43       * @param errorMessageTemplate a template for the exception message should the
44       *     check fail. The message is formed by replacing each {@code %s}
45       *     placeholder in the template with an argument. These are matched by
46       *     position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc.
47       *     Unmatched arguments will be appended to the formatted message in square
48       *     braces. Unmatched placeholders will be left as-is.
49       * @param errorMessageArgs the arguments to be substituted into the message
50       *     template. Arguments are converted to strings using
51       *     {@link String#valueOf(Object)}.
52       * @throws IllegalArgumentException if {@code expression} is false
53       * @throws NullPointerException if the check fails and either {@code
54       *     errorMessageTemplate} or {@code errorMessageArgs} is null (don't let
55       *     this happen)
56       */
57      public static void checkArgument( boolean expression, String errorMessageTemplate, Object... errorMessageArgs )
58      {
59          if ( !expression )
60          {
61              throw new IllegalArgumentException( format( errorMessageTemplate, errorMessageArgs ) );
62          }
63      }
64  
65      /**
66       * Ensures the truth of an expression involving the state of the calling
67       * instance, but not involving any parameters to the calling method.
68       *
69       * @param expression a boolean expression
70       * @param errorMessageTemplate a template for the exception message should the
71       *     check fail. The message is formed by replacing each {@code %s}
72       *     placeholder in the template with an argument. These are matched by
73       *     position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc.
74       *     Unmatched arguments will be appended to the formatted message in square
75       *     braces. Unmatched placeholders will be left as-is.
76       * @param errorMessageArgs the arguments to be substituted into the message template.
77       * @throws IllegalStateException if {@code expression} is false
78       * @throws NullPointerException if the check fails and either {@code
79       *     errorMessageTemplate} or {@code errorMessageArgs} is null (don't let
80       *     this happen)
81       */
82      public static void checkState( boolean expression, String errorMessageTemplate, Object... errorMessageArgs )
83      {
84          if ( !expression )
85          {
86              throw new IllegalStateException( format( errorMessageTemplate, errorMessageArgs ) );
87          }
88      }
89  
90      /**
91       * Ensures that an object reference passed as a parameter to the calling
92       * method is not null.
93       *
94       * @param reference an object reference
95       * @param errorMessageTemplate a template for the exception message should the
96       *     check fail. The message is formed by replacing each {@code %s}
97       *     placeholder in the template with an argument. These are matched by
98       *     position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc.
99       *     Unmatched arguments will be appended to the formatted message in square
100      *     braces. Unmatched placeholders will be left as-is.
101      * @param errorMessageArgs the arguments to be substituted into the message
102      *     template. Arguments are converted to strings using
103      *     {@link String#valueOf(Object)}.
104      * @return the non-null reference that was validated
105      * @throws NullPointerException if {@code reference} is null
106      */
107     public static <T> T checkNotNull( T reference, String errorMessageTemplate, Object... errorMessageArgs )
108     {
109         if ( reference == null )
110         {
111             // If either of these parameters is null, the right thing happens anyway
112             throw new NullPointerException( format( errorMessageTemplate, errorMessageArgs ) );
113         }
114         return reference;
115     }
116 
117     /**
118      * Ensures that none of the references in the array passed as a parameter to the
119      * calling method is null. Note that an array of length 0 will pass the check.
120      *
121      * @param references an array of object reference. Must not be {@code null}!
122      * @return the non-null reference that was validated
123      * @throws NullPointerException
124      *             if one of the contained references is null or if {@code references} itself
125      *             is null (don't let this happen)
126      */
127     public static <T> T[] checkNoneIsNull( T[] references )
128     {
129         for ( int i = 0; i < references.length; i++ )
130         {
131             checkNotNull( references[i], "Reference at index %s/%s is null!", i, references.length );
132         }
133         return references;
134     }
135 
136 }