com.landawn.abacus.util.CommonUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* 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
*
* http://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.Serializable;
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.nio.charset.Charset;
import java.security.SecureRandom;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
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.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.UUID;
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.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.Immutable;
import com.landawn.abacus.annotation.Internal;
import com.landawn.abacus.annotation.NullSafe;
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.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 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 null or empty String. the input String will be
* returned. But exception will be thrown if trying to repeat/swap a null or
* empty string or operate Array/Collection by adding/removing...
*
* @author Haiyang Li
*
* @version $Revision: 0.8 $ 07/03/10
*
* @see com.landawn.abacus.util.Array
* @see com.landawn.abacus.util.Iterables
* @see com.landawn.abacus.util.Iterators
* @see com.landawn.abacus.util.Maps
* @see com.landawn.abacus.util.Strings
* @see com.landawn.abacus.util.IOUtil
*/
@SuppressWarnings("java:S1192")
class CommonUtil {
// ... 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;
/**
* An empty immutable {@code Boolean} array.
*/
static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = {};
/**
* An empty immutable {@code Character} array.
*/
static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = {};
/**
* An empty immutable {@code Byte} array.
*/
static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = {};
/**
* An empty immutable {@code Short} array.
*/
static final Short[] EMPTY_SHORT_OBJECT_ARRAY = {};
/**
* An empty immutable {@code Integer} array.
*/
static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = {};
/**
* An empty immutable {@code Long} array.
*/
static final Long[] EMPTY_LONG_OBJECT_ARRAY = {};
/**
* An empty immutable {@code Float} array.
*/
static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = {};
/**
* An empty immutable {@code Double} array.
*/
static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = {};
/**
* An empty immutable {@code Class} array.
*/
static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
static final String TRUE = Boolean.TRUE.toString().intern();
static final char[] TRUE_CHAR_ARRAY = TRUE.toCharArray();
static final String FALSE = Boolean.FALSE.toString().intern();
static final char[] FALSE_CHAR_ARRAY = FALSE.toCharArray();
// /**
// * String with value {@code "null"}.
// * @deprecated replaced by {@code Strings.NULL_STRING}
// * @see Strings#NULL_STRING
// */
// @Deprecated
// public static final String NULL_STRING = "null".intern();
//
// /**
// * @deprecated replaced by {@code Strings.NULL_CHAR_ARRAY}
// * @see Strings#NULL_CHAR_ARRAY
// */
// @Deprecated
// public static final char[] NULL_CHAR_ARRAY = NULL_STRING.toCharArray();
static final char CHAR_ZERO = WD.CHAR_ZERO;
/**
* The Constant CHAR_LF.
*
* @see JLF:
* Escape Sequences for Character and String Literals
* @since 2.2
*/
static final char CHAR_LF = WD.CHAR_LF;
/**
* The Constant CHAR_CR.
*
* @see JLF:
* Escape Sequences for Character and String Literals
* @since 2.2
*/
static final char CHAR_CR = WD.CHAR_CR;
static final int BINARYSEARCH_THRESHOLD = 64;
// ...
/**
* 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;
// ...
public static final String EMPTY_STRING = "".intern();
/**
* An empty immutable {@code boolean} array.
*/
public static final boolean[] EMPTY_BOOLEAN_ARRAY = {};
/**
* An empty immutable {@code char} array.
*/
public static final char[] EMPTY_CHAR_ARRAY = {};
/**
* An empty immutable {@code byte} array.
*/
public static final byte[] EMPTY_BYTE_ARRAY = {};
/**
* An empty immutable {@code short} array.
*/
public static final short[] EMPTY_SHORT_ARRAY = {};
/**
* An empty immutable {@code int} array.
*/
public static final int[] EMPTY_INT_ARRAY = {};
/**
* An empty immutable {@code long} array.
*/
public static final long[] EMPTY_LONG_ARRAY = {};
/**
* An empty immutable {@code float} array.
*/
public static final float[] EMPTY_FLOAT_ARRAY = {};
/**
* An empty immutable {@code double} array.
*/
public static final double[] EMPTY_DOUBLE_ARRAY = {};
/**
* An empty immutable {@code Boolean} array.
*/
public static final Boolean[] EMPTY_BOOLEAN_OBJ_ARRAY = {};
/**
* An empty immutable {@code Character} array.
*/
public static final Character[] EMPTY_CHAR_OBJ_ARRAY = {};
/**
* An empty immutable {@code Byte} array.
*/
public static final Byte[] EMPTY_BYTE_OBJ_ARRAY = {};
/**
* An empty immutable {@code Short} array.
*/
public static final Short[] EMPTY_SHORT_OBJ_ARRAY = {};
/**
* An empty immutable {@code Integer} array.
*/
public static final Integer[] EMPTY_INT_OBJ_ARRAY = {};
/**
* An empty immutable {@code Long} array.
*/
public static final Long[] EMPTY_LONG_OBJ_ARRAY = {};
/**
* An empty immutable {@code Float} array.
*/
public static final Float[] EMPTY_FLOAT_OBJ_ARRAY = {};
/**
* An empty immutable {@code Double} array.
*/
public static final Double[] EMPTY_DOUBLE_OBJ_ARRAY = {};
/**
* An empty immutable {@code String} array.
*/
public static final String[] EMPTY_STRING_ARRAY = {};
/**
* An empty immutable {@code Date} array.
*/
public static final java.util.Date[] EMPTY_DATE_ARRAY = {};
/**
* An empty immutable {@code Calendar} array.
*/
public static final Calendar[] EMPTY_CALENDAR_ARRAY = {};
/**
* An empty immutable {@code Object} array.
*/
public static final Object[] EMPTY_OBJECT_ARRAY = {};
@SuppressWarnings("rawtypes")
static final List EMPTY_LIST = Collections.emptyList();
@SuppressWarnings("rawtypes")
static final Set EMPTY_SET = Collections.emptySet();
@SuppressWarnings("rawtypes")
static final SortedSet EMPTY_SORTED_SET = Collections.emptySortedSet();
@SuppressWarnings("rawtypes")
static final NavigableSet EMPTY_NAVIGABLE_SET = Collections.emptyNavigableSet();
@SuppressWarnings("rawtypes")
static final Map EMPTY_MAP = Collections.emptyMap();
@SuppressWarnings("rawtypes")
static final SortedMap EMPTY_SORTED_MAP = Collections.emptySortedMap();
@SuppressWarnings("rawtypes")
static final NavigableMap EMPTY_NAVIGABLE_MAP = Collections.emptyNavigableMap();
@SuppressWarnings("rawtypes")
static final Iterator EMPTY_ITERATOR = Collections.emptyIterator();
@SuppressWarnings("rawtypes")
static final ListIterator EMPTY_LIST_ITERATOR = Collections.emptyListIterator();
// ...
static final Object NULL_MASK = new NullMask();
static final String BACKSLASH_ASTERISK = "*";
// ...
static final int REVERSE_THRESHOLD = 18;
static final int FILL_THRESHOLD = 25;
static final int REPLACEALL_THRESHOLD = 11;
// ...
static final Random RAND = new SecureRandom();
@SuppressWarnings("rawtypes")
static final Comparator NULL_MIN_COMPARATOR = Comparators.nullsFirst();
@SuppressWarnings("rawtypes")
static final Comparator NULL_MAX_COMPARATOR = Comparators.nullsLast();
@SuppressWarnings("rawtypes")
static final Comparator NATURAL_ORDER = Comparators.naturalOrder();
// ...
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_OBJECT_ARRAY);
CLASS_EMPTY_ARRAY.put(char.class, EMPTY_CHAR_ARRAY);
CLASS_EMPTY_ARRAY.put(Character.class, EMPTY_CHARACTER_OBJECT_ARRAY);
CLASS_EMPTY_ARRAY.put(byte.class, EMPTY_BYTE_ARRAY);
CLASS_EMPTY_ARRAY.put(Byte.class, EMPTY_BYTE_OBJECT_ARRAY);
CLASS_EMPTY_ARRAY.put(short.class, EMPTY_SHORT_ARRAY);
CLASS_EMPTY_ARRAY.put(Short.class, EMPTY_SHORT_OBJECT_ARRAY);
CLASS_EMPTY_ARRAY.put(int.class, EMPTY_INT_ARRAY);
CLASS_EMPTY_ARRAY.put(Integer.class, EMPTY_INTEGER_OBJECT_ARRAY);
CLASS_EMPTY_ARRAY.put(long.class, EMPTY_LONG_ARRAY);
CLASS_EMPTY_ARRAY.put(Long.class, EMPTY_LONG_OBJECT_ARRAY);
CLASS_EMPTY_ARRAY.put(float.class, EMPTY_FLOAT_ARRAY);
CLASS_EMPTY_ARRAY.put(Float.class, EMPTY_FLOAT_OBJECT_ARRAY);
CLASS_EMPTY_ARRAY.put(double.class, EMPTY_DOUBLE_ARRAY);
CLASS_EMPTY_ARRAY.put(Double.class, EMPTY_DOUBLE_OBJECT_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);
}
// ...
static final BiMap, Class> PRIMITIVE_2_WRAPPER = new BiMap<>();
static {
PRIMITIVE_2_WRAPPER.put(boolean.class, Boolean.class);
PRIMITIVE_2_WRAPPER.put(char.class, Character.class);
PRIMITIVE_2_WRAPPER.put(byte.class, Byte.class);
PRIMITIVE_2_WRAPPER.put(short.class, Short.class);
PRIMITIVE_2_WRAPPER.put(int.class, Integer.class);
PRIMITIVE_2_WRAPPER.put(long.class, Long.class);
PRIMITIVE_2_WRAPPER.put(float.class, Float.class);
PRIMITIVE_2_WRAPPER.put(double.class, Double.class);
PRIMITIVE_2_WRAPPER.put(boolean[].class, Boolean[].class);
PRIMITIVE_2_WRAPPER.put(char[].class, Character[].class);
PRIMITIVE_2_WRAPPER.put(byte[].class, Byte[].class);
PRIMITIVE_2_WRAPPER.put(short[].class, Short[].class);
PRIMITIVE_2_WRAPPER.put(int[].class, Integer[].class);
PRIMITIVE_2_WRAPPER.put(long[].class, Long[].class);
PRIMITIVE_2_WRAPPER.put(float[].class, Float[].class);
PRIMITIVE_2_WRAPPER.put(double[].class, Double[].class);
}
// ...
private static final Map>, ImmutableList>> enumListPool = new ObjectPool<>(POOL_SIZE);
private static final Map>, ImmutableSet>> enumSetPool = new ObjectPool<>(POOL_SIZE);
private static final Map>, ImmutableBiMap, 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);
}
}
private static final int MIN_SIZE_FOR_COPY_ALL = 9;
CommonUtil() {
// Utility class.
}
/**
*
* @param
* @param typeName
* @return
* @throws IllegalArgumentException if the specified typeName
is null
.
*/
@SuppressWarnings("unchecked")
public static Type typeOf(final String typeName) throws IllegalArgumentException {
N.checkArgNotNull(typeName, "typeName");
Type type = nameTypePool.get(typeName);
if (type == null) {
type = TypeFactory.getType(typeName);
nameTypePool.put(typeName, type);
}
return (Type) type;
}
/**
*
* @param
* @param cls
* @return
* @throws IllegalArgumentException if the specified {@code Class} is null
.
*/
@SuppressWarnings("unchecked")
public static Type typeOf(final Class cls) throws IllegalArgumentException {
N.checkArgNotNull(cls, "cls");
Type type = clsTypePool.get(cls);
if (type == null) {
type = TypeFactory.getType(cls);
clsTypePool.put(cls, type);
}
return (Type) type;
}
/**
*
*
* @param
* @param str
* @param targetClass
* @return
*/
@SuppressWarnings("unchecked")
public static T valueOf(final String str, final Class targetClass) {
return (str == null) ? defaultValueOf(targetClass) : (T) typeOf(targetClass).valueOf(str);
}
/**
* Default value of.
*
* @param
* @param cls
* @return
*/
@SuppressWarnings("unchecked")
public static T defaultValueOf(final Class cls) {
return (T) typeOf(cls).defaultValue();
}
/**
* Default if null.
*
* @param b
* @return
*/
public static boolean defaultIfNull(Boolean b) {
if (b == null) {
return false;
}
return b;
}
/**
* Default if null.
*
* @param b
* @param defaultForNull
* @return
*/
public static boolean defaultIfNull(Boolean b, boolean defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Default if null.
*
* @param c
* @return
*/
public static char defaultIfNull(Character c) {
if (c == null) {
return CHAR_ZERO;
}
return c;
}
/**
* Default if null.
*
* @param c
* @param defaultForNull
* @return
*/
public static char defaultIfNull(Character c, char defaultForNull) {
if (c == null) {
return defaultForNull;
}
return c;
}
/**
* Default if null.
*
* @param b
* @return
*/
public static byte defaultIfNull(Byte b) {
if (b == null) {
return (byte) 0;
}
return b;
}
/**
* Default if null.
*
* @param b
* @param defaultForNull
* @return
*/
public static byte defaultIfNull(Byte b, byte defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Default if null.
*
* @param b
* @return
*/
public static short defaultIfNull(Short b) {
if (b == null) {
return (short) 0;
}
return b;
}
/**
* Default if null.
*
* @param b
* @param defaultForNull
* @return
*/
public static short defaultIfNull(Short b, short defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Default if null.
*
* @param b
* @return
*/
public static int defaultIfNull(Integer b) {
if (b == null) {
return 0;
}
return b;
}
/**
* Default if null.
*
* @param b
* @param defaultForNull
* @return
*/
public static int defaultIfNull(Integer b, int defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Default if null.
*
* @param b
* @return
*/
public static long defaultIfNull(Long b) {
if (b == null) {
return 0;
}
return b;
}
/**
* Default if null.
*
* @param b
* @param defaultForNull
* @return
*/
public static long defaultIfNull(Long b, long defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Default if null.
*
* @param b
* @return
*/
public static float defaultIfNull(Float b) {
if (b == null) {
return 0;
}
return b;
}
/**
* Default if null.
*
* @param b
* @param defaultForNull
* @return
*/
public static float defaultIfNull(Float b, float defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Default if null.
*
* @param b
* @return
*/
public static double defaultIfNull(Double b) {
if (b == null) {
return 0;
}
return b;
}
/**
* Default if null.
*
* @param b
* @param defaultForNull
* @return
*/
public static double defaultIfNull(Double b, double defaultForNull) {
if (b == null) {
return defaultForNull;
}
return b;
}
/**
* Default if null.
*
* @param
* @param obj
* @param defaultForNull
* @return
*/
public static T defaultIfNull(final T obj, final T defaultForNull) {
return obj == null ? defaultForNull : obj;
}
/**
* Default if null.
*
* @param
* @param obj
* @param supplierForDefault
* @return
*/
public static T defaultIfNull(final T obj, final Supplier supplierForDefault) {
if (obj == null) {
return supplierForDefault.get();
}
return obj;
}
/**
*
*
* @param
* @param str
* @param defaultStr
* @return
*/
public static T defaultIfNullOrEmpty(final T str, final T defaultStr) {
return N.isNullOrEmpty(str) ? defaultStr : str;
}
/**
* Returns either the passed in CharSequence, or if the CharSequence is
* whitespace, empty ("") or {@code null}, the value of {@code defaultStr}.
*
* Whitespace is defined by {@link Character#isWhitespace(char)}.
*
* @param
* @param str
* @param defaultStr
* @return
*/
public static T defaultIfBlank(final T str, final T defaultStr) {
return N.isBlank(str) ? defaultStr : str;
}
/**
*
* @param val
* @return
*/
public static String stringOf(final boolean val) {
return String.valueOf(val);
}
/**
*
* @param val
* @return
*/
public static String stringOf(final char val) {
if (val < 128) {
return charStringCache[val];
}
return String.valueOf(val);
}
/**
*
* @param val
* @return
*/
public static String stringOf(final byte val) {
if (val > intStringCacheLow && val < intStringCacheHigh) {
return intStringCache[val - intStringCacheLow];
}
return String.valueOf(val);
}
/**
*
* @param val
* @return
*/
public static String stringOf(final short val) {
if (val > intStringCacheLow && val < intStringCacheHigh) {
return intStringCache[val - intStringCacheLow];
}
return String.valueOf(val);
}
/**
*
* @param val
* @return
*/
public static String stringOf(final int val) {
if (val > intStringCacheLow && val < intStringCacheHigh) {
return intStringCache[val - intStringCacheLow];
}
return String.valueOf(val);
}
/**
*
* @param val
* @return
*/
public static String stringOf(final long val) {
if (val > intStringCacheLow && val < intStringCacheHigh) {
return intStringCache[(int) (val - intStringCacheLow)];
}
return String.valueOf(val);
}
/**
*
* @param val
* @return
*/
public static String stringOf(final float val) {
return String.valueOf(val);
}
/**
*
* @param val
* @return
*/
public static String stringOf(final double val) {
return String.valueOf(val);
}
/**
*
* @param obj
* @return null
if the specified object is null.
*/
public static String stringOf(final Object obj) {
return (obj == null) ? null : typeOf(obj.getClass()).stringOf(obj);
}
/**
* Enum list of.
*
* @param
* @param enumClass
* @return
*/
@SuppressWarnings("deprecation")
public static > ImmutableList enumListOf(final Class enumClass) {
ImmutableList enumList = (ImmutableList) enumListPool.get(enumClass);
if (enumList == null) {
enumList = ImmutableList.wrap(asList(enumClass.getEnumConstants()));
enumListPool.put(enumClass, enumList);
}
return enumList;
}
/**
* Enum set of.
*
* @param
* @param enumClass
* @return
*/
@SuppressWarnings("deprecation")
public static > ImmutableSet enumSetOf(final Class enumClass) {
ImmutableSet enumSet = (ImmutableSet) enumSetPool.get(enumClass);
if (enumSet == null) {
enumSet = ImmutableSet.wrap(EnumSet.allOf(enumClass));
enumSetPool.put(enumClass, enumSet);
}
return enumSet;
}
/**
* Enum map of.
*
* @param
* @param enumClass
* @return
*/
@SuppressWarnings("deprecation")
public static > ImmutableBiMap enumMapOf(final Class enumClass) {
ImmutableBiMap enumMap = (ImmutableBiMap) enumMapPool.get(enumClass);
if (enumMap == null) {
final EnumMap keyMap = new EnumMap<>(enumClass);
final Map valueMap = new HashMap<>();
for (final E e : enumClass.getEnumConstants()) {
keyMap.put(e, e.name());
valueMap.put(e.name(), e);
}
enumMap = ImmutableBiMap.wrap(new BiMap<>(keyMap, valueMap));
enumMapPool.put(enumClass, enumMap);
}
return enumMap;
}
/**
*
* @param
* @param cls
* @return T
* @see Suppliers#ofCollection(Class)
* @see Suppliers#registerForCollection(Class, com.landawn.abacus.util.function.Supplier)
* @see Suppliers#ofMap(Class)
* @see Suppliers#registerForMap(Class, com.landawn.abacus.util.function.Supplier)
* @see IntFunctions#ofCollection(Class)
* @see IntFunctions#registerForCollection(Class, com.landawn.abacus.util.function.IntFunction)
* @see IntFunctions#ofMap(Class)
* @see IntFunctions#registerForMap(Class, com.landawn.abacus.util.function.IntFunction)
*/
@SuppressWarnings("rawtypes")
public static T newInstance(final Class cls) {
if (Modifier.isAbstract(cls.getModifiers())) {
if (Collection.class.isAssignableFrom(cls)) {
return (T) Suppliers.ofCollection((Class) cls).get();
} else if (Map.class.isAssignableFrom(cls)) {
return (T) Suppliers.ofMap((Class