Merge "BUG-1276: fixed generated union constructor"
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / RpcResultBuilder.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.common;
10
11 import java.util.Collection;
12 import java.util.Collections;
13
14 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
15 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
16
17 import com.google.common.collect.ImmutableList;
18
19 /**
20  * A builder for creating RpcResult instances.
21  *
22  * @author Thomas Pantelis
23  *
24  * @param <T> the result value type
25  */
26 public final class RpcResultBuilder<T> {
27
28     private static class RpcResultImpl<T> implements RpcResult<T> {
29
30         private final Collection<RpcError> errors;
31         private final T result;
32         private final boolean successful;
33
34         RpcResultImpl( boolean successful, T result,
35                        Collection<RpcError> errors ) {
36             this.successful = successful;
37             this.result = result;
38             this.errors = errors;
39         }
40
41         @Override
42         public Collection<RpcError> getErrors() {
43             return errors;
44         }
45
46         @Override
47         public T getResult() {
48             return result;
49         }
50
51         @Override
52         public boolean isSuccessful() {
53             return successful;
54         }
55
56         @Override
57         public String toString(){
58             return "RpcResult [successful=" + successful + ", result="
59                     + result + ", errors=" + errors + "]";
60         }
61     }
62
63     private static class RpcErrorImpl implements RpcError {
64
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;
72
73         RpcErrorImpl( ErrorSeverity severity, ErrorType errorType,
74                 String tag, String message, String applicationTag, String info,
75                 Throwable cause ) {
76             this.severity = severity;
77             this.errorType = errorType;
78             this.tag = tag;
79             this.message = message;
80             this.applicationTag = applicationTag;
81             this.info = info;
82             this.cause = cause;
83         }
84
85         @Override
86         public String getApplicationTag() {
87             return applicationTag;
88         }
89
90         @Override
91         public String getTag() {
92             return tag;
93         }
94
95         @Override
96         public String getInfo() {
97             return info;
98         }
99
100         @Override
101         public ErrorSeverity getSeverity() {
102             return severity;
103         }
104
105         @Override
106         public String getMessage(){
107             return message;
108         }
109
110         @Override
111         public ErrorType getErrorType() {
112             return errorType;
113         }
114
115         @Override
116         public Throwable getCause() {
117             return cause;
118         }
119
120         @Override
121         public String toString(){
122             return "RpcError [message=" + message + ", severity="
123                     + severity + ", errorType=" + errorType + ", tag=" + tag
124                     + ", applicationTag=" + applicationTag + ", info=" + info
125                     + ", cause=" + cause + "]";
126         }
127     }
128
129     private ImmutableList.Builder<RpcError> errors;
130     private T result;
131     private final boolean successful;
132
133     private RpcResultBuilder( boolean successful, T result ) {
134         this.successful = successful;
135         this.result = result;
136     }
137
138     /**
139      * Returns a builder for a successful result.
140      */
141     public static <T> RpcResultBuilder<T> success() {
142         return new RpcResultBuilder<T>( true, null );
143     }
144
145     /**
146      * Returns a builder for a successful result.
147      *
148      * @param result the result value
149      */
150     public static <T> RpcResultBuilder<T> success( T result ) {
151          return new RpcResultBuilder<T>( true, result );
152     }
153
154     /**
155      * Returns a builder for a failed result.
156      */
157     public static <T> RpcResultBuilder<T> failed() {
158         return new RpcResultBuilder<T>( false, null );
159     }
160
161     /**
162      * Returns a builder based on the given status.
163      *
164      * @param success true if successful, false otherwise.
165      */
166     public static <T> RpcResultBuilder<T> status( boolean success ) {
167         return new RpcResultBuilder<T>( success, null );
168     }
169
170     /**
171      * Returns a builder from another RpcResult.
172      *
173      * @param other the other RpcResult.
174      */
175     public static <T> RpcResultBuilder<T> from( RpcResult<T> other ) {
176         return new RpcResultBuilder<T>( other.isSuccessful(), other.getResult() )
177                                                       .withRpcErrors( other.getErrors() );
178     }
179
180     /**
181      * Creates an RpcError with severity ERROR for reuse.
182      *
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.
187      *
188      * @return an RpcError
189      */
190     public static RpcError newError( ErrorType errorType, String tag, String message ) {
191         return new RpcErrorImpl( ErrorSeverity.ERROR, errorType, tag, message, null, null, null );
192     }
193
194     /**
195      * Creates an RpcError with severity ERROR for reuse.
196      *
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.
205      *
206      * @return an RpcError
207      */
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 );
212     }
213
214     /**
215      * Creates an RpcError with severity WARNING for reuse.
216      *
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.
221      *
222      * @return an RpcError
223      */
224     public static RpcError newWarning( ErrorType errorType, String tag, String message ) {
225         return new RpcErrorImpl( ErrorSeverity.WARNING, errorType, tag, message, null, null, null );
226     }
227
228     /**
229      * Creates an RpcError with severity WARNING for reuse.
230      *
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.
239      *
240      * @return an RpcError
241      */
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 );
246     }
247
248     /**
249      * Sets the value of the result.
250      *
251      * @param result the result value
252      */
253     public RpcResultBuilder<T> withResult( T result ) {
254         this.result = result;
255         return this;
256     }
257
258     private void addError( ErrorSeverity severity, ErrorType errorType,
259             String tag, String message, String applicationTag, String info,
260             Throwable cause ) {
261
262         addError( new RpcErrorImpl( severity, errorType,
263                                     tag != null ? tag : "operation-failed", message,
264                                     applicationTag, info, cause ) );
265     }
266
267     private void addError( RpcError error ) {
268
269         if( errors == null ) {
270             errors = new ImmutableList.Builder<RpcError>();
271         }
272
273         errors.add( error );
274     }
275
276     /**
277      * Adds a warning to the result.
278      *
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.
283      */
284     public RpcResultBuilder<T> withWarning( ErrorType errorType, String tag, String message ) {
285         addError( ErrorSeverity.WARNING, errorType, tag, message, null, null, null );
286         return this;
287     }
288
289     /**
290      * Adds a warning to the result.
291      *
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.
300      */
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 );
304         return this;
305     }
306
307     /**
308      * Adds an error to the result. The general error tag defaults to "operation-failed".
309      *
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.
312      */
313     public RpcResultBuilder<T> withError( ErrorType errorType, String message ) {
314         addError( ErrorSeverity.ERROR, errorType, null, message, null, null, null );
315         return this;
316     }
317
318     /**
319      * Adds an error to the result.
320      *
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.
325      */
326     public RpcResultBuilder<T> withError( ErrorType errorType, String tag, String message ) {
327         addError( ErrorSeverity.ERROR, errorType, tag, message, null, null, null );
328         return this;
329     }
330
331     /**
332      * Adds an error to the result. The general error tag defaults to "operation-failed".
333      *
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.
337      */
338     public RpcResultBuilder<T> withError( ErrorType errorType, String message,
339                                           Throwable cause ) {
340         addError( ErrorSeverity.ERROR, errorType, null, message, null, null, cause );
341         return this;
342     }
343
344     /**
345      * Adds an error to the result.
346      *
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.
355      */
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 );
359         return this;
360     }
361
362     /**
363      * Adds an RpcError.
364      *
365      * @param error the RpcError
366      */
367     public RpcResultBuilder<T> withRpcError( RpcError error ) {
368         addError( error );
369         return this;
370     }
371
372     /**
373      * Adds RpcErrors.
374      *
375      * @param errors the list of RpcErrors
376      */
377     public RpcResultBuilder<T> withRpcErrors( Collection<RpcError> errors ) {
378         if( errors != null ) {
379             for( RpcError error: errors ) {
380                 addError( error );
381             }
382         }
383         return this;
384     }
385
386     public RpcResult<T> build() {
387
388         return new RpcResultImpl<T>( successful, result,
389                 errors != null ? errors.build() : Collections.<RpcError>emptyList() );
390     }
391 }