2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.yang.common;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
15 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
16 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
19 * A builder for creating RpcResult instances.
21 * @author Thomas Pantelis
23 * @param <T> the result value type
25 public class RpcResultBuilder<T> {
27 private static class RpcResultImpl<T> implements RpcResult<T> {
29 private final Collection<RpcError> errors;
30 private final T result;
31 private final boolean successful;
33 RpcResultImpl( boolean successful, T result,
34 Collection<RpcError> errors ) {
35 this.successful = successful;
41 public Collection<RpcError> getErrors() {
46 public T getResult() {
51 public boolean isSuccessful() {
56 public String toString(){
57 return "RpcResult [successful=" + successful + ", result="
58 + result + ", errors=" + errors + "]";
62 private static class RpcErrorImpl implements RpcError {
64 private final String applicationTag;
65 private final String tag;
66 private final String info;
67 private final ErrorSeverity severity;
68 private final String message;
69 private final ErrorType errorType;
70 private final Throwable cause;
72 RpcErrorImpl( ErrorSeverity severity, ErrorType errorType,
73 String tag, String message, String applicationTag, String info,
75 this.severity = severity;
76 this.errorType = errorType;
78 this.message = message;
79 this.applicationTag = applicationTag;
85 public String getApplicationTag() {
86 return applicationTag;
90 public String getTag() {
95 public String getInfo() {
100 public ErrorSeverity getSeverity() {
105 public String getMessage(){
110 public ErrorType getErrorType() {
115 public Throwable getCause() {
120 public String toString(){
121 return "RpcError [message=" + message + ", severity="
122 + severity + ", errorType=" + errorType + ", tag=" + tag
123 + ", applicationTag=" + applicationTag + ", info=" + info
124 + ", cause=" + cause + "]";
128 private Collection<RpcError> errors;
130 private final boolean successful;
132 private RpcResultBuilder( boolean successful, T result ) {
133 this.successful = successful;
134 this.result = result;
138 * Returns a builder for a successful result.
140 public static <T> RpcResultBuilder<T> success() {
141 return new RpcResultBuilder<T>( true, null );
145 * Returns a builder for a successful result.
147 * @param result the result value
149 public static <T> RpcResultBuilder<T> success( T result ) {
150 return new RpcResultBuilder<T>( true, result );
154 * Returns a builder for a failed result.
156 public static <T> RpcResultBuilder<T> failed() {
157 return new RpcResultBuilder<T>( false, null );
161 * Sets the value of the result.
163 * @param result the result value
165 public RpcResultBuilder<T> withResult( T result ) {
166 this.result = result;
170 private void addError( ErrorSeverity severity, ErrorType errorType,
171 String tag, String message, String applicationTag, String info,
174 if( errors == null ) {
175 errors = new ArrayList<>();
178 errors.add( new RpcErrorImpl( severity, errorType, tag, message,
179 applicationTag, info, cause ) );
183 * Adds a warning to the result.
185 * @param errorType the conceptual layer at which the warning occurred.
186 * @param tag a short string that identifies the general type of warning condition. See
187 * {@link RpcError#getTag} for a list of suggested values.
188 * @param message a string suitable for human display that describes the warning condition.
190 public RpcResultBuilder<T> withWarning( ErrorType errorType, String tag, String message ) {
191 addError( ErrorSeverity.WARNING, errorType, tag, message, null, null, null );
196 * Adds a warning to the result.
198 * @param errorType the conceptual layer at which the warning occurred.
199 * @param tag a short string that identifies the general type of warning condition. See
200 * {@link RpcError#getTag} for a list of suggested values.
201 * @param message a string suitable for human display that describes the warning condition.
202 * @param applicationTag a short string that identifies the specific type of warning condition.
203 * @param info a string containing additional information to provide extended
204 * and/or implementation-specific debugging information.
205 * @param cause the exception that triggered the warning.
207 public RpcResultBuilder<T> withWarning( ErrorType errorType, String tag, String message,
208 String applicationTag, String info, Throwable cause ) {
209 addError( ErrorSeverity.WARNING, errorType, tag, message, applicationTag, info, cause );
214 * Adds an error to the result. The general error tag defaults to "operation-failed".
216 * @param errorType the conceptual layer at which the error occurred.
217 * @param message a string suitable for human display that describes the error condition.
219 public RpcResultBuilder<T> withError( ErrorType errorType, String message ) {
220 addError( ErrorSeverity.ERROR, errorType, "operation-failed", message, null, null, null );
225 * Adds an error to the result.
227 * @param errorType the conceptual layer at which the error occurred.
228 * @param tag a short string that identifies the general type of error condition. See
229 * {@link RpcError#getTag} for a list of suggested values.
230 * @param message a string suitable for human display that describes the error condition.
232 public RpcResultBuilder<T> withError( ErrorType errorType, String tag, String message ) {
233 addError( ErrorSeverity.ERROR, errorType, tag, message, null, null, null );
238 * Adds an error to the result.
240 * @param errorType the conceptual layer at which the error occurred.
241 * @param tag a short string that identifies the general type of error condition. See
242 * {@link RpcError#getTag} for a list of suggested values.
243 * @param message a string suitable for human display that describes the error condition.
244 * @param applicationTag a short string that identifies the specific type of error condition.
245 * @param info a string containing additional information to provide extended
246 * and/or implementation-specific debugging information.
247 * @param cause the exception that triggered the error.
249 public RpcResultBuilder<T> withError( ErrorType errorType, String tag, String message,
250 String applicationTag, String info, Throwable cause ) {
251 addError( ErrorSeverity.ERROR, errorType, tag, message, applicationTag, info, cause );
255 public RpcResult<T> build() {
257 return new RpcResultImpl<T>( successful, result,
258 errors != null ? errors : Collections.<RpcError>emptyList() );