View Javadoc

1   /*
2    * Copyright 2011-2013 smartics, Kronseder & Reiner GmbH
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  package de.smartics.util.lang;
17  
18  import org.apache.commons.lang.StringUtils;
19  
20  /**
21   * Utility class to check arguments.
22   *
23   * @deprecated Due to a breaking change a new class called {@link Arg} has been
24   *             designed to be used from now on. It has the same methods as this
25   *             one, but it returns the value passed in to check. This allows to
26   *             do the check and an initialization (e.g. in a constructor) in one
27   *             line. It also allows to check arguments if another constructor is
28   *             called (such as in <code>this()</code> and <code>super()</code>).
29   *             This class is planned to be removed with the release of version
30   *             2.0.
31   */
32  @Deprecated
33  public final class Arguments
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    // --- members --------------------------------------------------------------
40  
41    // ****************************** Initializer *******************************
42  
43    // ****************************** Constructors ******************************
44  
45    /**
46     * Utility class pattern.
47     */
48    private Arguments()
49    {
50    }
51  
52    // ****************************** Inner Classes *****************************
53  
54    // ********************************* Methods ********************************
55  
56    // --- init -----------------------------------------------------------------
57  
58    // --- get&set --------------------------------------------------------------
59  
60    // --- business -------------------------------------------------------------
61  
62    /**
63     * Checks if {@code value} is <code>null</code>.
64     *
65     * @param name the name of the argument of error reporting. Not used if no
66     *          exception is thrown. May be <code>null</code>, although not
67     *          recommended.
68     * @param value the value of the argument to check to be not <code>null</code>
69     *          .
70     * @throws NullPointerException if {@code value} is <code>null</code>.
71     */
72    public static void checkNotNull(final String name, final Object value)
73      throws NullPointerException
74    {
75      checkNotNull(name, value, null);
76    }
77  
78    /**
79     * Checks if {@code value} is <code>null</code> providing an additional
80     * message.
81     *
82     * @param name the name of the argument of error reporting. Not used if no
83     *          exception is thrown. May be <code>null</code>, although not
84     *          recommended.
85     * @param value the value of the argument to check to be not <code>null</code>
86     *          .
87     * @param message the message to pass to the exception as additional
88     *          information to the standard message being generated.
89     * @throws NullPointerException if {@code value} is <code>null</code>.
90     */
91    public static void checkNotNull(final String name, final Object value,
92        final String message) throws NullPointerException
93    {
94      if (value == null)
95      {
96        throw new NullArgumentException(name, message);
97      }
98    }
99  
100   /**
101    * Checks if {@code value} is blank.
102    *
103    * @param name the name of the argument of error reporting. Not used if no
104    *          exception is thrown. May be <code>null</code>, although not
105    *          recommended.
106    * @param value the value of the argument to check to be not blank.
107    * @throws BlankArgumentException if {@code value} is blank.
108    */
109   public static void checkNotBlank(final String name, final String value)
110     throws BlankArgumentException
111   {
112     checkNotBlank(name, value, null);
113   }
114 
115   /**
116    * Checks if {@code value} is blank providing an additional message.
117    *
118    * @param name the name of the argument of error reporting. Not used if no
119    *          exception is thrown. May be <code>null</code>, although not
120    *          recommended.
121    * @param value the value of the argument to check to be not blank.
122    * @param message the message to pass to the exception as additional
123    *          information to the standard message being generated.
124    * @throws BlankArgumentException if {@code value} is blank.
125    */
126   public static void checkNotBlank(final String name, final String value,
127       final String message) throws BlankArgumentException
128   {
129     if (StringUtils.isBlank(value))
130     {
131       throw new BlankArgumentException(name, message);
132     }
133   }
134 
135   /**
136    * Checks if {@code value} is blank except <code>null</code>.
137    *
138    * @param name the name of the argument of error reporting. Not used if no
139    *          exception is thrown. May be <code>null</code>, although not
140    *          recommended.
141    * @param value the value of the argument to check to be not blank but may be
142    *          <code>null</code>.
143    * @throws BlankExceptNullArgumentException if {@code value} is blank and not
144    *           <code>null</code>.
145    */
146   public static void checkNotBlankExceptNull(final String name,
147       final String value) throws BlankExceptNullArgumentException
148   {
149     checkNotBlankExceptNull(name, value, null);
150   }
151 
152   /**
153    * Checks if {@code value} is blank except <code>null</code> providing an
154    * additional message.
155    *
156    * @param name the name of the argument of error reporting. Not used if no
157    *          exception is thrown. May be <code>null</code>, although not
158    *          recommended.
159    * @param value the value of the argument to check to be not blank but may be
160    *          <code>null</code>.
161    * @param message the message to pass to the exception as additional
162    *          information to the standard message being generated.
163    * @throws BlankExceptNullArgumentException if {@code value} is blank and not
164    *           <code>null</code>.
165    */
166   public static void checkNotBlankExceptNull(final String name,
167       final String value, final String message)
168     throws BlankExceptNullArgumentException
169   {
170     if (value != null && StringUtils.isBlank(value))
171     {
172       throw new BlankArgumentException(name, message);
173     }
174   }
175 
176   // --- object basics --------------------------------------------------------
177 
178 }