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 <T> T notNull(final T argument, final String name) {
47          if (argument == null) {
48              throw new IllegalArgumentException(name + " may not be null");
49          }
50          return argument;
51      }
52  
53      public static <T extends CharSequence> T notEmpty(final T argument, final String name) {
54          if (argument == null) {
55              throw new IllegalArgumentException(name + " may not be null");
56          }
57          if (TextUtils.isEmpty(argument)) {
58              throw new IllegalArgumentException(name + " may not be empty");
59          }
60          return argument;
61      }
62  
63      public static <T extends CharSequence> T notBlank(final T argument, final String name) {
64          if (argument == null) {
65              throw new IllegalArgumentException(name + " may not be null");
66          }
67          if (TextUtils.isBlank(argument)) {
68              throw new IllegalArgumentException(name + " may not be blank");
69          }
70          return argument;
71      }
72  
73      public static <E, T extends Collection<E>> T notEmpty(final T argument, final String name) {
74          if (argument == null) {
75              throw new IllegalArgumentException(name + " may not be null");
76          }
77          if (argument.isEmpty()) {
78              throw new IllegalArgumentException(name + " may not be empty");
79          }
80          return argument;
81      }
82  
83      public static int positive(final int n, final String name) {
84          if (n <= 0) {
85              throw new IllegalArgumentException(name + " may not be negative or zero");
86          }
87          return n;
88      }
89  
90      public static long positive(final long n, final String name) {
91          if (n <= 0) {
92              throw new IllegalArgumentException(name + " may not be negative or zero");
93          }
94          return n;
95      }
96  
97      public static int notNegative(final int n, final String name) {
98          if (n < 0) {
99              throw new IllegalArgumentException(name + " may not be negative");
100         }
101         return n;
102     }
103 
104     public static long notNegative(final long n, final String name) {
105         if (n < 0) {
106             throw new IllegalArgumentException(name + " may not be negative");
107         }
108         return n;
109     }
110 
111 }