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.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14
15 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
16 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
17
18 /**
19  * A builder for creating RpcResult instances.
20  *
21  * @author Thomas Pantelis
22  *
23  * @param <T> the result value type
24  */
25 public class RpcResultBuilder<T> {
26
27     private static class RpcResultImpl<T> implements RpcResult<T> {
28
29         private final Collection<RpcError> errors;
30         private final T result;
31         private final boolean successful;
32
33         RpcResultImpl( boolean successful, T result,
34                        Collection<RpcError> errors ) {
35             this.successful = successful;
36             this.result = result;
37             this.errors = errors;
38         }
39
40         @Override
41         public Collection<RpcError> getErrors() {
42             return errors;
43         }
44
45         @Override
46         public T getResult() {
47             return result;
48         }
49
50         @Override
51         public boolean isSuccessful() {
52             return successful;
53         }
54
55         @Override
56         public String toString(){
57             return "RpcResult [successful=" + successful + ", result="
58                     + result + ", errors=" + errors + "]";
59         }
60     }
61
62     private static class RpcErrorImpl implements RpcError {
63
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;
71
72         RpcErrorImpl( ErrorSeverity severity, ErrorType errorType,
73                 String tag, String message, String applicationTag, String info,
74                 Throwable cause ) {
75             this.severity = severity;
76             this.errorType = errorType;
77             this.tag = tag;
78             this.message = message;
79             this.applicationTag = applicationTag;
80             this.info = info;
81             this.cause = cause;
82         }
83
84         @Override
85         public String getApplicationTag() {
86             return applicationTag;
87         }
88
89         @Override
90         public String getTag() {
91             return tag;
92         }
93
94         @Override
95         public String getInfo() {
96             return info;
97         }
98
99         @Override
100         public ErrorSeverity getSeverity() {
101             return severity;
102         }
103
104         @Override
105         public String getMessage(){
106             return message;
107         }
108
109         @Override
110         public ErrorType getErrorType() {
111             return errorType;
112         }
113
114         @Override
115         public Throwable getCause() {
116             return cause;
117         }
118
119         @Override
120         public String toString(){
121             return "RpcError [message=" + message + ", severity="
122                     + severity + ", errorType=" + errorType + ", tag=" + tag
123                     + ", applicationTag=" + applicationTag + ", info=" + info
124                     + ", cause=" + cause + "]";
125         }
126     }
127
128     private Collection<RpcError> errors;
129     private T result;
130     private final boolean successful;
131
132     private RpcResultBuilder( boolean successful, T result ) {
133         this.successful = successful;
134         this.result = result;
135     }
136
137     /**
138      * Returns a builder for a successful result.
139      */
140     public static <T> RpcResultBuilder<T> success() {
141         return new RpcResultBuilder<T>( true, null );
142     }
143
144     /**
145      * Returns a builder for a successful result.
146      *
147      * @param result the result value
148      */
149     public static <T> RpcResultBuilder<T> success( T result ) {
150          return new RpcResultBuilder<T>( true, result );
151     }
152
153     /**
154      * Returns a builder for a failed result.
155      */
156     public static <T> RpcResultBuilder<T> failed() {
157         return new RpcResultBuilder<T>( false, null );
158     }
159
160     /**
161      * Sets the value of the result.
162      *
163      * @param result the result value
164      */
165     public RpcResultBuilder<T> withResult( T result ) {
166         this.result = result;
167         return this;
168     }
169
170     private void addError( ErrorSeverity severity, ErrorType errorType,
171             String tag, String message, String applicationTag, String info,
172             Throwable cause ) {
173
174         if( errors == null ) {
175             errors = new ArrayList<>();
176         }
177
178         errors.add( new RpcErrorImpl( severity, errorType, tag, message,
179                                       applicationTag, info, cause ) );
180     }
181
182     /**
183      * Adds a warning to the result.
184      *
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.
189      */
190     public RpcResultBuilder<T> withWarning( ErrorType errorType, String tag, String message ) {
191         addError( ErrorSeverity.WARNING, errorType, tag, message, null, null, null );
192         return this;
193     }
194
195     /**
196      * Adds a warning to the result.
197      *
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.
206      */
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 );
210         return this;
211     }
212
213     /**
214      * Adds an error to the result. The general error tag defaults to "operation-failed".
215      *
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.
218      */
219     public RpcResultBuilder<T> withError( ErrorType errorType, String message ) {
220         addError( ErrorSeverity.ERROR, errorType, "operation-failed", message, null, null, null );
221         return this;
222     }
223
224     /**
225      * Adds an error to the result.
226      *
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.
231      */
232     public RpcResultBuilder<T> withError( ErrorType errorType, String tag, String message ) {
233         addError( ErrorSeverity.ERROR, errorType, tag, message, null, null, null );
234         return this;
235     }
236
237     /**
238      * Adds an error to the result.
239      *
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.
248      */
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 );
252         return this;
253     }
254
255     public RpcResult<T> build() {
256
257         return new RpcResultImpl<T>( successful, result,
258                 errors != null ? errors : Collections.<RpcError>emptyList() );
259     }
260 }