From 181e7661c1c28cf0abbe895433a1661d77e47909 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Fri, 5 Apr 2024 08:10:28 -0400 Subject: [PATCH] fromString and asString EnumSet helper methods created --- src/engine/mbEnums.java | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/engine/mbEnums.java b/src/engine/mbEnums.java index b77bf902..11679641 100644 --- a/src/engine/mbEnums.java +++ b/src/engine/mbEnums.java @@ -55,6 +55,46 @@ public class mbEnums { return enumSet; } + public static > EnumSet fromString(String dbString, Class enumClass) { + + // Build enumset from ; delimited string (from db) + + EnumSet enumSet = EnumSet.noneOf(enumClass); + + // Early exit if empty set + + if (dbString.isEmpty()) + return enumSet; + + String[] enumArray = dbString.split(";"); + + for (String enumeration : enumArray) { + E parsedEnum = Enum.valueOf(enumClass, enumeration); + enumSet.add(parsedEnum); + } + + return enumSet; + } + + + public static > String asString(EnumSet enumSet) { + + if (enumSet == null || enumSet.isEmpty()) + return ""; + + StringBuilder result = new StringBuilder(); + + for (E element : enumSet) + result.append(element.name()).append(";"); + + // Remove the trailing comma + + if (result.length() > 1) + result.setLength(result.length() - 1); + + return result.toString(); + } + public enum PetitionType { NONE, GENERAL,