forked from MagicBane/Server
fromString and asString EnumSet helper methods created
This commit is contained in:
@@ -55,6 +55,46 @@ public class mbEnums {
|
|||||||
return enumSet;
|
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 {
|
public enum PetitionType {
|
||||||
NONE,
|
NONE,
|
||||||
GENERAL,
|
GENERAL,
|
||||||
|
|||||||
Reference in New Issue
Block a user