Browse Source

fromString and asString EnumSet helper methods created

combat-2
MagicBot 7 months ago
parent
commit
181e7661c1
  1. 40
      src/engine/mbEnums.java

40
src/engine/mbEnums.java

@ -55,6 +55,46 @@ public class mbEnums { @@ -55,6 +55,46 @@ public class mbEnums {
return enumSet;
}
public static <E extends java.lang.Enum<E>> EnumSet<E> fromString(String dbString, Class<E> enumClass) {
// Build enumset from ; delimited string (from db)
EnumSet<E> 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 <E extends Enum<E>> String asString(EnumSet<E> 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,

Loading…
Cancel
Save