Package net.thauvin.erik.httpstatus
Class StatusCode
java.lang.Object
net.thauvin.erik.httpstatus.StatusCode
- All Implemented Interfaces:
Serializable
Represents an HTTP status code and provides convenience methods for determining
its classification (informational, success, redirect, client error, server error,
or project-defined extension codes).
Static methods operate on a raw status code, while instance methods operate on
the stored code value. Several methods have both short and long forms
(e.g., isInfo and isInformational) for readability.
- Since:
- 1.1.0
- Author:
- Erik C. Thauvin
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new StatusCode with no initial value.StatusCode(int code) Creates a new StatusCode with the given value. -
Method Summary
Modifier and TypeMethodDescriptionintgetCode()Returns the stored status code.Returns the reason phrase for the stored status code, ornullif no reason is defined.static StringgetReason(int code) Returns the reason phrase for the given status code, ornullif no reason is defined.booleanReturnstrueif the stored code represents a client error (4xx).static booleanisClientError(int code) Returnstrueif the code represents a client error (4xx).booleanisError()Returnstrueif the stored code represents any error (4xx or 5xx).static booleanisError(int code) Returnstrueif the code represents any error (4xx or 5xx).booleanisInfo()Returnstrueif the stored code represents an informational response (1xx).static booleanisInfo(int code) Returnstrueif the code represents an informational response (1xx).booleanAlias forisInfo().static booleanisInformational(int code) Alias forisInfo(int).booleanReturnstrueif the stored code represents a redirect (3xx).static booleanisRedirect(int code) Returnstrueif the code represents a redirect (3xx).booleanReturnstrueif the stored code represents a server error (5xx).static booleanisServerError(int code) Returnstrueif the code represents a server error (5xx).booleanReturnstrueif the stored code represents a successful response (2xx).static booleanisSuccess(int code) Returnstrueif the code represents a successful response (2xx).booleanAlias forisSuccess().static booleanisSuccessful(int code) Alias forisSuccess(int).booleanisValid()Returnstrueif the stored code is a valid HTTP status code.static booleanisValid(int code) Returnstrueif the code is a valid HTTP status code.voidsetCode(int code) Sets the stored status code.
-
Constructor Details
-
StatusCode
public StatusCode()Creates a new StatusCode with no initial value. -
StatusCode
public StatusCode(int code) Creates a new StatusCode with the given value.- Parameters:
code- the status code- Since:
- 2.0.0
-
-
Method Details
-
getReason
Returns the reason phrase for the given status code, ornullif no reason is defined.- Parameters:
code- the status code- Returns:
- The reason, or
null - Since:
- 2.0.0
- See Also:
-
isClientError
public static boolean isClientError(int code) Returnstrueif the code represents a client error (4xx). Example:404 Not Found.- Parameters:
code- the status code- Returns:
trueif the status code is a client error,falseotherwise- Since:
- 2.0.0
-
isError
public static boolean isError(int code) Returnstrueif the code represents any error (4xx or 5xx).- Parameters:
code- the status code- Returns:
trueif the status code is an error,falseotherwise- Since:
- 2.0.0
-
isInfo
public static boolean isInfo(int code) Returnstrueif the code represents an informational response (1xx).- Parameters:
code- the status code- Returns:
trueif the status code is informational,falseotherwise- Since:
- 2.0.0
- See Also:
-
isInformational
public static boolean isInformational(int code) Alias forisInfo(int).- Parameters:
code- the status code- Returns:
trueif the status code is informational,false- Since:
- 2.0.0
- See Also:
-
isRedirect
public static boolean isRedirect(int code) Returnstrueif the code represents a redirect (3xx).- Parameters:
code- the status code- Returns:
trueif the status code is a redirect,falseotherwise- Since:
- 2.0.0
-
isServerError
public static boolean isServerError(int code) Returnstrueif the code represents a server error (5xx).- Parameters:
code- the status code- Returns:
trueif the status code is a server error,falseotherwise- Since:
- 2.0.0
-
isSuccess
public static boolean isSuccess(int code) Returnstrueif the code represents a successful response (2xx).- Parameters:
code- the status code- Returns:
trueif the status code is a success,falseotherwise- Since:
- 2.0.0
- See Also:
-
isSuccessful
public static boolean isSuccessful(int code) Alias forisSuccess(int).- Parameters:
code- the status code- Returns:
trueif the status code is a success,falseotherwise- Since:
- 2.0.0
- See Also:
-
isValid
public static boolean isValid(int code) Returnstrueif the code is a valid HTTP status code.- Parameters:
code- the status code- Returns:
trueif the status code is valid,falseotherwise
-
getCode
public int getCode()Returns the stored status code. -
setCode
public void setCode(int code) Sets the stored status code.- Parameters:
code- The HTTP status code
-
getReason
Returns the reason phrase for the stored status code, ornullif no reason is defined.- Returns:
- The reason, or
null - See Also:
-
isClientError
public boolean isClientError()Returnstrueif the stored code represents a client error (4xx).- Returns:
trueif the status code is a client error,falseotherwise
-
isError
public boolean isError()Returnstrueif the stored code represents any error (4xx or 5xx).- Returns:
trueif the status code is an error,falseotherwise
-
isInfo
public boolean isInfo()Returnstrueif the stored code represents an informational response (1xx).- Returns:
trueif the status code is informational,falseotherwise
-
isInformational
public boolean isInformational()Alias forisInfo().- Returns:
trueif the status code is informational,falseotherwise- Since:
- 2.0.0
-
isRedirect
public boolean isRedirect()Returnstrueif the stored code represents a redirect (3xx).- Returns:
trueif the status code is a redirect,falseotherwise
-
isServerError
public boolean isServerError()Returnstrueif the stored code represents a server error (5xx).- Returns:
trueif the status code is a server error,falseotherwise
-
isSuccess
public boolean isSuccess()Returnstrueif the stored code represents a successful response (2xx).- Returns:
trueif the status code is a success,falseotherwise
-
isSuccessful
public boolean isSuccessful()Alias forisSuccess().- Returns:
trueif the status code is a success,falseotherwise- Since:
- 2.0.0
-
isValid
public boolean isValid()Returnstrueif the stored code is a valid HTTP status code.- Returns:
trueif the status code is valid,falseotherwise
-