package ${domain};

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * ApiResponse - 基类
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
public class ApiResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private List<Error> errors = new ArrayList();

    public void setErrors(List<Error> errors) {
        this.errors = errors;
    }

    public List<Error> getErrors() {
        return new ArrayList<Error>(errors);
    }

    public Error peepError() {
        if (errors.size() > 0) {
        return new Error(errors.get(0));
    }
        return null;
    }

    public void addErrors(List<Error> errors) {
        this.errors = errors;
    }

    public void addError(ErrorType type, String message) {
        Error error = new Error();
        error.setType(type);
        error.setMessage(message);
        this.addError(error);
    }

    public void addError(Error error) {
        this.errors.add(error);
    }

    public boolean hasError() {
        return this.errors != null && this.errors.size() > 0;
    }
}
