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          // but never more than Integer.MAX_VALUE.
58          return checkRange(entityDetails.getContentLength(), -1, Integer.MAX_VALUE,
59                          "HTTP entity too large to be buffered in memory)");
60      }
61  
62      public static int checkRange(final int value, final int lowInclusive, final int highInclusive,
63                      final String message) {
64          if (value < lowInclusive || value > highInclusive) {
65              throw illegalArgumentException("%s: %d is out of range [%d, %d]", message, Integer.valueOf(value),
66                              Integer.valueOf(lowInclusive), Integer.valueOf(highInclusive));
67          }
68          return value;
69      }
70  
71      public static long checkRange(final long value, final long lowInclusive, final long highInclusive,
72                      final String message) {
73          if (value < lowInclusive || value > highInclusive) {
74              throw illegalArgumentException("%s: %d is out of range [%d, %d]", message, Long.valueOf(value),
75                              Long.valueOf(lowInclusive), Long.valueOf(highInclusive));
76          }
77          return value;
78      }
79  
80      public static <T extends CharSequence> T containsNoBlanks(final T argument, final String name) {
81          if (argument == null) {
82              throw NullPointerException(name);
83          }
84          if (argument.length() == 0) {
85              throw illegalArgumentExceptionNotEmpty(name);
86          }
87          if (TextUtils.containsBlanks(argument)) {
88              throw new IllegalArgumentException(name + " must not contain blanks");
89          }
90          return argument;
91      }
92  
93      private static IllegalArgumentException illegalArgumentException(final String format, final Object... args) {
94          return new IllegalArgumentException(String.format(format, args));
95      }
96  
97      private static IllegalArgumentException illegalArgumentExceptionNotEmpty(final String name) {
98          return new IllegalArgumentException(name + " must not be empty");
99      }
100 
101     private static NullPointerException NullPointerException(final String name) {
102         return new NullPointerException(name + " must not be null");
103     }
104 
105     public static <T extends CharSequence> T notBlank(final T argument, final String name) {
106         if (argument == null) {
107             throw NullPointerException(name);
108         }
109         if (TextUtils.isBlank(argument)) {
110             throw new IllegalArgumentException(name + " must not be blank");
111         }
112         return argument;
113     }
114 
115     public static <T extends CharSequence> T notEmpty(final T argument, final String name) {
116         if (argument == null) {
117             throw NullPointerException(name);
118         }
119         if (TextUtils.isEmpty(argument)) {
120             throw illegalArgumentExceptionNotEmpty(name);
121         }
122         return argument;
123     }
124 
125     public static <E, T extends Collection<E>> T notEmpty(final T argument, final String name) {
126         if (argument == null) {
127             throw NullPointerException(name);
128         }
129         if (argument.isEmpty()) {
130             throw illegalArgumentExceptionNotEmpty(name);
131         }
132         return argument;
133     }
134 
135     public static int notNegative(final int n, final String name) {
136         if (n < 0) {
137             throw illegalArgumentException("%s must not be negative: %d", name, n);
138         }
139         return n;
140     }
141 
142     public static long notNegative(final long n, final String name) {
143         if (n < 0) {
144             throw illegalArgumentException("%s must not be negative: %d", name, n);
145         }
146         return n;
147     }
148 
149     public static <T> T notNull(final T argument, final String name) {
150         if (argument == null) {
151             throw NullPointerException(name);
152         }
153         return argument;
154     }
155 
156     public static int positive(final int n, final String name) {
157         if (n <= 0) {
158             throw illegalArgumentException("%s must not be negative or zero: %d", name, n);
159         }
160         return n;
161     }
162 
163     public static long positive(final long n, final String name) {
164         if (n <= 0) {
165             throw illegalArgumentException("%s must not be negative or zero: %d", name, n);
166         }
167         return n;
168     }
169 
170     public static <T extends TimeValue> T positive(final T timeValue, final String name) {
171         positive(timeValue.getDuration(), name);
172         return timeValue;
173     }
174 
175     private Args() {
176         // Do not allow utility class to be instantiated.
177     }
178 
179 }