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.hc.core5.util;
29  
30  import java.util.Collection;
31  
32  import org.apache.hc.core5.http.EntityDetails;
33  
34  public class Args {
35  
36      public static void check(final boolean expression, final String message) {
37          if (!expression) {
38              throw new IllegalArgumentException(message);
39          }
40      }
41  
42      public static void check(final boolean expression, final String message, final Object... args) {
43          if (!expression) {
44              throw new IllegalArgumentException(String.format(message, args));
45          }
46      }
47  
48      public static void check(final boolean expression, final String message, final Object arg) {
49          if (!expression) {
50              throw new IllegalArgumentException(String.format(message, arg));
51          }
52      }
53  
54      public static long checkContentLength(final EntityDetails entityDetails) {
55          // -1 is a special value
56          // 0 is allowed as well
57          return checkRange(entityDetails.getContentLength(), -1, Integer.MAX_VALUE,
58                  "HTTP entity too large to be buffered in memory)");
59      }
60  
61      public static int checkRange(final int value, final int lowInclusive, final int highInclusive,
62              final String message) {
63          if (value < lowInclusive || value > highInclusive) {
64              throw new IllegalArgumentException(String.format("%s: %,d is out of range [%,d, %,d]", message,
65                      Integer.valueOf(value), Integer.valueOf(lowInclusive), Integer.valueOf(highInclusive)));
66          }
67          return value;
68      }
69  
70      public static long checkRange(final long value, final long lowInclusive, final long highInclusive,
71              final String message) {
72          if (value < lowInclusive || value > highInclusive) {
73              throw new IllegalArgumentException(String.format("%s: %,d is out of range [%,d, %,d]", message,
74                      Long.valueOf(value), Long.valueOf(lowInclusive), Long.valueOf(highInclusive)));
75          }
76          return value;
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 <T extends CharSequence> T notBlank(final T argument, final String name) {
93          if (argument == null) {
94              throw new IllegalArgumentException(name + " may not be null");
95          }
96          if (TextUtils.isBlank(argument)) {
97              throw new IllegalArgumentException(name + " may not be blank");
98          }
99          return argument;
100     }
101 
102     public static <T extends CharSequence> T notEmpty(final T argument, final String name) {
103         if (argument == null) {
104             throw new IllegalArgumentException(name + " may not be null");
105         }
106         if (TextUtils.isEmpty(argument)) {
107             throw new IllegalArgumentException(name + " may not be empty");
108         }
109         return argument;
110     }
111 
112     public static <E, T extends Collection<E>> T notEmpty(final T argument, final String name) {
113         if (argument == null) {
114             throw new IllegalArgumentException(name + " may not be null");
115         }
116         if (argument.isEmpty()) {
117             throw new IllegalArgumentException(name + " may not be empty");
118         }
119         return argument;
120     }
121 
122     public static int notNegative(final int n, final String name) {
123         if (n < 0) {
124             throw new IllegalArgumentException(name + " may not be negative");
125         }
126         return n;
127     }
128 
129     public static long notNegative(final long n, final String name) {
130         if (n < 0) {
131             throw new IllegalArgumentException(name + " may not be negative");
132         }
133         return n;
134     }
135 
136     public static <T> T notNull(final T argument, final String name) {
137         if (argument == null) {
138             throw new IllegalArgumentException(name + " may not be null");
139         }
140         return argument;
141     }
142 
143     public static int positive(final int n, final String name) {
144         if (n <= 0) {
145             throw new IllegalArgumentException(name + " may not be negative or zero");
146         }
147         return n;
148     }
149 
150     public static long positive(final long n, final String name) {
151         if (n <= 0) {
152             throw new IllegalArgumentException(name + " may not be negative or zero");
153         }
154         return n;
155     }
156 
157     private Args() {
158         // Do not allow utility class to be instantiated.
159     }
160 
161 }