ResourceException extends RuntimeException { public static int codeForSymbol(String sym) { String symbol = sym.toUpperCase(); if (symbol.contains("OK")) { return OK; } else if ("CREATED".equals(symbol)) { return CREATED; } else if ("ACCEPTED".equals(symbol)) { return ACCEPTED; } else if ("NO_CONTENT".equals(symbol)) { return NO_CONTENT; } else if ("MOVED_PERMANENTLY".equals(symbol)) { return MOVED_PERMANENTLY; } else if ("FOUND".equals(symbol)) { return FOUND; } else if ("SEE_OTHER".equals(symbol)) { return SEE_OTHER; } else if ("NOT_MODIFIED".equals(symbol)) { return NOT_MODIFIED; } else if ("TEMPORARY_REDIRECT".equals(symbol)) { return TEMPORARY_REDIRECT; } else if ("BAD_REQUEST".equals(symbol)) { return BAD_REQUEST; } else if ("UNAUTHORIZED".equals(symbol)) { return UNAUTHORIZED; } else if ("FORBIDDEN".equals(symbol)) { return FORBIDDEN; } else if ("NOT_FOUND".equals(symbol)) { return NOT_FOUND; } else if ("CONFLICT".equals(symbol)) { return CONFLICT; } else if ("GONE".equals(symbol)) { return GONE; } else if ("PRECONDITION_FAILED".equals(symbol)) { return PRECONDITION_FAILED; } else if ("INTERNAL_SERVER_ERROR".equals(symbol)) { return INTERNAL_SERVER_ERROR; } else if ("NOT_IMPLEMENTED".equals(symbol)) { return NOT_IMPLEMENTED; } else if ("UNSUPPORTED_MEDIA_TYPE".equals(symbol)) { return UNSUPPORTED_MEDIA_TYPE; } else if ("SERVICE_UNAVAILABLE".equals(symbol)) { return SERVICE_UNAVAILABLE; } else { try { return Integer.parseInt(sym); } catch (NumberFormatException ignored) { } } return 0; } ResourceException(int code);  ResourceException(int code, Object data); static int codeForSymbol(String sym); static String symbolForCode(int code); int getCode(); Object getData(); T getData(Class<T> cl); final static int OK; final static int CREATED; final static int ACCEPTED; final static int NO_CONTENT; final static int MOVED_PERMANENTLY; final static int FOUND; final static int SEE_OTHER; final static int NOT_MODIFIED; final static int TEMPORARY_REDIRECT; final static int BAD_REQUEST; final static int UNAUTHORIZED; final static int FORBIDDEN; final static int NOT_FOUND; final static int CONFLICT; final static int GONE; final static int PRECONDITION_FAILED; final static int UNSUPPORTED_MEDIA_TYPE; final static int INTERNAL_SERVER_ERROR; final static int NOT_IMPLEMENTED; final static int SERVICE_UNAVAILABLE; }
@Test public void testCodeForSymbol() { assertEquals(ResourceException.codeForSymbol("ok"), 200); assertEquals(ResourceException.codeForSymbol("CREATED"), 201); assertEquals(ResourceException.codeForSymbol("ACCEPTED"), 202); assertEquals(ResourceException.codeForSymbol("NO_CONTENT"), 204); assertEquals(ResourceException.codeForSymbol("MOVED_PERMANENTLY"), 301); assertEquals(ResourceException.codeForSymbol("FOUND"), 302); assertEquals(ResourceException.codeForSymbol("SEE_OTHER"), 303); assertEquals(ResourceException.codeForSymbol("NOT_MODIFIED"), 304); assertEquals(ResourceException.codeForSymbol("TEMPORARY_REDIRECT"), 307); assertEquals(ResourceException.codeForSymbol("BAD_REQUEST"), 400); assertEquals(ResourceException.codeForSymbol("FORBIDDEN"), 403); assertEquals(ResourceException.codeForSymbol("UNAUTHORIZED"), 401); assertEquals(ResourceException.codeForSymbol("NOT_FOUND"), 404); assertEquals(ResourceException.codeForSymbol("CONFLICT"), 409); assertEquals(ResourceException.codeForSymbol("GONE"), 410); assertEquals(ResourceException.codeForSymbol("PRECONDITION_FAILED"), 412); assertEquals(ResourceException.codeForSymbol("UNSUPPORTED_MEDIA_TYPE"), 415); assertEquals(ResourceException.codeForSymbol("INTERNAL_SERVER_ERROR"), 500); assertEquals(ResourceException.codeForSymbol("NOT_IMPLEMENTED"), 501); assertEquals(ResourceException.codeForSymbol("SERVICE_UNAVAILABLE"), 503); assertEquals(ResourceException.codeForSymbol("UNAUTHORIZED"), 401); assertEquals(ResourceException.codeForSymbol("1111"), 1111); assertEquals(ResourceException.codeForSymbol("abc"), 0); }