View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.util;
18  
19  import java.util.Collection;
20  import java.util.Map;
21  
22  /**
23   * Utility class providing common validation logic.
24   */
25  public final class Assert {
26      private Assert() {
27      }
28  
29      /**
30       * Checks if an object has empty semantics. The following scenarios are considered empty:
31       * <ul>
32       * <li>{@code null}</li>
33       * <li>empty {@link CharSequence}</li>
34       * <li>empty array</li>
35       * <li>empty {@link Iterable}</li>
36       * <li>empty {@link Map}</li>
37       * </ul>
38       *
39       * @param o value to check for emptiness
40       * @return true if the value is empty, false otherwise
41       * @since 2.8
42       */
43      public static boolean isEmpty(final Object o) {
44          if (o == null) {
45              return true;
46          }
47          if (o instanceof CharSequence) {
48              return ((CharSequence) o).length() == 0;
49          }
50          if (o.getClass().isArray()) {
51              return ((Object[]) o).length == 0;
52          }
53          if (o instanceof Collection) {
54              return ((Collection<?>) o).isEmpty();
55          }
56          if (o instanceof Map) {
57              return ((Map<?, ?>) o).isEmpty();
58          }
59          return false;
60      }
61  
62      /**
63       * Opposite of {@link #isEmpty(Object)}.
64       *
65       * @param o value to check for non-emptiness
66       * @return true if the value is non-empty, false otherwise
67       * @since 2.8
68       */
69      public static boolean isNonEmpty(final Object o) {
70          return !isEmpty(o);
71      }
72  
73      /**
74       * Checks a value for emptiness and throws an IllegalArgumentException if it's empty.
75       *
76       * @param value value to check for emptiness
77       * @param <T>   type of value
78       * @return the provided value if non-empty
79       * @since 2.8
80       */
81      public static <T> T requireNonEmpty(final T value) {
82          return requireNonEmpty(value, "");
83      }
84  
85      /**
86       * Checks a value for emptiness and throws an IllegalArgumentException if it's empty.
87       *
88       * @param value   value to check for emptiness
89       * @param message message to provide in exception
90       * @param <T>     type of value
91       * @return the provided value if non-empty
92       * @since 2.8
93       */
94      public static <T> T requireNonEmpty(final T value, final String message) {
95          if (isEmpty(value)) {
96              throw new IllegalArgumentException(message);
97          }
98          return value;
99      }
100 
101     public static int valueIsAtLeast(final int value, final int minValue) {
102         if (value < minValue) {
103             throw new IllegalArgumentException("Value should be at least " + minValue + " but was " + value);
104         }
105         return value;
106     }
107 }