Advertisement
Guest User

funsionsilla mejorada

a guest
Feb 12th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1.     @SuppressWarnings({ "unchecked" })
  2.     public static <T, E extends Enum<?>> T getCorrespondingEnumByValue(Class<T> returnClass, E enumerado){
  3.         if(NoHay._(returnClass) || NoHay._(enumerado)) return null;
  4.        
  5.         Class<E> claseEnum = (Class<E>) enumerado.getClass();
  6.        
  7.         Method getValueEntryEnum = null;
  8.         Method getValueExitEnum = null;
  9.         try {
  10.             getValueEntryEnum = claseEnum.getMethod("getValue");
  11.             getValueExitEnum = returnClass.getMethod("getValue");
  12.         } catch (NoSuchMethodException | SecurityException e) {
  13.             return null;
  14.         }
  15.         Boolean sameType = getValueEntryEnum.getReturnType().equals(getValueExitEnum.getReturnType());
  16.         try {
  17.             Object entryValue = getValueEntryEnum.invoke(enumerado);
  18.             if(NoHay._(entryValue)) return null;
  19.             for(T t: returnClass.getEnumConstants()){
  20.                 Object exitValue = getValueExitEnum.invoke(t);
  21.                 if(sameType){
  22.                     if(entryValue.equals(exitValue))
  23.                         return t;
  24.                 }else{
  25.                     if(isMatchingValue(entryValue,exitValue))
  26.                         return t;
  27.                 }
  28.             }
  29.         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  30.             return null;
  31.         }
  32.         return null;
  33.     }
  34.  
  35.     private static boolean isMatchingValue(Object entryValue, Object exitValue) {
  36.         String stringEntryValue = (entryValue.toString()).trim();
  37.         String stringExitValue = (exitValue.toString()).trim();
  38.         if(StringUtils.isNumber(stringEntryValue)){
  39.             stringEntryValue = ((Double)Double.parseDouble(stringEntryValue)).toString();
  40.         }
  41.         if(StringUtils.isNumber(stringExitValue)){
  42.             stringExitValue = ((Double)Double.parseDouble(stringExitValue)).toString();
  43.         }
  44.         return stringEntryValue.equals(stringExitValue);
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement