Skip to content

Refactor: Use HttpStatus.Series instead of magic value. #34266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* Default implementation of {@link HttpStatusCode}.
*
* @author Arjen Poutsma
* @author Mengqi Xu
* @since 6.0
*/
final class DefaultHttpStatusCode implements HttpStatusCode, Comparable<HttpStatusCode>, Serializable {
Expand All @@ -32,9 +33,12 @@ final class DefaultHttpStatusCode implements HttpStatusCode, Comparable<HttpStat

private final int value;

private final HttpStatus.Series series;


public DefaultHttpStatusCode(int value) {
this.value = value;
this.series = HttpStatus.Series.resolve(value);
}

@Override
Expand All @@ -44,37 +48,40 @@ public int value() {

@Override
public boolean is1xxInformational() {
return hundreds() == 1;
return series() == HttpStatus.Series.INFORMATIONAL;
}

@Override
public boolean is2xxSuccessful() {
return hundreds() == 2;
return series() == HttpStatus.Series.SUCCESSFUL;
}

@Override
public boolean is3xxRedirection() {
return hundreds() == 3;
return series() == HttpStatus.Series.REDIRECTION;
}

@Override
public boolean is4xxClientError() {
return hundreds() == 4;
return series() == HttpStatus.Series.CLIENT_ERROR;
}

@Override
public boolean is5xxServerError() {
return hundreds() == 5;
return series() == HttpStatus.Series.SERVER_ERROR;
}

@Override
public boolean isError() {
int hundreds = hundreds();
return hundreds == 4 || hundreds == 5;
return (is4xxClientError() || is5xxServerError());
}

private int hundreds() {
return this.value / 100;
/**
* Return the HTTP status series of this status code.
* @see HttpStatus.Series
*/
private HttpStatus.Series series() {
return this.series;
}


Expand Down
Loading