Coverage Summary for Class: Bloom (org.ethereum.core)
Class |
Class, %
|
Method, %
|
Line, %
|
Bloom |
100%
(1/1)
|
40%
(4/10)
|
32.3%
(10/31)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package org.ethereum.core;
21
22 import org.ethereum.util.ByteUtil;
23
24 import java.util.Arrays;
25
26 /**
27 * See http://www.herongyang.com/Java/Bit-String-Set-Bit-to-Byte-Array.html.
28 *
29 * @author Roman Mandeleil
30 * @since 20.11.2014
31 */
32
33 public class Bloom {
34 public static final int BLOOM_BYTES = 256;
35
36 static final int _8STEPS = 8;
37 static final int _3LOW_BITS = 7;
38 static final int ENSURE_BYTE = 255;
39
40 private byte[] data = new byte[BLOOM_BYTES];
41
42 public Bloom() {
43 }
44
45 public Bloom(byte[] data) {
46 this.data = data;
47 }
48
49 public static Bloom create(byte[] toBloom) {
50
51 int mov1 = (((toBloom[0] & ENSURE_BYTE) & (_3LOW_BITS)) << _8STEPS) + ((toBloom[1]) & ENSURE_BYTE);
52 int mov2 = (((toBloom[2] & ENSURE_BYTE) & (_3LOW_BITS)) << _8STEPS) + ((toBloom[3]) & ENSURE_BYTE);
53 int mov3 = (((toBloom[4] & ENSURE_BYTE) & (_3LOW_BITS)) << _8STEPS) + ((toBloom[5]) & ENSURE_BYTE);
54
55 byte[] data = new byte[256];
56 Bloom bloom = new Bloom(data);
57
58 ByteUtil.setBit(data, mov1, 1);
59 ByteUtil.setBit(data, mov2, 1);
60 ByteUtil.setBit(data, mov3, 1);
61
62 return bloom;
63 }
64
65 public void or(Bloom bloom) {
66 for (int i = 0; i < data.length; ++i) {
67 data[i] |= bloom.data[i];
68 }
69 }
70
71 public boolean matches(Bloom topicBloom) {
72 Bloom copy = copy();
73 copy.or(topicBloom);
74 return this.equals(copy);
75 }
76
77 public byte[] getData() {
78 return data;
79 }
80
81 public Bloom copy() {
82 return new Bloom(Arrays.copyOf(getData(), getData().length));
83 }
84
85 @Override
86 public String toString() {
87 return ByteUtil.toHexString(data);
88 }
89
90 @Override
91 public boolean equals(Object o) {
92 if (this == o) {
93 return true;
94 }
95
96 if (o == null || getClass() != o.getClass()) {
97 return false;
98 }
99
100 Bloom bloom = (Bloom) o;
101
102 return Arrays.equals(data, bloom.data);
103
104 }
105
106 @Override
107 public int hashCode() {
108 return data != null ? Arrays.hashCode(data) : 0;
109 }
110 }