View Javadoc

1   /* Copyright (c) 2008 Sascha Kohlmann
2    *
3    * This program is free software: you can redistribute it and/or modify
4    * it under the terms of the GNU Affero General Public License as published by
5    * the Free Software Foundation, either version 3 of the License, or
6    * (at your option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Affero General Public License for more details.
12   *
13   * You should have received a copy of the GNU Affero General Public License
14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15   */
16  package net.sf.eos.util;
17  
18  /**
19   * Provides static methods to simpler condition handling. This implies less
20   * code.
21   * <p><strong>Example</strong></p>
22   * <pre>
23   *     if (reference == null) {
24   *         throw new IllegalArgumentException("reference is null");
25   *     }
26   *     this.value = reference;
27   * </pre>
28   * <p>will change to a simple single line:</p>
29   * <pre>
30   *     this.value = <em>checkArgumentNotNull</em>(reference, "reference is null");
31   * </pre>
32   * @author Sascha Kohlmann
33   */
34  public final class Conditions {
35  
36      private Conditions() { }
37  
38      /**
39       * Checks that the given reference is {@code null} and throws an
40       * {@code IllegalArgumentException} if so.
41       *
42       * @param ref an object reference
43       * @return the non-{@code null} reference
44       * @throws IllegalArgumentException if {@code ref} is {@code null}
45       */
46      public static <T> T checkArgumentNotNull(final T ref) {
47          if (ref == null) {
48               throw new IllegalArgumentException();
49          }
50          return ref;
51      }
52  
53      /**
54       * Checks that the given reference is {@code null} and throws an
55       * {@code IllegalArgumentException} if so.
56       *
57       * @param ref an object reference
58       * @return the non-{@code null} reference
59       * @throws IllegalArgumentException if {@code ref} is {@code null}
60       */
61      public static <T> T checkArgumentNotNull(final T ref,
62                                               final Object errorMessage) {
63          if (ref == null) {
64               throw new IllegalArgumentException("" + errorMessage);
65          }
66          return ref;
67      }
68  
69      /**
70       * Checks the expression of an argument and throws an
71       * {@code IllegalArgumentException} if the expression is {@code false}.
72       *
73       * @param expr the expression to test
74       * @throws IllegalArgumentException if {@code expr} is {@code false}
75       */
76      public static void checkArgument(final boolean expr) {
77          if (! expr) {
78              throw new IllegalArgumentException();
79          }
80      }
81  
82      /**
83       * Checks the expression of an argument and throws an
84       * {@code IllegalArgumentException} if the expression is {@code false}.
85       *
86       * @param expr the expression to test
87       * @throws IllegalArgumentException if {@code expr} is {@code false}
88       */
89      public static void checkArgument(final boolean expr,
90                                       final Object errorMessage) {
91          if (! expr) {
92              throw new IllegalArgumentException("" + errorMessage);
93          }
94      }
95  
96      /**
97       * Checks the expression of an argument and throws an
98       * {@code IllegalStateException} if the expression is {@code false}.
99       *
100      * @param expr the expression to test
101      * @throws IllegalStateException if {@code expr} is {@code false}
102      */
103     public static void checkState(final boolean expr) {
104         if (! expr) {
105             throw new IllegalStateException();
106         }
107     }
108 
109     /**
110      * Checks the expression of an argument and throws an
111      * {@code IllegalStateException} if the expression is {@code false}.
112      *
113      * @param expr the expression to test
114      * @throws IllegalStateException if {@code expr} is {@code false}
115      */
116     public static void checkState(final boolean expr,
117                                   final Object errorMessage) {
118         if (! expr) {
119             throw new IllegalStateException("" + errorMessage);
120         }
121     }
122 }