001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.util; 018 019import java.util.Collection; 020import java.util.Map; 021 022/** 023 * Utility class providing common validation logic. 024 */ 025public final class Assert { 026 private Assert() { 027 } 028 029 /** 030 * Checks if an object has empty semantics. The following scenarios are considered empty: 031 * <ul> 032 * <li>{@code null}</li> 033 * <li>empty {@link CharSequence}</li> 034 * <li>empty array</li> 035 * <li>empty {@link Iterable}</li> 036 * <li>empty {@link Map}</li> 037 * </ul> 038 * 039 * @param o value to check for emptiness 040 * @return true if the value is empty, false otherwise 041 * @since 2.8 042 */ 043 public static boolean isEmpty(final Object o) { 044 if (o == null) { 045 return true; 046 } 047 if (o instanceof CharSequence) { 048 return ((CharSequence) o).length() == 0; 049 } 050 if (o.getClass().isArray()) { 051 return ((Object[]) o).length == 0; 052 } 053 if (o instanceof Collection) { 054 return ((Collection<?>) o).isEmpty(); 055 } 056 if (o instanceof Map) { 057 return ((Map<?, ?>) o).isEmpty(); 058 } 059 return false; 060 } 061 062 /** 063 * Opposite of {@link #isEmpty(Object)}. 064 * 065 * @param o value to check for non-emptiness 066 * @return true if the value is non-empty, false otherwise 067 * @since 2.8 068 */ 069 public static boolean isNonEmpty(final Object o) { 070 return !isEmpty(o); 071 } 072 073 /** 074 * Checks a value for emptiness and throws an IllegalArgumentException if it's empty. 075 * 076 * @param value value to check for emptiness 077 * @param <T> type of value 078 * @return the provided value if non-empty 079 * @since 2.8 080 */ 081 public static <T> T requireNonEmpty(final T value) { 082 return requireNonEmpty(value, ""); 083 } 084 085 /** 086 * Checks a value for emptiness and throws an IllegalArgumentException if it's empty. 087 * 088 * @param value value to check for emptiness 089 * @param message message to provide in exception 090 * @param <T> type of value 091 * @return the provided value if non-empty 092 * @since 2.8 093 */ 094 public static <T> T requireNonEmpty(final T value, final String message) { 095 if (isEmpty(value)) { 096 throw new IllegalArgumentException(message); 097 } 098 return value; 099 } 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}