Class StatusCode

java.lang.Object
net.thauvin.erik.httpstatus.StatusCode
All Implemented Interfaces:
Serializable

public class StatusCode extends Object implements 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

    Constructors
    Constructor
    Description
    Creates a new StatusCode with no initial value.
    StatusCode(int code)
    Creates a new StatusCode with the given value.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the stored status code.
    Returns the reason phrase for the stored status code, or null if no reason is defined.
    static String
    getReason(int code)
    Returns the reason phrase for the given status code, or null if no reason is defined.
    boolean
    Returns true if the stored code represents a client error (4xx).
    static boolean
    isClientError(int code)
    Returns true if the code represents a client error (4xx).
    boolean
    Returns true if the stored code represents any error (4xx or 5xx).
    static boolean
    isError(int code)
    Returns true if the code represents any error (4xx or 5xx).
    boolean
    Returns true if the stored code represents an informational response (1xx).
    static boolean
    isInfo(int code)
    Returns true if the code represents an informational response (1xx).
    boolean
    Alias for isInfo().
    static boolean
    isInformational(int code)
    Alias for isInfo(int).
    boolean
    Returns true if the stored code represents a redirect (3xx).
    static boolean
    isRedirect(int code)
    Returns true if the code represents a redirect (3xx).
    boolean
    Returns true if the stored code represents a server error (5xx).
    static boolean
    isServerError(int code)
    Returns true if the code represents a server error (5xx).
    boolean
    Returns true if the stored code represents a successful response (2xx).
    static boolean
    isSuccess(int code)
    Returns true if the code represents a successful response (2xx).
    boolean
    Alias for isSuccess().
    static boolean
    isSuccessful(int code)
    Alias for isSuccess(int).
    boolean
    Returns true if the stored code is a valid HTTP status code.
    static boolean
    isValid(int code)
    Returns true if the code is a valid HTTP status code.
    void
    setCode(int code)
    Sets the stored status code.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static String getReason(int code)
      Returns the reason phrase for the given status code, or null if 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)
      Returns true if the code represents a client error (4xx). Example: 404 Not Found.
      Parameters:
      code - the status code
      Returns:
      true if the status code is a client error, false otherwise
      Since:
      2.0.0
    • isError

      public static boolean isError(int code)
      Returns true if the code represents any error (4xx or 5xx).
      Parameters:
      code - the status code
      Returns:
      true if the status code is an error, false otherwise
      Since:
      2.0.0
    • isInfo

      public static boolean isInfo(int code)
      Returns true if the code represents an informational response (1xx).
      Parameters:
      code - the status code
      Returns:
      true if the status code is informational, false otherwise
      Since:
      2.0.0
      See Also:
    • isInformational

      public static boolean isInformational(int code)
      Alias for isInfo(int).
      Parameters:
      code - the status code
      Returns:
      true if the status code is informational, false
      Since:
      2.0.0
      See Also:
    • isRedirect

      public static boolean isRedirect(int code)
      Returns true if the code represents a redirect (3xx).
      Parameters:
      code - the status code
      Returns:
      true if the status code is a redirect, false otherwise
      Since:
      2.0.0
    • isServerError

      public static boolean isServerError(int code)
      Returns true if the code represents a server error (5xx).
      Parameters:
      code - the status code
      Returns:
      true if the status code is a server error, false otherwise
      Since:
      2.0.0
    • isSuccess

      public static boolean isSuccess(int code)
      Returns true if the code represents a successful response (2xx).
      Parameters:
      code - the status code
      Returns:
      true if the status code is a success, false otherwise
      Since:
      2.0.0
      See Also:
    • isSuccessful

      public static boolean isSuccessful(int code)
      Alias for isSuccess(int).
      Parameters:
      code - the status code
      Returns:
      true if the status code is a success, false otherwise
      Since:
      2.0.0
      See Also:
    • isValid

      public static boolean isValid(int code)
      Returns true if the code is a valid HTTP status code.
      Parameters:
      code - the status code
      Returns:
      true if the status code is valid, false otherwise
    • 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

      public String getReason()
      Returns the reason phrase for the stored status code, or null if no reason is defined.
      Returns:
      The reason, or null
      See Also:
    • isClientError

      public boolean isClientError()
      Returns true if the stored code represents a client error (4xx).
      Returns:
      true if the status code is a client error, false otherwise
    • isError

      public boolean isError()
      Returns true if the stored code represents any error (4xx or 5xx).
      Returns:
      true if the status code is an error, false otherwise
    • isInfo

      public boolean isInfo()
      Returns true if the stored code represents an informational response (1xx).
      Returns:
      true if the status code is informational, false otherwise
    • isInformational

      public boolean isInformational()
      Alias for isInfo().
      Returns:
      true if the status code is informational, false otherwise
      Since:
      2.0.0
    • isRedirect

      public boolean isRedirect()
      Returns true if the stored code represents a redirect (3xx).
      Returns:
      true if the status code is a redirect, false otherwise
    • isServerError

      public boolean isServerError()
      Returns true if the stored code represents a server error (5xx).
      Returns:
      true if the status code is a server error, false otherwise
    • isSuccess

      public boolean isSuccess()
      Returns true if the stored code represents a successful response (2xx).
      Returns:
      true if the status code is a success, false otherwise
    • isSuccessful

      public boolean isSuccessful()
      Alias for isSuccess().
      Returns:
      true if the status code is a success, false otherwise
      Since:
      2.0.0
    • isValid

      public boolean isValid()
      Returns true if the stored code is a valid HTTP status code.
      Returns:
      true if the status code is valid, false otherwise