Coverage Summary for Class: Uint24 (co.rsk.core.types.ints)
Class |
Class, %
|
Method, %
|
Line, %
|
Uint24 |
100%
(1/1)
|
77.8%
(7/9)
|
79.2%
(19/24)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2019 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 co.rsk.core.types.ints;
20
21 public final class Uint24 implements Comparable<Uint24> {
22 private static final int MAX_VALUE_INT = 0x00ffffff;
23 public static final int SIZE = 24;
24 public static final int BYTES = SIZE / Byte.SIZE;
25 public static final Uint24 ZERO = new Uint24(0);
26 public static final Uint24 MAX_VALUE = new Uint24(MAX_VALUE_INT);
27
28 private final int intValue;
29
30 public Uint24(int intValue) {
31 if (intValue < 0 || intValue > MAX_VALUE_INT) {
32 throw new IllegalArgumentException("The supplied value doesn't fit in a Uint24");
33 }
34
35 this.intValue = intValue;
36 }
37
38 public byte[] encode() {
39 byte[] bytes = new byte[BYTES];
40 bytes[2] = (byte) (intValue);
41 bytes[1] = (byte) (intValue >>> 8);
42 bytes[0] = (byte) (intValue >>> 16);
43 return bytes;
44 }
45
46 public int intValue() {
47 return intValue;
48 }
49
50 @Override
51 public boolean equals(Object o) {
52 if (this == o) {
53 return true;
54 }
55
56 if (o == null || getClass() != o.getClass()) {
57 return false;
58 }
59
60 Uint24 uint24 = (Uint24) o;
61 return intValue == uint24.intValue;
62 }
63
64 @Override
65 public int hashCode() {
66 return Integer.hashCode(intValue);
67 }
68
69 @Override
70 public int compareTo(Uint24 o) {
71 return Integer.compare(intValue, o.intValue);
72 }
73
74 @Override
75 public String toString() {
76 return Integer.toString(intValue);
77 }
78
79 public static Uint24 decode(byte[] bytes, int offset) {
80 int intValue = (bytes[offset + 2] & 0xFF) +
81 ((bytes[offset + 1] & 0xFF) << 8) +
82 ((bytes[offset ] & 0xFF) << 16);
83 return new Uint24(intValue);
84 }
85 }