Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2015, Haiyang Li.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.landawn.abacus.util;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.AbstractMap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.RandomAccess;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.UnaryOperator;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.Immutable;
import com.landawn.abacus.annotation.Internal;
import com.landawn.abacus.annotation.MayReturnNull;
import com.landawn.abacus.annotation.NotNull;
import com.landawn.abacus.annotation.NullSafe;
import com.landawn.abacus.annotation.SuppressFBWarnings;
import com.landawn.abacus.exception.TooManyElementsException;
import com.landawn.abacus.exception.UncheckedSQLException;
import com.landawn.abacus.parser.ParserUtil;
import com.landawn.abacus.parser.ParserUtil.BeanInfo;
import com.landawn.abacus.parser.ParserUtil.PropInfo;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.type.TypeFactory;
import com.landawn.abacus.util.Builder.ComparisonBuilder;
import com.landawn.abacus.util.Fn.BiPredicates;
import com.landawn.abacus.util.Fn.IntFunctions;
import com.landawn.abacus.util.Fn.Suppliers;
import com.landawn.abacus.util.u.Nullable;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.u.OptionalInt;
import com.landawn.abacus.util.function.ToFloatFunction;
/**
*
* Note: This class includes codes copied from Apache Commons Lang, Google Guava and other open source projects under the Apache License 2.0.
* The methods copied from other libraries/frameworks/projects may be modified in this class.
*
* Class {@code N} is a general java utility class. It provides the most daily used operations for Object/primitive types/String/Array/Collection/Map/Bean...:
*
*
*
* When to throw exception? It's designed to avoid throwing any unnecessary
* exception if the contract defined by method is not broken. For example, if
* user tries to reverse a {@code null} or empty String. The input String will be
* returned. But exception will be thrown if try to add element to a {@code null} Object array or collection.
*
*
* An empty String/Array/Collection/Map/Iterator/Iterable/InputStream/Reader will always be a preferred choice than a {@code null} for the return value of a method.
*
* There are only {@code fromIndex/startIndex} and {toIndex/endIndex} parameters in the methods defined in class {@code CommonUtil/N}, no {@code offset/count} parameters.
*
*
* @see com.landawn.abacus.util.Comparators
* @see com.landawn.abacus.util.Fn
* @see com.landawn.abacus.util.Fn.Fnn
* @see com.landawn.abacus.util.Array
* @see com.landawn.abacus.util.CommonUtil
* @see com.landawn.abacus.util.Iterables
* @see com.landawn.abacus.util.Iterators
* @see com.landawn.abacus.util.Index
* @see com.landawn.abacus.util.Median
* @see com.landawn.abacus.util.Maps
* @see com.landawn.abacus.util.Strings
* @see com.landawn.abacus.util.Numbers
* @see com.landawn.abacus.util.IOUtil
* @see java.lang.reflect.Array
* @see java.util.Arrays
* @see java.util.Collections
*
* @version $Revision: 0.8 $ 07/03/10
*/
@SuppressWarnings({ "java:S1192", "java:S6539" })
sealed class CommonUtil permits N {
static final int BINARY_SEARCH_THRESHOLD = 64;
// ...
static final int REVERSE_THRESHOLD = 18;
static final int FILL_THRESHOLD = 25;
static final int REPLACE_ALL_THRESHOLD = 11;
static final int MIN_SIZE_FOR_COPY_ALL = 9;
static final int DEFAULT_SIZE_FOR_NEW_COLLECTION = 9;
// ...
static final Random RAND = new SecureRandom();
// ... it has to be big enough to make it's safety to add element to
// ArrayBlockingQueue.
@SuppressWarnings("deprecation")
static final int POOL_SIZE = InternalUtil.POOL_SIZE;
// ...
/**
* The index value when an element is not found in a list or array:
* {@code -1}. This value is returned by methods in this class and can also
* be used in comparisons with values returned by various method from
* {@link java.util.List} .
*/
public static final int INDEX_NOT_FOUND = -1;
/**
* An empty immutable/unmodifiable {@code boolean} array.
*/
public static final boolean[] EMPTY_BOOLEAN_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code char} array.
*/
public static final char[] EMPTY_CHAR_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code byte} array.
*/
public static final byte[] EMPTY_BYTE_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code short} array.
*/
public static final short[] EMPTY_SHORT_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code int} array.
*/
public static final int[] EMPTY_INT_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code long} array.
*/
public static final long[] EMPTY_LONG_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code float} array.
*/
public static final float[] EMPTY_FLOAT_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code double} array.
*/
public static final double[] EMPTY_DOUBLE_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code Boolean} array.
*/
public static final Boolean[] EMPTY_BOOLEAN_OBJ_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code Character} array.
*/
public static final Character[] EMPTY_CHAR_OBJ_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code Byte} array.
*/
public static final Byte[] EMPTY_BYTE_OBJ_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code Short} array.
*/
public static final Short[] EMPTY_SHORT_OBJ_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code Integer} array.
*/
public static final Integer[] EMPTY_INT_OBJ_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code Long} array.
*/
public static final Long[] EMPTY_LONG_OBJ_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code Float} array.
*/
public static final Float[] EMPTY_FLOAT_OBJ_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code Double} array.
*/
public static final Double[] EMPTY_DOUBLE_OBJ_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code BigInteger} array.
*/
public static final BigInteger[] EMPTY_BIG_INTEGER_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code BigDecimal} array.
*/
public static final BigDecimal[] EMPTY_BIG_DECIMAL_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code String} array.
*/
public static final String[] EMPTY_STRING_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code java.util.Date} array.
*/
public static final java.util.Date[] EMPTY_JU_DATE_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code java.sql.Date} array.
*/
public static final java.sql.Date[] EMPTY_DATE_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code Time} array.
*/
public static final java.sql.Time[] EMPTY_TIME_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code Timestamp} array.
*/
public static final java.sql.Timestamp[] EMPTY_TIMESTAMP_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code Calendar} array.
*/
@SuppressFBWarnings("STCAL_STATIC_CALENDAR_INSTANCE")
public static final Calendar[] EMPTY_CALENDAR_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code LocalDate} array.
*/
public static final LocalDate[] EMPTY_LOCAL_DATE_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code LocalTime} array.
*/
public static final LocalTime[] EMPTY_LOCAL_TIME_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code LocalDateTime} array.
*/
public static final LocalDateTime[] EMPTY_LOCAL_DATE_TIME_ARRAY = {};
/**
* An empty immutable/unmodifiable {@code Object} array.
*/
public static final Object[] EMPTY_OBJECT_ARRAY = {};
// /**
// * An empty immutable/unmodifiable {@code DataSet}.
// */
// public static final DataSet EMPTY_DATA_SET = RowDataSet.EMPTY_DATA_SET;
/**
* An empty immutable/unmodifiable {@code Class} array.
*/
static final Class>[] EMPTY_CLASS_ARRAY = new Class[0];
/**
* An empty immutable/unmodifiable {@code List}.
*/
@SuppressWarnings("rawtypes")
static final List EMPTY_LIST = Collections.emptyList();
/**
* An empty immutable/unmodifiable {@code Set}.
*/
@SuppressWarnings("rawtypes")
static final Set EMPTY_SET = Collections.emptySet();
/**
* An empty immutable/unmodifiable {@code SortedSet}.
*/
@SuppressWarnings("rawtypes")
static final SortedSet EMPTY_SORTED_SET = Collections.emptySortedSet();
/**
* An empty immutable/unmodifiable {@code NavigableSet}.
*/
@SuppressWarnings("rawtypes")
static final NavigableSet EMPTY_NAVIGABLE_SET = Collections.emptyNavigableSet();
/**
* An empty immutable/unmodifiable {@code Map}.
*/
@SuppressWarnings("rawtypes")
static final Map EMPTY_MAP = Collections.emptyMap();
/**
* An empty immutable/unmodifiable {@code SortedMap}.
*/
@SuppressWarnings("rawtypes")
static final SortedMap EMPTY_SORTED_MAP = Collections.emptySortedMap();
/**
* An empty immutable/unmodifiable {@code NavigableMap}.
*/
@SuppressWarnings("rawtypes")
static final NavigableMap EMPTY_NAVIGABLE_MAP = Collections.emptyNavigableMap();
/**
* An empty immutable/unmodifiable iterator.
*/
@SuppressWarnings("rawtypes")
static final Iterator EMPTY_ITERATOR = Collections.emptyIterator();
/**
* An empty immutable/unmodifiable {@code ListIterator}.
*/
@SuppressWarnings("rawtypes")
static final ListIterator EMPTY_LIST_ITERATOR = Collections.emptyListIterator();
@SuppressWarnings("rawtypes")
static final Comparator NULL_MIN_COMPARATOR = Comparators.NULL_FIRST_COMPARATOR;
@SuppressWarnings("rawtypes")
static final Comparator NULL_MAX_COMPARATOR = Comparators.NULL_LAST_COMPARATOR;
@SuppressWarnings("rawtypes")
static final Comparator NATURAL_COMPARATOR = Comparators.NATURAL_ORDER;
@SuppressWarnings("rawtypes")
static final Comparator REVERSED_COMPARATOR = Comparators.REVERSED_ORDER;
static final Comparator CHAR_COMPARATOR = Character::compare;
static final Comparator BYTE_COMPARATOR = Byte::compare;
static final Comparator SHORT_COMPARATOR = Short::compare;
static final Comparator INT_COMPARATOR = Integer::compare;
static final Comparator LONG_COMPARATOR = Long::compare;
static final Comparator FLOAT_COMPARATOR = Float::compare;
static final Comparator DOUBLE_COMPARATOR = Double::compare;
// ...
static final Object NULL_MASK = ClassUtil.createNullMask();
// ...
static final Map, Object> CLASS_EMPTY_ARRAY = new ConcurrentHashMap<>();
static {
CLASS_EMPTY_ARRAY.put(boolean.class, EMPTY_BOOLEAN_ARRAY);
CLASS_EMPTY_ARRAY.put(Boolean.class, EMPTY_BOOLEAN_OBJ_ARRAY);
CLASS_EMPTY_ARRAY.put(char.class, EMPTY_CHAR_ARRAY);
CLASS_EMPTY_ARRAY.put(Character.class, EMPTY_CHAR_OBJ_ARRAY);
CLASS_EMPTY_ARRAY.put(byte.class, EMPTY_BYTE_ARRAY);
CLASS_EMPTY_ARRAY.put(Byte.class, EMPTY_BYTE_OBJ_ARRAY);
CLASS_EMPTY_ARRAY.put(short.class, EMPTY_SHORT_ARRAY);
CLASS_EMPTY_ARRAY.put(Short.class, EMPTY_SHORT_OBJ_ARRAY);
CLASS_EMPTY_ARRAY.put(int.class, EMPTY_INT_ARRAY);
CLASS_EMPTY_ARRAY.put(Integer.class, EMPTY_INT_OBJ_ARRAY);
CLASS_EMPTY_ARRAY.put(long.class, EMPTY_LONG_ARRAY);
CLASS_EMPTY_ARRAY.put(Long.class, EMPTY_LONG_OBJ_ARRAY);
CLASS_EMPTY_ARRAY.put(float.class, EMPTY_FLOAT_ARRAY);
CLASS_EMPTY_ARRAY.put(Float.class, EMPTY_FLOAT_OBJ_ARRAY);
CLASS_EMPTY_ARRAY.put(double.class, EMPTY_DOUBLE_ARRAY);
CLASS_EMPTY_ARRAY.put(Double.class, EMPTY_DOUBLE_OBJ_ARRAY);
CLASS_EMPTY_ARRAY.put(String.class, EMPTY_STRING_ARRAY);
CLASS_EMPTY_ARRAY.put(Object.class, EMPTY_OBJECT_ARRAY);
}
// ...
static final Map, Integer> CLASS_TYPE_ENUM = new HashMap<>();
static {
CLASS_TYPE_ENUM.put(boolean.class, 1);
CLASS_TYPE_ENUM.put(char.class, 2);
CLASS_TYPE_ENUM.put(byte.class, 3);
CLASS_TYPE_ENUM.put(short.class, 4);
CLASS_TYPE_ENUM.put(int.class, 5);
CLASS_TYPE_ENUM.put(long.class, 6);
CLASS_TYPE_ENUM.put(float.class, 7);
CLASS_TYPE_ENUM.put(double.class, 8);
CLASS_TYPE_ENUM.put(String.class, 9);
CLASS_TYPE_ENUM.put(boolean[].class, 11);
CLASS_TYPE_ENUM.put(char[].class, 12);
CLASS_TYPE_ENUM.put(byte[].class, 13);
CLASS_TYPE_ENUM.put(short[].class, 14);
CLASS_TYPE_ENUM.put(int[].class, 15);
CLASS_TYPE_ENUM.put(long[].class, 16);
CLASS_TYPE_ENUM.put(float[].class, 17);
CLASS_TYPE_ENUM.put(double[].class, 18);
CLASS_TYPE_ENUM.put(String[].class, 19);
CLASS_TYPE_ENUM.put(Boolean.class, 21);
CLASS_TYPE_ENUM.put(Character.class, 22);
CLASS_TYPE_ENUM.put(Byte.class, 23);
CLASS_TYPE_ENUM.put(Short.class, 24);
CLASS_TYPE_ENUM.put(Integer.class, 25);
CLASS_TYPE_ENUM.put(Long.class, 26);
CLASS_TYPE_ENUM.put(Float.class, 27);
CLASS_TYPE_ENUM.put(Double.class, 28);
}
// ...
private static final Map>, ImmutableList extends Enum>>> enumListPool = new ObjectPool<>(POOL_SIZE);
private static final Map>, ImmutableSet extends Enum>>> enumSetPool = new ObjectPool<>(POOL_SIZE);
private static final Map>, ImmutableBiMap extends Enum>, String>> enumMapPool = new ObjectPool<>(POOL_SIZE);
private static final Map> nameTypePool = new ObjectPool<>(POOL_SIZE);
private static final Map, Type>> clsTypePool = new ObjectPool<>(POOL_SIZE);
static final String[] charStringCache = new String[128];
static final int intStringCacheLow = -1001;
static final int intStringCacheHigh = 10001;
static final String[] intStringCache = new String[intStringCacheHigh - intStringCacheLow];
static final Map stringIntCache = new HashMap<>((int) (intStringCache.length * 1.5));
static {
for (int i = 0, j = intStringCacheLow, len = intStringCache.length; i < len; i++, j++) {
intStringCache[i] = Integer.toString(j);
stringIntCache.put(intStringCache[i], j);
}
for (int i = 0; i < charStringCache.length; i++) {
charStringCache[i] = String.valueOf((char) i);
}
}
CommonUtil() {
// Utility class.
}
/**
* Gets a Type by the given type name.
*
* @param typeName the name of the type to be retrieved.
* @return the Type corresponding to the given type name.
* @throws IllegalArgumentException if the specified {@code typeName} is {@code null}.
*/
@SuppressWarnings("unchecked")
public static Type typeOf(@NotNull final String typeName) throws IllegalArgumentException {
checkArgNotNull(typeName, cs.typeName);
Type> type = nameTypePool.get(typeName);
if (type == null) {
type = TypeFactory.getType(typeName);
nameTypePool.put(typeName, type);
}
return (Type) type;
}
/**
* Gets a Type by the given {@code Class}.
*
* @param cls the name of the type to be retrieved.
* @return the Type corresponding to the given type name.
* @throws IllegalArgumentException if the specified {@code Class} is {@code null}.
*/
@SuppressWarnings("unchecked")
public static Type typeOf(@NotNull final Class> cls) throws IllegalArgumentException {
checkArgNotNull(cls, cs.cls);
Type> type = clsTypePool.get(cls);
if (type == null) {
type = TypeFactory.getType(cls);
clsTypePool.put(cls, type);
}
return (Type) type;
}
/**
* Returns the default value of the given class type.
*
* @param
* @param cls the class type for which the default value is to be returned.
* @return the default value of the given class type. For example, for an Integer class type, it will return 0.
* @throws IllegalArgumentException if the specified class type is {@code null}.
*/
@SuppressWarnings("unchecked")
public static T defaultValueOf(final Class cls) {
return (T) typeOf(cls).defaultValue();
}
/**
* Returns the default value of the given type if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @return
*/
public static boolean defaultIfNull(final Boolean b) {
if (b == null) {
return false;
}
return b;
}
/**
* Returns the specified default value if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @param defaultForNull
* @return
*/
public static boolean defaultIfNull(final Boolean b, final boolean defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Returns the default value of the given type if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param c
* @return
*/
public static char defaultIfNull(final Character c) {
if (c == null) {
return Strings.CHAR_ZERO;
}
return c;
}
/**
* Returns the specified default value if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param c
* @param defaultForNull
* @return
*/
public static char defaultIfNull(final Character c, final char defaultForNull) {
if (c == null) {
return defaultForNull;
}
return c;
}
/**
* Returns the default value of the given type if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @return
*/
public static byte defaultIfNull(final Byte b) {
if (b == null) {
return (byte) 0;
}
return b;
}
/**
* Returns the specified default value if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @param defaultForNull
* @return
*/
public static byte defaultIfNull(final Byte b, final byte defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Returns the default value of the given type if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @return
*/
public static short defaultIfNull(final Short b) {
if (b == null) {
return (short) 0;
}
return b;
}
/**
* Returns the specified default value if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @param defaultForNull
* @return
*/
public static short defaultIfNull(final Short b, final short defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Returns the default value of the given type if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @return
*/
public static int defaultIfNull(final Integer b) {
if (b == null) {
return 0;
}
return b;
}
/**
* Returns the specified default value if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @param defaultForNull
* @return
*/
public static int defaultIfNull(final Integer b, final int defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Returns the default value of the given type if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @return
*/
public static long defaultIfNull(final Long b) {
if (b == null) {
return 0;
}
return b;
}
/**
* Returns the specified default value if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @param defaultForNull
* @return
*/
public static long defaultIfNull(final Long b, final long defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Returns the default value of the given type if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @return
*/
public static float defaultIfNull(final Float b) {
if (b == null) {
return 0;
}
return b;
}
/**
* Returns the specified default value if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @param defaultForNull
* @return
*/
public static float defaultIfNull(final Float b, final float defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Returns the default value of the given type if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @return
*/
public static double defaultIfNull(final Double b) {
if (b == null) {
return 0;
}
return b;
}
/**
* Returns the specified default value if the specified object is {@code null} or itself if the specified object is not {@code null}.
*
* @param b
* @param defaultForNull
* @return
*/
public static double defaultIfNull(final Double b, final double defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Returns the specified default value if the given object is {@code null}, otherwise returns the object itself.
*
* @param the type of the object
* @param obj the object to check for {@code null}
* @param defaultForNull the default value to return if {@code obj} is {@code null}
* @return {@code obj} if it is not {@code null}, otherwise {@code defaultForNull}
* @throws IllegalArgumentException if the specified default value is {@code null}.
* @see Strings#defaultIfNull(CharSequence, CharSequence)
*/
public static T defaultIfNull(final T obj, final T defaultForNull) throws IllegalArgumentException {
checkArgNotNull(defaultForNull, cs.defaultValue);
return obj == null ? defaultForNull : obj;
}
/**
* Returns the default value provided by specified {@code Supplier} if the specified object is {@code null}, otherwise returns the object itself.
*
* @param
* @param obj
* @param supplierForDefault
* @return
* @throws IllegalArgumentException if default value provided by specified {@code Supplier} is {@code null} when the specified object is {@code null}.
* @see Strings#defaultIfNull(CharSequence, Supplier)
*/
public static T defaultIfNull(final T obj, final Supplier extends T> supplierForDefault) throws IllegalArgumentException {
if (obj == null) {
return checkArgNotNull(supplierForDefault.get(), cs.defaultValue);
}
return obj;
}
/**
* Returns the specified default value if the specified {@code charSequence} is empty, otherwise returns the {@code charSequence} itself.
*
* @param
* @param str
* @param defaultForEmpty
* @return
* @throws IllegalArgumentException if the specified default charSequence value is empty.
* @see Strings#defaultIfEmpty(CharSequence, CharSequence)
*/
public static T defaultIfEmpty(final T str, final T defaultForEmpty) throws IllegalArgumentException {
checkArgNotEmpty(defaultForEmpty, cs.defaultValue);
return isEmpty(str) ? defaultForEmpty : str;
}
/**
* Returns the default value provided by specified {@code Supplier} if the specified {@code charSequence} is empty, otherwise returns the {@code charSequence} itself.
*
* @param
* @param str
* @param supplierForDefault
* @return
* @throws IllegalArgumentException if default value provided by specified {@code Supplier} is empty when the specified {@code charSequence} is empty.
* @see Strings#defaultIfEmpty(CharSequence, Supplier)
*/
public static T defaultIfEmpty(final T str, final Supplier extends T> supplierForDefault) {
if (isEmpty(str)) {
return checkArgNotEmpty(supplierForDefault.get(), cs.defaultValue);
}
return str;
}
/**
* Returns the specified default value if the specified {@code charSequence} is blank, otherwise returns the {@code charSequence} itself.
*
* @param
* @param str
* @param defaultForBlank
* @return
* @throws IllegalArgumentException if the specified default charSequence value is bank.
* @see Strings#defaultIfBlank(CharSequence, CharSequence)
*/
public static T defaultIfBlank(final T str, final T defaultForBlank) throws IllegalArgumentException {
checkArgNotBlank(defaultForBlank, cs.defaultValue);
return isBlank(str) ? defaultForBlank : str;
}
/**
* Returns the default value provided by specified {@code Supplier} if the specified {@code charSequence} is blank, otherwise returns the {@code charSequence} itself.
*
* @param
* @param str
* @param supplierForDefault
* @return
* @throws IllegalArgumentException if default value provided by specified {@code Supplier} is blank when the specified {@code charSequence} is blank.
* @see Strings#defaultIfBlank(CharSequence, Supplier)
*/
public static T defaultIfBlank(final T str, final Supplier extends T> supplierForDefault) {
if (isBlank(str)) {
return checkArgNotBlank(supplierForDefault.get(), cs.defaultValue);
}
return str;
}
/**
* Returns the specified default value if the specified Collection is empty, otherwise returns the {@code Collection} itself.
*
* @param
* @param c
* @param defaultForEmpty
* @return
* @throws IllegalArgumentException if the specified default collection value is empty.
*/
public static > T defaultIfEmpty(final T c, final T defaultForEmpty) throws IllegalArgumentException {
checkArgNotEmpty(defaultForEmpty, cs.defaultValue);
return isEmpty(c) ? defaultForEmpty : c;
}
/**
* Returns the specified default value if the specified Map is empty, otherwise returns the {@code Map} itself.
*
* @param
* @param m
* @param defaultForEmpty
* @return
* @throws IllegalArgumentException if the specified default map value is empty.
*/
public static > T defaultIfEmpty(final T m, final T defaultForEmpty) throws IllegalArgumentException {
checkArgNotEmpty(defaultForEmpty, cs.defaultValue);
return isEmpty(m) ? defaultForEmpty : m;
}
/**
* Converts the given value to its corresponding String representation.
*
* @param val the value to be converted.
* @return the String representation of the given value. Returns "true" if the value is {@code true}, "false" otherwise.
*/
public static String stringOf(final boolean val) {
return val ? Strings.TRUE : Strings.FALSE;
}
/**
* Converts the given value to its corresponding String representation.
*
* @param val the value to be converted.
* @return the String representation of the given value.
*/
public static String stringOf(final char val) {
if (val < 128) {
return charStringCache[val];
}
return String.valueOf(val);
}
/**
* Converts the given value to its corresponding String representation.
*
* @param val the value to be converted.
* @return the String representation of the given value.
*/
@SuppressFBWarnings({ "INT_BAD_COMPARISON_WITH_SIGNED_BYTE", "INT_BAD_COMPARISON_WITH_SIGNED_BYTE" })
public static String stringOf(final byte val) {
//noinspection ConstantValue
if (val > intStringCacheLow && val < intStringCacheHigh) {
return intStringCache[val - intStringCacheLow];
}
return String.valueOf(val);
}
/**
* Converts the given value to its corresponding String representation.
*
* @param val the value to be converted.
* @return the String representation of the given value.
*/
public static String stringOf(final short val) {
if (val > intStringCacheLow && val < intStringCacheHigh) {
return intStringCache[val - intStringCacheLow];
}
return String.valueOf(val);
}
/**
* Converts the given value to its corresponding String representation.
*
* @param val the value to be converted.
* @return the String representation of the given value.
*/
public static String stringOf(final int val) {
if (val > intStringCacheLow && val < intStringCacheHigh) {
return intStringCache[val - intStringCacheLow];
}
return String.valueOf(val);
}
/**
* Converts the given value to its corresponding String representation.
*
* @param val the value to be converted.
* @return the String representation of the given value.
*/
public static String stringOf(final long val) {
if (val > intStringCacheLow && val < intStringCacheHigh) {
return intStringCache[(int) (val - intStringCacheLow)];
}
return String.valueOf(val);
}
/**
* Converts the given value to its corresponding String representation.
*
* @param val the value to be converted.
* @return the String representation of the given value.
*/
public static String stringOf(final float val) {
return String.valueOf(val);
}
/**
* Converts the given value to its corresponding String representation.
*
* @param val the value to be converted.
* @return the String representation of the given value.
*/
public static String stringOf(final double val) {
return String.valueOf(val);
}
/**
* Converts the given value to its corresponding String representation by {@code Type.stringOf(Object)}.
*
* @param obj the value to be converted.
* @return the String representation of the given value. {@code null} if the specified object is null
* @see #valueOf(String, Class)
* @see Type#stringOf(Object)
*/
public static String stringOf(final Object obj) {
return (obj == null) ? null : typeOf(obj.getClass()).stringOf(obj);
}
/**
* Converts the given string to its corresponding value of the specified target type by {@code typeOf(targetType).valueOf(str)}.
*
* @param The type of the target object after conversion.
* @param str The string to be converted.
* @param targetType The class of the target type to which the string is to be converted.
* @return The converted value of the specified target type. If the input string is {@code null}, it returns the default value of the target type.
* @throws IllegalArgumentException if the specified target type is {@code null}.
* @see #stringOf(Object)
* @see Type#valueOf(String)
*/
@SuppressWarnings("unchecked")
public static T valueOf(final String str, final Class extends T> targetType) {
return (str == null) ? defaultValueOf(targetType) : (T) typeOf(targetType).valueOf(str);
}
private static final Map, BiFunction