added getEnumString method

This commit is contained in:
2019-09-12 19:03:08 +02:00
parent 2128ad95bc
commit 218d7a7365

View File

@ -1,7 +1,10 @@
package de.muehlencord.shared.util; package de.muehlencord.shared.util;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException; import java.text.ParseException;
import java.util.HashMap;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -84,8 +87,7 @@ public abstract class StringUtil {
* @param keyWord1 the starting keyword * @param keyWord1 the starting keyword
* @param keyWord2 the end keywod * @param keyWord2 the end keywod
* @return the string between keyword1 and keyword2 * @return the string between keyword1 and keyword2
* @throws ParseException if the value cannot be determined - e.g. if one of * @throws ParseException if the value cannot be determined - e.g. if one of the keywords is not found
* the keywords is not found
*/ */
public static String getValueBetweenKeywords(String content, String keyWord1, String keyWord2) throws ParseException { public static String getValueBetweenKeywords(String content, String keyWord1, String keyWord2) throws ParseException {
int pos1 = content.indexOf(keyWord1); int pos1 = content.indexOf(keyWord1);
@ -111,4 +113,25 @@ public abstract class StringUtil {
public static boolean isEmpty(String s) { public static boolean isEmpty(String s) {
return (s == null) || (s.equals("")); return (s == null) || (s.equals(""));
} }
public static <E extends Enum<E>> String getEnumString(Class<E> clazz, E[] enumValues, String method) {
try {
String returnValue = "";
Method m = clazz.getMethod(method);
for (E e : enumValues) {
String value = (String) m.invoke(e);
returnValue += value + ", ";
}
if (returnValue.endsWith(", ")) {
returnValue = returnValue.substring(0, returnValue.length() - 2);
}
return returnValue;
} catch (ClassCastException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
LOGGER.error(ex.getMessage());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Detailed stacktrace", new Object[]{ex});
}
return null;
}
} }
}