1 16 17 package cintoo.messages.error; 18 19 import api.cintoo.messages.context.Context; 20 21 import java.lang.reflect.Field ; 22 import java.util.HashSet ; 23 import java.util.Set ; 24 25 34 public class Errors { 35 private static ErrorHandler handler; 36 37 43 private static synchronized ErrorHandler handler() { 44 if (null == handler) { 45 handler = new ErrorHandler(); 46 } 47 return handler; 48 } 49 50 public static String error(Context context, ErrorCode errorCode) { 51 return handler().error(context, errorCode); 52 } 53 54 public static String error(String context, ErrorCode errorCode) { 55 return handler().error(context, errorCode); 56 } 57 58 public static String error(Object context, ErrorCode errorCode) { 59 return handler().error(context, errorCode); 60 } 61 62 public static String error(ErrorCode errorCode) { 63 return handler().error(errorCode); 64 } 65 66 public static void setPattern(String pattern) { 67 handler().setPattern(pattern); 68 } 69 70 public static void clearPattern() { 71 handler().clearPattern(); 72 } 73 74 79 public static void setBundle(String bundleName) { 80 handler().setBundle(bundleName); 81 } 82 83 89 public static void setBundle(String bundleName, String packageName) { 90 handler().setBundle(bundleName, packageName); 91 } 92 93 99 public static void setBundle(String bundleName, Context context) { 100 handler().setBundle(bundleName, context); 101 } 102 103 public static boolean validateErrorCodes(Class [] classesToCheck) { 104 boolean result = true; 105 Set <Integer > codes = new HashSet <Integer >(); 106 for (Class toCheck : classesToCheck) { 107 if (! validateErrorCodes(toCheck, codes)) { 108 result = false; 109 break; 110 } 111 } 112 return result; 113 } 114 115 private static boolean validateErrorCodes(Class toCheck, Set <Integer > codes) { 116 boolean result = true; 117 for (Field field : toCheck.getDeclaredFields()) { 118 if (field.getType().equals(ErrorCode.class)) { 119 try { 120 ErrorCode error = (ErrorCode) field.get(toCheck); 121 int code = error.getCode(); 122 if (codes.contains(code)) { 123 result = false; 124 break; 125 } 126 codes.add(code); 127 } catch (IllegalAccessException e) { 128 e.printStackTrace(); 129 } 130 } 131 } 132 return result; 133 } 134 135 public static boolean validateErrorCodes(Class classToCheck) { 136 return validateErrorCodes(classToCheck, new HashSet <Integer >()); 137 } 138 } | Popular Tags |