From e4a54fda20d6a7bb785a0878381d47b493e57e8a Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Mon, 20 Feb 2023 19:29:18 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=B2=D0=B0=D1=82=D1=8C?= =?UTF-8?q?=20enum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../struchkov/haiti/utils/ObjectUtils.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/haiti-utils/src/main/java/dev/struchkov/haiti/utils/ObjectUtils.java b/haiti-utils/src/main/java/dev/struchkov/haiti/utils/ObjectUtils.java index 417c6e6..90cfced 100644 --- a/haiti-utils/src/main/java/dev/struchkov/haiti/utils/ObjectUtils.java +++ b/haiti-utils/src/main/java/dev/struchkov/haiti/utils/ObjectUtils.java @@ -7,7 +7,9 @@ import java.lang.reflect.Method; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Arrays; +import java.util.Optional; +import static dev.struchkov.haiti.utils.Checker.checkNotNull; import static dev.struchkov.haiti.utils.Exceptions.utilityClass; import static dev.struchkov.haiti.utils.Inspector.isNotNull; @@ -123,8 +125,8 @@ public class ObjectUtils { public static boolean isGetMethod(String fieldName, Method method) { isNotNull(fieldName); return (method.getName().startsWith("get")) - && (method.getName().length() == (fieldName.length() + 3)) - && method.getName().toLowerCase().endsWith(fieldName.toLowerCase()); + && (method.getName().length() == (fieldName.length() + 3)) + && method.getName().toLowerCase().endsWith(fieldName.toLowerCase()); } /** @@ -137,8 +139,23 @@ public class ObjectUtils { public static boolean isSetMethod(String fieldName, Method method) { isNotNull(fieldName); return (method.getName().startsWith("set")) - && (method.getName().length() == (fieldName.length() + 3)) - && method.getName().toLowerCase().endsWith(fieldName.toLowerCase()); + && (method.getName().length() == (fieldName.length() + 3)) + && method.getName().toLowerCase().endsWith(fieldName.toLowerCase()); + } + + /** + * Позволяет создать произвольный enum из его значения. + * + * @param name значение из enum + * @param enumClass класс enum + * @param возвращаемый тип енума + * @return преобразованный енум или пустой Optional + */ + public static > Optional createEnum(String name, Class enumClass) { + if (checkNotNull(name)) { + return Optional.of(Enum.valueOf(enumClass, name)); + } + return Optional.empty(); } }