5565d8b56d3b890058a7ab2d54a345e3c42171c7
[controller.git] / opendaylight / md-sal / sal-common-util / src / main / java / org / opendaylight / controller / sal / common / util / Rpcs.java
1 /*
2  * Copyright (c) 2013 Cisco 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 package org.opendaylight.controller.sal.common.util;
9
10 import java.io.Serializable;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.yang.common.RpcError;
17 import org.opendaylight.yangtools.yang.common.RpcResult;
18
19 import com.google.common.collect.ImmutableList;
20
21 public class Rpcs {
22     
23     public static <T> RpcResult<T> getRpcResult(boolean successful) {
24         RpcResult<T> ret = new RpcResultTO<T>(successful, null, ImmutableList.<RpcError>of());
25         return ret;
26     }
27     
28     public static <T> RpcResult<T> getRpcResult(boolean successful, T result,
29             Collection<RpcError> errors) {
30         RpcResult<T> ret = new RpcResultTO<T>(successful, result, errors);
31         return ret;
32     }
33
34     public static <T> RpcResult<T> getRpcResult(boolean successful, Collection<RpcError> errors) {
35         return new RpcResultTO<T>(successful, null, errors);
36     }
37     
38     private static class RpcResultTO<T> implements RpcResult<T>, Serializable, Immutable {
39         private static final long serialVersionUID = 1L;
40         private final Collection<RpcError> errors;
41         private final T result;
42         private final boolean successful;
43
44         public RpcResultTO(boolean successful, T result,
45                 Collection<RpcError> errors) {
46             this.successful = successful;
47             this.result = result;
48             this.errors = ImmutableList.copyOf(errors);
49         }
50
51         @Override
52         public boolean isSuccessful() {
53             return successful;
54         }
55
56         @Override
57         public T getResult() {
58             return result;
59         }
60
61         @Override
62         public Collection<RpcError> getErrors() {
63             return errors;
64         }
65
66     }
67 }