Rev 767 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 762 | blopes | 1 | package br.com.sl.domain.util; |
| 2 | |||
| 3 | import java.math.BigDecimal; |
||
| 4 | import java.math.RoundingMode; |
||
| 5 | |||
| 6 | public final class BigDecimalUtils { |
||
| 7 | |||
| 8 | private static final BigDecimal ZERO = BigDecimal.ZERO; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Valor sentinela para representar NaN em BigDecimal. |
||
| 12 | * (Você pode ajustar para outro número se quiser) |
||
| 13 | */ |
||
| 14 | public static final BigDecimal BD_NAN = new BigDecimal("-999999999"); |
||
| 15 | |||
| 16 | private BigDecimalUtils() {} |
||
| 17 | |||
| 18 | /* ============================================================ |
||
| 19 | CONVERSÕES SEGURAS |
||
| 20 | ============================================================ */ |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Converte double em BigDecimal de forma segura. |
||
| 24 | */ |
||
| 25 | public static BigDecimal converterDoubleEmBigDecimal(Double value) { |
||
| 26 | return BigDecimal.valueOf(value); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Converte String em BigDecimal de forma segura. |
||
| 31 | * Aceita vírgula ou ponto. |
||
| 32 | */ |
||
| 33 | public static BigDecimal converterStringEmBigDecimal(String value) { |
||
| 34 | if (value == null || value.trim().isEmpty()) { |
||
| 35 | return ZERO; |
||
| 36 | } |
||
| 37 | value = value.trim().replace(",", "."); |
||
| 38 | return new BigDecimal(value); |
||
| 39 | } |
||
| 767 | blopes | 40 | |
| 41 | public static BigDecimal converterIntEmBigDecimal(int valor) { |
||
| 42 | return BigDecimal.valueOf(valor); |
||
| 43 | } |
||
| 762 | blopes | 44 | |
| 45 | /** |
||
| 46 | * Normaliza null → ZERO |
||
| 47 | */ |
||
| 48 | public static BigDecimal n(BigDecimal value) { |
||
| 49 | return value == null ? ZERO : value; |
||
| 50 | } |
||
| 51 | |||
| 52 | /* ============================================================ |
||
| 53 | OPERAÇÕES MATEMÁTICAS |
||
| 54 | ============================================================ */ |
||
| 55 | |||
| 56 | public static BigDecimal soma(BigDecimal a, BigDecimal b) { |
||
| 57 | return n(a).add(n(b)); |
||
| 58 | } |
||
| 59 | |||
| 60 | public static BigDecimal subtracao(BigDecimal a, BigDecimal b) { |
||
| 61 | return n(a).subtract(n(b)); |
||
| 62 | } |
||
| 63 | |||
| 64 | public static BigDecimal multiplicacao(BigDecimal a, BigDecimal b) { |
||
| 65 | return n(a).multiply(n(b)); |
||
| 66 | } |
||
| 67 | |||
| 68 | public static BigDecimal divisao(BigDecimal a, BigDecimal b) { |
||
| 69 | if (b == null || b.compareTo(BigDecimal.ZERO) == 0) { |
||
| 70 | throw new ArithmeticException("Divisão por zero"); |
||
| 71 | } |
||
| 72 | return n(a).divide(n(b), 10, RoundingMode.HALF_UP); |
||
| 73 | } |
||
| 74 | |||
| 75 | /* ============================================================ |
||
| 76 | COMPARAÇÕES |
||
| 77 | ============================================================ */ |
||
| 78 | |||
| 79 | public static boolean ehMaiorQue(BigDecimal a, BigDecimal b) { // a > b |
||
| 80 | return n(a).compareTo(n(b)) > 0; |
||
| 81 | } |
||
| 82 | |||
| 83 | public static boolean ehMenorQue(BigDecimal a, BigDecimal b) { // a < b |
||
| 84 | return n(a).compareTo(n(b)) < 0; |
||
| 85 | } |
||
| 86 | |||
| 87 | public static boolean ehMaiorOuIgualQue(BigDecimal a, BigDecimal b) { // a >= b |
||
| 88 | return n(a).compareTo(n(b)) >= 0; |
||
| 89 | } |
||
| 90 | |||
| 91 | public static boolean ehMenorOuIgualQue(BigDecimal a, BigDecimal b) { // a <= b |
||
| 92 | return n(a).compareTo(n(b)) <= 0; |
||
| 93 | } |
||
| 94 | |||
| 95 | public static boolean ehIgual(BigDecimal a, BigDecimal b) { // a == b |
||
| 96 | return n(a).compareTo(n(b)) == 0; |
||
| 97 | } |
||
| 98 | |||
| 99 | public static boolean ehDiferente(BigDecimal a, BigDecimal b) { // a != b |
||
| 100 | return n(a).compareTo(n(b)) != 0; |
||
| 101 | } |
||
| 102 | |||
| 103 | /* ============================================================ |
||
| 104 | EXTENSÕES |
||
| 105 | ============================================================ */ |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Valor absoluto. |
||
| 109 | */ |
||
| 110 | public static BigDecimal retornarValorAbsoluto(BigDecimal a) { |
||
| 111 | return n(a).abs(); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Arredonda com casas decimais. |
||
| 116 | */ |
||
| 117 | public static BigDecimal arredondar(BigDecimal a, int casasDecimais) { |
||
| 118 | return n(a).setScale(casasDecimais, RoundingMode.HALF_UP); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Verifica se 'a' está entre min e max, inclusive. |
||
| 123 | */ |
||
| 124 | public static boolean estaEntre(BigDecimal valor, BigDecimal minimo, BigDecimal maximo) { |
||
| 125 | BigDecimal val = n(valor); |
||
| 126 | return val.compareTo(n(minimo)) >= 0 && val.compareTo(n(maximo)) <= 0; |
||
| 127 | } |
||
| 128 | |||
| 129 | /* ============================================================ |
||
| 130 | MÉTODOS ESPECIALIZADOS PARA O ROBÔ |
||
| 131 | ============================================================ */ |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Diferença absoluta (amplitude). |
||
| 135 | */ |
||
| 136 | public static BigDecimal amplitude(BigDecimal abertura, BigDecimal fechamento) { |
||
| 137 | return retornarValorAbsoluto(subtracao(fechamento, abertura)); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Retorna true se valor for maior que zero. |
||
| 142 | */ |
||
| 143 | public static boolean positivo(BigDecimal a) { |
||
| 144 | return ehMaiorQue(a, ZERO); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Retorna true se valor for menor que zero. |
||
| 149 | */ |
||
| 150 | public static boolean negativo(BigDecimal a) { |
||
| 151 | return ehMenorQue(a, ZERO); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Retorna o maior dos dois valores (equivalente ao Math.max). |
||
| 156 | */ |
||
| 157 | public static BigDecimal maximo(BigDecimal a, BigDecimal b) { |
||
| 158 | BigDecimal na = n(a); |
||
| 159 | BigDecimal nb = n(b); |
||
| 160 | return na.compareTo(nb) >= 0 ? na : nb; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Retorna o menor dos dois valores (equivalente ao Math.min). |
||
| 165 | */ |
||
| 166 | public static BigDecimal minimo(BigDecimal a, BigDecimal b) { |
||
| 167 | BigDecimal na = n(a); |
||
| 168 | BigDecimal nb = n(b); |
||
| 169 | return na.compareTo(nb) <= 0 ? na : nb; |
||
| 170 | } |
||
| 171 | |||
| 172 | /* ============================================================ |
||
| 173 | SUPORTE A NaN / INFINITY |
||
| 174 | ============================================================ */ |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Converte double em BigDecimal de forma segura |
||
| 178 | * evitando NaN, -Infinity e +Infinity. |
||
| 179 | */ |
||
| 180 | public static BigDecimal fromDouble(double value) { |
||
| 181 | if (Double.isNaN(value) || Double.isInfinite(value)) { |
||
| 182 | return BD_NAN; // Valor sentinela |
||
| 183 | } |
||
| 184 | return BigDecimal.valueOf(value); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Verifica se um BigDecimal representa um NaN lógico. |
||
| 189 | */ |
||
| 190 | public static boolean isNaN(BigDecimal v) { |
||
| 191 | return v != null && v.compareTo(BD_NAN) == 0; |
||
| 192 | } |
||
| 779 | blopes | 193 | |
| 194 | |||
| 762 | blopes | 195 | |
| 196 | } |