Coverage Summary for Class: Topic (org.ethereum.rpc)
Class |
Class, %
|
Method, %
|
Line, %
|
Topic |
0%
(0/1)
|
0%
(0/7)
|
0%
(0/15)
|
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
19 package org.ethereum.rpc;
20
21 import org.ethereum.util.ByteUtil;
22
23 import java.util.Arrays;
24
25 /**
26 * Created by ajlopez on 18/01/2018.
27 */
28 public final class Topic {
29 /**
30 * This is the size of a topic in bytes.
31 */
32 private static final int LENGTH_IN_BYTES = 32;
33
34 private final byte[] bytes;
35
36 /**
37 * @param topic the hex-encoded 32 bytes long topic, with or without 0x prefix.
38 */
39 public Topic(String topic) {
40 this(TypeConverter.stringHexToByteArray(topic));
41 }
42
43 /**
44 * @param bytes the 32 bytes long raw topic bytes.
45 */
46 public Topic(byte[] bytes) {
47 if (bytes.length != LENGTH_IN_BYTES) {
48 throw new RuntimeException(String.format("A topic must be %d bytes long", LENGTH_IN_BYTES));
49 }
50
51 this.bytes = bytes;
52 }
53
54 public byte[] getBytes() {
55 return bytes;
56 }
57
58 @Override
59 public boolean equals(Object other) {
60 if (this == other) {
61 return true;
62 }
63
64 if (other == null || this.getClass() != other.getClass()) {
65 return false;
66 }
67
68 Topic otherTopic = (Topic) other;
69
70 return Arrays.equals(bytes, otherTopic.bytes);
71 }
72
73 @Override
74 public int hashCode() {
75 return Arrays.hashCode(bytes);
76 }
77
78 @Override
79 public String toString() {
80 return ByteUtil.toHexString(bytes);
81 }
82
83 public String toJsonString() {
84 return TypeConverter.toUnformattedJsonHex(this.getBytes());
85 }
86 }