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.Collection;
12 import java.util.Collections;
14 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
15 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
17 import com.google.common.collect.ImmutableList;
20 * A builder for creating RpcResult instances.
22 * @author Thomas Pantelis
24 * @param <T> the result value type
26 public final class RpcResultBuilder<T> {
28 private static class RpcResultImpl<T> implements RpcResult<T> {
30 private final Collection<RpcError> errors;
31 private final T result;
32 private final boolean successful;
34 RpcResultImpl( boolean successful, T result,
35 Collection<RpcError> errors ) {
36 this.successful = successful;
42 public Collection<RpcError> getErrors() {
47 public T getResult() {
52 public boolean isSuccessful() {
57 public String toString(){
58 return "RpcResult [successful=" + successful + ", result="
59 + result + ", errors=" + errors + "]";
63 private static class RpcErrorImpl implements RpcError {
65 private final String applicationTag;
66 private final String tag;
67 private final String info;
68 private final ErrorSeverity severity;
69 private final String message;
70 private final ErrorType errorType;
71 private final Throwable cause;
73 RpcErrorImpl( ErrorSeverity severity, ErrorType errorType,
74 String tag, String message, String applicationTag, String info,
76 this.severity = severity;
77 this.errorType = errorType;
79 this.message = message;
80 this.applicationTag = applicationTag;
86 public String getApplicationTag() {
87 return applicationTag;
91 public String getTag() {
96 public String getInfo() {
101 public ErrorSeverity getSeverity() {
106 public String getMessage(){
111 public ErrorType getErrorType() {
116 public Throwable getCause() {
121 public String toString(){
122 return "RpcError [message=" + message + ", severity="
123 + severity + ", errorType=" + errorType + ", tag=" + tag
124 + ", applicationTag=" + applicationTag + ", info=" + info
125 + ", cause=" + cause + "]";
129 private ImmutableList.Builder<RpcError> errors;
131 private final boolean successful;
133 private RpcResultBuilder( boolean successful, T result ) {
134 this.successful = successful;
135 this.result = result;
139 * Returns a builder for a successful result.
141 public static <T> RpcResultBuilder<T> success() {
142 return new RpcResultBuilder<T>( true, null );
146 * Returns a builder for a successful result.
148 * @param result the result value
150 public static <T> RpcResultBuilder<T> success( T result ) {
151 return new RpcResultBuilder<T>( true, result );
155 * Returns a builder for a failed result.
157 public static <T> RpcResultBuilder<T> failed() {
158 return new RpcResultBuilder<T>( false, null );
162 * Returns a builder based on the given status.
164 * @param success true if successful, false otherwise.
166 public static <T> RpcResultBuilder<T> status( boolean success ) {
167 return new RpcResultBuilder<T>( success, null );
171 * Returns a builder from another RpcResult.
173 * @param other the other RpcResult.
175 public static <T> RpcResultBuilder<T> from( RpcResult<T> other ) {
176 return new RpcResultBuilder<T>( other.isSuccessful(), other.getResult() )
177 .withRpcErrors( other.getErrors() );
181 * Creates an RpcError with severity ERROR for reuse.
183 * @param errorType the conceptual layer at which the error occurred.
184 * @param tag a short string that identifies the general type of error condition. See
185 * {@link RpcError#getTag} for a list of suggested values.
186 * @param message a string suitable for human display that describes the error condition.
188 * @return an RpcError
190 public static RpcError newError( ErrorType errorType, String tag, String message ) {
191 return new RpcErrorImpl( ErrorSeverity.ERROR, errorType, tag, message, null, null, null );
195 * Creates an RpcError with severity ERROR for reuse.
197 * @param errorType the conceptual layer at which the error occurred.
198 * @param tag a short string that identifies the general type of error condition. See
199 * {@link RpcError#getTag} for a list of suggested values.
200 * @param message a string suitable for human display that describes the error condition.
201 * * @param applicationTag a short string that identifies the specific type of error condition.
202 * @param info a string containing additional information to provide extended
203 * and/or implementation-specific debugging information.
204 * @param cause the exception that triggered the error.
206 * @return an RpcError
208 public static RpcError newError( ErrorType errorType, String tag, String message,
209 String applicationTag, String info, Throwable cause ) {
210 return new RpcErrorImpl( ErrorSeverity.ERROR, errorType, tag, message,
211 applicationTag, info, cause );
215 * Creates an RpcError with severity WARNING for reuse.
217 * @param errorType the conceptual layer at which the warning occurred.
218 * @param tag a short string that identifies the general type of warning condition. See
219 * {@link RpcError#getTag} for a list of suggested values.
220 * @param message a string suitable for human display that describes the warning condition.
222 * @return an RpcError
224 public static RpcError newWarning( ErrorType errorType, String tag, String message ) {
225 return new RpcErrorImpl( ErrorSeverity.WARNING, errorType, tag, message, null, null, null );
229 * Creates an RpcError with severity WARNING for reuse.
231 * @param errorType the conceptual layer at which the warning occurred.
232 * @param tag a short string that identifies the general type of warning condition. See
233 * {@link RpcError#getTag} for a list of suggested values.
234 * @param message a string suitable for human display that describes the warning condition.
235 * * @param applicationTag a short string that identifies the specific type of warning condition.
236 * @param info a string containing additional information to provide extended
237 * and/or implementation-specific debugging information.
238 * @param cause the exception that triggered the warning.
240 * @return an RpcError
242 public static RpcError newWarning( ErrorType errorType, String tag, String message,
243 String applicationTag, String info, Throwable cause ) {
244 return new RpcErrorImpl( ErrorSeverity.WARNING, errorType, tag, message,
245 applicationTag, info, cause );
249 * Sets the value of the result.
251 * @param result the result value
253 public RpcResultBuilder<T> withResult( T result ) {
254 this.result = result;
258 private void addError( ErrorSeverity severity, ErrorType errorType,
259 String tag, String message, String applicationTag, String info,
262 addError( new RpcErrorImpl( severity, errorType,
263 tag != null ? tag : "operation-failed", message,
264 applicationTag, info, cause ) );
267 private void addError( RpcError error ) {
269 if( errors == null ) {
270 errors = new ImmutableList.Builder<RpcError>();
277 * Adds a warning to the result.
279 * @param errorType the conceptual layer at which the warning occurred.
280 * @param tag a short string that identifies the general type of warning condition. See
281 * {@link RpcError#getTag} for a list of suggested values.
282 * @param message a string suitable for human display that describes the warning condition.
284 public RpcResultBuilder<T> withWarning( ErrorType errorType, String tag, String message ) {
285 addError( ErrorSeverity.WARNING, errorType, tag, message, null, null, null );
290 * Adds a warning to the result.
292 * @param errorType the conceptual layer at which the warning occurred.
293 * @param tag a short string that identifies the general type of warning condition. See
294 * {@link RpcError#getTag} for a list of suggested values.
295 * @param message a string suitable for human display that describes the warning condition.
296 * @param applicationTag a short string that identifies the specific type of warning condition.
297 * @param info a string containing additional information to provide extended
298 * and/or implementation-specific debugging information.
299 * @param cause the exception that triggered the warning.
301 public RpcResultBuilder<T> withWarning( ErrorType errorType, String tag, String message,
302 String applicationTag, String info, Throwable cause ) {
303 addError( ErrorSeverity.WARNING, errorType, tag, message, applicationTag, info, cause );
308 * Adds an error to the result. The general error tag defaults to "operation-failed".
310 * @param errorType the conceptual layer at which the error occurred.
311 * @param message a string suitable for human display that describes the error condition.
313 public RpcResultBuilder<T> withError( ErrorType errorType, String message ) {
314 addError( ErrorSeverity.ERROR, errorType, null, message, null, null, null );
319 * Adds an error to the result.
321 * @param errorType the conceptual layer at which the error occurred.
322 * @param tag a short string that identifies the general type of error condition. See
323 * {@link RpcError#getTag} for a list of suggested values.
324 * @param message a string suitable for human display that describes the error condition.
326 public RpcResultBuilder<T> withError( ErrorType errorType, String tag, String message ) {
327 addError( ErrorSeverity.ERROR, errorType, tag, message, null, null, null );
332 * Adds an error to the result. The general error tag defaults to "operation-failed".
334 * @param errorType the conceptual layer at which the error occurred.
335 * @param message a string suitable for human display that describes the error condition.
336 * @param cause the exception that triggered the error.
338 public RpcResultBuilder<T> withError( ErrorType errorType, String message,
340 addError( ErrorSeverity.ERROR, errorType, null, message, null, null, cause );
345 * Adds an error to the result.
347 * @param errorType the conceptual layer at which the error occurred.
348 * @param tag a short string that identifies the general type of error condition. See
349 * {@link RpcError#getTag} for a list of suggested values.
350 * @param message a string suitable for human display that describes the error condition.
351 * @param applicationTag a short string that identifies the specific type of error condition.
352 * @param info a string containing additional information to provide extended
353 * and/or implementation-specific debugging information.
354 * @param cause the exception that triggered the error.
356 public RpcResultBuilder<T> withError( ErrorType errorType, String tag, String message,
357 String applicationTag, String info, Throwable cause ) {
358 addError( ErrorSeverity.ERROR, errorType, tag, message, applicationTag, info, cause );
365 * @param error the RpcError
367 public RpcResultBuilder<T> withRpcError( RpcError error ) {
375 * @param errors the list of RpcErrors
377 public RpcResultBuilder<T> withRpcErrors( Collection<RpcError> errors ) {
378 if( errors != null ) {
379 for( RpcError error: errors ) {
386 public RpcResult<T> build() {
388 return new RpcResultImpl<T>( successful, result,
389 errors != null ? errors.build() : Collections.<RpcError>emptyList() );