Coverage Summary for Class: Uint8 (co.rsk.core.types.ints)
Class |
Class, %
|
Method, %
|
Line, %
|
Uint8 |
100%
(1/1)
|
66.7%
(6/9)
|
57.1%
(12/21)
|
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 Uint8 {
22 private static final int MAX_VALUE_INT = 0xff;
23 private static final int SIZE = 8;
24 public static final int BYTES = SIZE / Byte.SIZE;
25 public static final Uint8 MAX_VALUE = new Uint8(MAX_VALUE_INT);
26
27 private final int intValue;
28
29 public Uint8(int intValue) {
30 if (intValue < 0 || intValue > MAX_VALUE_INT) {
31 throw new IllegalArgumentException("The supplied value doesn't fit in a Uint8");
32 }
33
34 this.intValue = intValue;
35 }
36
37 public byte asByte() {
38 return (byte) intValue;
39 }
40
41 public byte[] encode() {
42 byte[] bytes = new byte[BYTES];
43 bytes[0] = asByte();
44 return bytes;
45 }
46
47 public int intValue() {
48 return intValue;
49 }
50
51 @Override
52 public boolean equals(Object o) {
53 if (this == o) {
54 return true;
55 }
56
57 if (o == null || getClass() != o.getClass()) {
58 return false;
59 }
60
61 Uint8 uint8 = (Uint8) o;
62 return intValue == uint8.intValue;
63 }
64
65 @Override
66 public int hashCode() {
67 return Integer.hashCode(intValue);
68 }
69
70 @Override
71 public String toString() {
72 return Integer.toString(intValue);
73 }
74
75 public static Uint8 decode(byte[] bytes, int offset) {
76 int intValue = bytes[offset] & 0xFF;
77 return new Uint8(intValue);
78 }
79 }