Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @SuppressWarnings({ "unchecked" })
- public static <T, E extends Enum<?>> T getCorrespondingEnumByValue(Class<T> returnClass, E enumerado){
- if(NoHay._(returnClass) || NoHay._(enumerado)) return null;
- Class<E> claseEnum = (Class<E>) enumerado.getClass();
- Method getValueEntryEnum = null;
- Method getValueExitEnum = null;
- try {
- getValueEntryEnum = claseEnum.getMethod("getValue");
- getValueExitEnum = returnClass.getMethod("getValue");
- } catch (NoSuchMethodException | SecurityException e) {
- return null;
- }
- Boolean sameType = getValueEntryEnum.getReturnType().equals(getValueExitEnum.getReturnType());
- try {
- Object entryValue = getValueEntryEnum.invoke(enumerado);
- if(NoHay._(entryValue)) return null;
- for(T t: returnClass.getEnumConstants()){
- Object exitValue = getValueExitEnum.invoke(t);
- if(sameType){
- if(entryValue.equals(exitValue))
- return t;
- }else{
- if(isMatchingValue(entryValue,exitValue))
- return t;
- }
- }
- } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
- return null;
- }
- return null;
- }
- private static boolean isMatchingValue(Object entryValue, Object exitValue) {
- String stringEntryValue = (entryValue.toString()).trim();
- String stringExitValue = (exitValue.toString()).trim();
- if(StringUtils.isNumber(stringEntryValue)){
- stringEntryValue = ((Double)Double.parseDouble(stringEntryValue)).toString();
- }
- if(StringUtils.isNumber(stringExitValue)){
- stringExitValue = ((Double)Double.parseDouble(stringExitValue)).toString();
- }
- return stringEntryValue.equals(stringExitValue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement