View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.util;
29  
30  import java.util.Collection;
31  
32  public class Args {
33  
34      public static void check(final boolean expression, final String message) {
35          if (!expression) {
36              throw new IllegalArgumentException(message);
37          }
38      }
39  
40      public static void check(final boolean expression, final String message, final Object... args) {
41          if (!expression) {
42              throw new IllegalArgumentException(String.format(message, args));
43          }
44      }
45  
46      public static void check(final boolean expression, final String message, final Object arg) {
47          if (!expression) {
48              throw new IllegalArgumentException(String.format(message, arg));
49          }
50      }
51  
52      public static <T> T notNull(final T argument, final String name) {
53          if (argument == null) {
54              throw new IllegalArgumentException(name + " may not be null");
55          }
56          return argument;
57      }
58  
59      public static <T extends CharSequence> T notEmpty(final T argument, final String name) {
60          if (argument == null) {
61              throw new IllegalArgumentException(name + " may not be null");
62          }
63          if (TextUtils.isEmpty(argument)) {
64              throw new IllegalArgumentException(name + " may not be empty");
65          }
66          return argument;
67      }
68  
69      public static <T extends CharSequence> T notBlank(final T argument, final String name) {
70          if (argument == null) {
71              throw new IllegalArgumentException(name + " may not be null");
72          }
73          if (TextUtils.isBlank(argument)) {
74              throw new IllegalArgumentException(name + " may not be blank");
75          }
76          return argument;
77      }
78  
79      public static <T extends CharSequence> T containsNoBlanks(final T argument, final String name) {
80          if (argument == null) {
81              throw new IllegalArgumentException(name + " may not be null");
82          }
83          if (argument.length() == 0) {
84              throw new IllegalArgumentException(name + " may not be empty");
85          }
86          if (TextUtils.containsBlanks(argument)) {
87              throw new IllegalArgumentException(name + " may not contain blanks");
88          }
89          return argument;
90      }
91  
92      public static <E, T extends Collection<E>> T notEmpty(final T argument, final String name) {
93          if (argument == null) {
94              throw new IllegalArgumentException(name + " may not be null");
95          }
96          if (argument.isEmpty()) {
97              throw new IllegalArgumentException(name + " may not be empty");
98          }
99          return argument;
100     }
101 
102     public static int positive(final int n, final String name) {
103         if (n <= 0) {
104             throw new IllegalArgumentException(name + " may not be negative or zero");
105         }
106         return n;
107     }
108 
109     public static long positive(final long n, final String name) {
110         if (n <= 0) {
111             throw new IllegalArgumentException(name + " may not be negative or zero");
112         }
113         return n;
114     }
115 
116     public static int notNegative(final int n, final String name) {
117         if (n < 0) {
118             throw new IllegalArgumentException(name + " may not be negative");
119         }
120         return n;
121     }
122 
123     public static long notNegative(final long n, final String name) {
124         if (n < 0) {
125             throw new IllegalArgumentException(name + " may not be negative");
126         }
127         return n;
128     }
129 
130 }