Coverage Summary for Class: SubscriptionId (co.rsk.rpc.modules.eth.subscribe)
Class |
Class, %
|
Method, %
|
Line, %
|
SubscriptionId |
0%
(0/1)
|
0%
(0/6)
|
0%
(0/14)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2018 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 package co.rsk.rpc.modules.eth.subscribe;
19
20 import co.rsk.jsonrpc.JsonRpcResult;
21 import com.fasterxml.jackson.annotation.JsonCreator;
22 import com.fasterxml.jackson.annotation.JsonValue;
23 import org.ethereum.rpc.TypeConverter;
24
25 import java.security.SecureRandom;
26 import java.util.Arrays;
27
28 /**
29 * The subscription id DTO for JSON serialization purposes.
30 */
31 public class SubscriptionId extends JsonRpcResult {
32 private final byte[] id;
33
34 @JsonCreator
35 public SubscriptionId(String hexId) {
36 this.id = TypeConverter.stringHexToByteArray(hexId);
37 }
38
39 public SubscriptionId() {
40 this.id = new byte[16];
41 new SecureRandom().nextBytes(id);
42 }
43
44 public byte[] getId() {
45 return Arrays.copyOf(id, id.length);
46 }
47
48 @Override
49 public int hashCode() {
50 return Arrays.hashCode(id);
51 }
52
53 @Override
54 public boolean equals(Object o) {
55 if (o == this) {
56 return true;
57 }
58
59 if (!(o instanceof SubscriptionId)) {
60 return false;
61 }
62
63 SubscriptionId other = (SubscriptionId) o;
64 return Arrays.equals(this.id, other.id);
65 }
66
67 @JsonValue
68 @SuppressWarnings("unused")
69 private String serialize() {
70 return TypeConverter.toJsonHex(id);
71 }
72 }