Java tutorial
/* * Copyright 2012 Stormpath, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.novation.eligibility.rest.spring.web.servlet.handler; import org.springframework.http.HttpStatus; import org.springframework.util.ObjectUtils; /** * @author Les Hazlewood */ public class RestError { private final HttpStatus status; private final int code; private final String message; private final String developerMessage; private final String moreInfoUrl; private final Throwable throwable; public RestError(HttpStatus status, int code, String message, String developerMessage, String moreInfoUrl, Throwable throwable) { if (status == null) { throw new NullPointerException("HttpStatus argument cannot be null."); } this.status = status; this.code = code; this.message = message; this.developerMessage = developerMessage; this.moreInfoUrl = moreInfoUrl; this.throwable = throwable; } public HttpStatus getStatus() { return status; } public int getCode() { return code; } public String getMessage() { return message; } public String getDeveloperMessage() { return developerMessage; } public String getMoreInfoUrl() { return moreInfoUrl; } public Throwable getThrowable() { return throwable; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o instanceof RestError) { RestError re = (RestError) o; return ObjectUtils.nullSafeEquals(getStatus(), re.getStatus()) && getCode() == re.getCode() && ObjectUtils.nullSafeEquals(getMessage(), re.getMessage()) && ObjectUtils.nullSafeEquals(getDeveloperMessage(), re.getDeveloperMessage()) && ObjectUtils.nullSafeEquals(getMoreInfoUrl(), re.getMoreInfoUrl()) && ObjectUtils.nullSafeEquals(getThrowable(), re.getThrowable()); } return false; } @Override public int hashCode() { //noinspection ThrowableResultOfMethodCallIgnored return ObjectUtils.nullSafeHashCode(new Object[] { getStatus(), getCode(), getMessage(), getDeveloperMessage(), getMoreInfoUrl(), getThrowable() }); } public String toString() { //noinspection StringBufferReplaceableByString return new StringBuilder().append(getStatus().value()).append(" (").append(getStatus().getReasonPhrase()) .append(" )").toString(); } public static class Builder { private HttpStatus status; private int code; private String message; private String developerMessage; private String moreInfoUrl; private Throwable throwable; public Builder() { } public Builder setStatus(int statusCode) { this.status = HttpStatus.valueOf(statusCode); return this; } public Builder setStatus(HttpStatus status) { this.status = status; return this; } public Builder setCode(int code) { this.code = code; return this; } public Builder setMessage(String message) { this.message = message; return this; } public Builder setDeveloperMessage(String developerMessage) { this.developerMessage = developerMessage; return this; } public Builder setMoreInfoUrl(String moreInfoUrl) { this.moreInfoUrl = moreInfoUrl; return this; } public Builder setThrowable(Throwable throwable) { this.throwable = throwable; return this; } public RestError build() { if (this.status == null) { this.status = HttpStatus.INTERNAL_SERVER_ERROR; } return new RestError(this.status, this.code, this.message, this.developerMessage, this.moreInfoUrl, this.throwable); } } }