blob: a2f454143c513b811b8a0928222ec053c584200c [file] [log] [blame]
lishuai2ddc4692015-07-31 15:15:16 +08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ovsdb.rfc.jsonrpc;
17
18import static com.google.common.base.MoreObjects.toStringHelper;
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import java.util.List;
22import java.util.Objects;
23
24import com.google.common.collect.Lists;
25
26/**
27 * Json Rpc Response information that include id,error,result.
28 */
29public class JsonRpcResponse {
30
31 private final String id;
32 private final String error;
33 private final List<Object> result;
34
35 /**
36 * JsonRpcResponse Constructor.
37 * @param id the id node of response information
38 */
39 public JsonRpcResponse(String id) {
40 checkNotNull(id, "id cannot be null");
41 this.id = id;
42 this.error = null;
43 this.result = Lists.newArrayList();
44 }
45
46 /**
47 * JsonRpcResponse Constructor.
48 * @param id the id node of response information
49 * @param error the error node of response information
50 */
51 public JsonRpcResponse(String id, String error) {
52 checkNotNull(id, "id cannot be null");
53 checkNotNull(error, "error cannot be null");
54 this.id = id;
55 this.error = error;
56 this.result = Lists.newArrayList();
57 }
58
59 /**
60 * JsonRpcResponse Constructor.
61 * @param id the id node of response information
62 * @param error the error node of response information
63 * @param result the result node of response information
64 */
65 public JsonRpcResponse(String id, String error, List<Object> result) {
66 checkNotNull(id, "id cannot be null");
67 checkNotNull(error, "error cannot be null");
68 checkNotNull(result, "result cannot be null");
69 this.id = id;
70 this.error = error;
71 this.result = result;
72 }
73
74 /**
75 * Returns id.
76 * @return id
77 */
78 public String getId() {
79 return id;
80 }
81
82 /**
83 * Returns error.
84 * @return error
85 */
86 public String getError() {
87 return error;
88 }
89
90 /**
91 * Returns result.
92 * @return result
93 */
94 public List<Object> getResult() {
95 return result;
96 }
97
98 @Override
99 public int hashCode() {
100 return Objects.hash(id, error, result);
101 }
102
103 @Override
104 public boolean equals(Object obj) {
105 if (this == obj) {
106 return true;
107 }
108 if (obj instanceof JsonRpcResponse) {
109 final JsonRpcResponse other = (JsonRpcResponse) obj;
110 return Objects.equals(this.id, other.id)
111 && Objects.equals(this.error, other.error)
112 && Objects.equals(this.result, other.result);
113 }
114 return false;
115 }
116
117 @Override
118 public String toString() {
119 return toStringHelper(this).add("id", id).add("error", error)
120 .add("result", result).toString();
121 }
122}