Coverage Summary for Class: BIUtil (org.ethereum.util)
Class |
Class, %
|
Method, %
|
Line, %
|
BIUtil |
100%
(1/1)
|
57.1%
(8/14)
|
50%
(8/16)
|
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.util;
21
22 import co.rsk.core.Coin;
23
24 import java.math.BigInteger;
25
26 public class BIUtil {
27
28
29 /**
30 * @param value - not null
31 * @return true - if the param is zero
32 */
33 public static boolean isZero(BigInteger value){
34 return value.compareTo(BigInteger.ZERO) == 0;
35 }
36
37 /**
38 * @param valueA - not null
39 * @param valueB - not null
40 * @return true - if the valueA is equalBytes to valueB is zero
41 */
42 public static boolean isEqual(BigInteger valueA, BigInteger valueB){
43 return valueA.compareTo(valueB) == 0;
44 }
45
46 /**
47 * @param valueA - not null
48 * @param valueB - not null
49 * @return true - if the valueA is not equalBytes to valueB is zero
50 */
51 public static boolean isNotEqual(BigInteger valueA, BigInteger valueB){
52 return !isEqual(valueA, valueB);
53 }
54
55 /**
56 * @param valueA - not null
57 * @param valueB - not null
58 * @return true - if the valueA is less than valueB is zero
59 */
60 public static boolean isLessThan(BigInteger valueA, BigInteger valueB){
61 return valueA.compareTo(valueB) < 0;
62 }
63
64 /**
65 * @param valueA - not null
66 * @param valueB - not null
67 * @return true - if the valueA is more than valueB is zero
68 */
69 public static boolean isMoreThan(BigInteger valueA, BigInteger valueB){
70 return valueA.compareTo(valueB) > 0;
71 }
72
73
74 /**
75 * @param valueA - not null
76 * @param valueB - not null
77 * @return sum - valueA + valueB
78 */
79 public static BigInteger sum(BigInteger valueA, BigInteger valueB){
80 return valueA.add(valueB);
81 }
82
83
84 /**
85 * @param data = not null
86 * @return new positive BigInteger
87 */
88 public static BigInteger toBI(byte[] data){
89 return new BigInteger(1, data);
90 }
91
92 /**
93 * @param data = not null
94 * @return new positive BigInteger
95 */
96 public static BigInteger toBI(long data){
97 return BigInteger.valueOf(data);
98 }
99
100
101 public static boolean isPositive(BigInteger value){
102 return value.signum() > 0;
103 }
104
105 public static boolean isCovers(Coin covers, Coin value){
106 return !isNotCovers(covers, value);
107 }
108
109 public static boolean isNotCovers(Coin covers, Coin value){
110 return covers.compareTo(value) < 0;
111 }
112
113
114 public static boolean isIn20PercentRange(BigInteger first, BigInteger second) {
115 BigInteger five = BigInteger.valueOf(5);
116 BigInteger limit = first.add(first.divide(five));
117 return !isMoreThan(second, limit);
118 }
119
120 public static BigInteger max(BigInteger first, BigInteger second) {
121 return first.compareTo(second) < 0 ? second : first;
122 }
123
124 }