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 (TextUtils.containsBlanks(argument)) {
84              throw new IllegalArgumentException(name + " may not contain blanks");
85          }
86          return argument;
87      }
88  
89      public static <E, T extends Collection<E>> T notEmpty(final T argument, final String name) {
90          if (argument == null) {
91              throw new IllegalArgumentException(name + " may not be null");
92          }
93          if (argument.isEmpty()) {
94              throw new IllegalArgumentException(name + " may not be empty");
95          }
96          return argument;
97      }
98  
99      public static int positive(final int n, final String name) {
100         if (n <= 0) {
101             throw new IllegalArgumentException(name + " may not be negative or zero");
102         }
103         return n;
104     }
105 
106     public static long positive(final long n, final String name) {
107         if (n <= 0) {
108             throw new IllegalArgumentException(name + " may not be negative or zero");
109         }
110         return n;
111     }
112 
113     public static int notNegative(final int n, final String name) {
114         if (n < 0) {
115             throw new IllegalArgumentException(name + " may not be negative");
116         }
117         return n;
118     }
119 
120     public static long notNegative(final long n, final String name) {
121         if (n < 0) {
122             throw new IllegalArgumentException(name + " may not be negative");
123         }
124         return n;
125     }
126 
127 }