diff --git a/src/main/java/ua/net/uid/utils/helpers/Cast.java b/src/main/java/ua/net/uid/utils/helpers/Cast.java index f646a9e..e62b3a9 100644 --- a/src/main/java/ua/net/uid/utils/helpers/Cast.java +++ b/src/main/java/ua/net/uid/utils/helpers/Cast.java @@ -139,4 +139,8 @@ public static > T toEnum(Class enumType, String value, T defaults) { return EnumHelper.valueOf(enumType, value, defaults); } + + public static > T toEnumIgnoreCase(Class enumType, String value, T defaults) { + return EnumHelper.valueOfIgnoreCase(enumType, value, defaults); + } } diff --git a/src/main/java/ua/net/uid/utils/helpers/CharsetHelper.java b/src/main/java/ua/net/uid/utils/helpers/CharsetHelper.java new file mode 100644 index 0000000..2774820 --- /dev/null +++ b/src/main/java/ua/net/uid/utils/helpers/CharsetHelper.java @@ -0,0 +1,44 @@ +/* + * Copyright 2019 nightfall. + * + * 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 ua.net.uid.utils.helpers; + +import java.nio.charset.Charset; +import java.nio.charset.IllegalCharsetNameException; + +public class CharsetHelper { + private CharsetHelper() {} + + public static boolean isSupported(String charset) { + if (charset != null) { + try { + return Charset.isSupported(charset); + } catch (IllegalCharsetNameException ex) {} + } + return false; + } + + public static Charset toCharset(Charset charset) { + return charset == null ? Charset.defaultCharset() : charset; + } + + public static Charset toCharset(String charsetName) { + return charsetName == null ? Charset.defaultCharset() : Charset.forName(charsetName); + } + + public static String toCharsetName(String charsetName) { + return toCharset(charsetName).name(); + } +} \ No newline at end of file diff --git a/src/main/java/ua/net/uid/utils/helpers/CommonHelper.java b/src/main/java/ua/net/uid/utils/helpers/CommonHelper.java index efd07d8..bced1a9 100644 --- a/src/main/java/ua/net/uid/utils/helpers/CommonHelper.java +++ b/src/main/java/ua/net/uid/utils/helpers/CommonHelper.java @@ -15,6 +15,7 @@ */ package ua.net.uid.utils.helpers; +import java.lang.reflect.Array; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -54,6 +55,54 @@ return array == null || array.length == 0; } + public static boolean isEmpty(Object object) { + if (object == null) return true; + if (object instanceof CharSequence) return ((CharSequence) object).length() == 0; + if (object.getClass().isArray()) return Array.getLength(object) == 0; + if (object instanceof Collection) return ((Collection) object).isEmpty(); + if (object instanceof Map) return ((Map) object).isEmpty(); + return false; + } + + @SafeVarargs + public static T coalesce(T ... items) { + if (!isEmpty(items)) { + for (T item : items) + if (item != null) + return item; + } + return null; + } + + public static > int compare(T v1, T v2, boolean nullGreater) { + if (v1 == v2) return 0; + if (v1 == null) return nullGreater ? 1 : -1; + if (v2 == null) return nullGreater ? -1 : 1; + return v1.compareTo(v2); + } + + @SafeVarargs + public static > T min(T ... items) { + T result = null; + if (!isEmpty(items)) { + for (T item : items) + if (compare(item, result, true) < 0) + result = item; + } + return result; + } + + @SafeVarargs + public static > T max(T ... items) { + T result = null; + if (!isEmpty(items)) { + for (T item : items) + if (compare(item, result, false) > 0) + result = item; + } + return result; + } + @SafeVarargs public static Set setOf(T ... items) { if (!isEmpty(items)) { diff --git a/src/main/java/ua/net/uid/utils/helpers/EnumHelper.java b/src/main/java/ua/net/uid/utils/helpers/EnumHelper.java index 2735fd1..805340b 100644 --- a/src/main/java/ua/net/uid/utils/helpers/EnumHelper.java +++ b/src/main/java/ua/net/uid/utils/helpers/EnumHelper.java @@ -33,10 +33,22 @@ } } + public static > T valueOfIgnoreCase(Class enumType, String value, T defValue) { + if (value == null) return defValue; + for (T item : enumType.getEnumConstants()) + if (item.name().equalsIgnoreCase(value)) + return item; + return defValue; + } + public static > T valueOf(Class enumType, String value) { return valueOf(enumType, value, null); } + public static > T valueOfIgnoreCase(Class enumType, String value) { + return valueOfIgnoreCase(enumType, value, null); + } + public static > List toList(final Class enumType) { return Arrays.asList(enumType.getEnumConstants()); }