Coverage Summary for Class: Coin (co.rsk.core)

Class Class, % Method, % Line, %
Coin 100% (1/1) 63.2% (12/19) 67.9% (19/28)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2018 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; 20  21 import org.ethereum.core.Denomination; 22  23 import javax.annotation.Nonnull; 24 import java.math.BigInteger; 25  26 /** 27  * RSK's native coin. 28  * One coin is convertible to (10^10)^(-1) satoshis. 29  * It is comparable to 1 wei in the Ethereum network. 30  */ 31 public class Coin implements Comparable<Coin> { 32  public static final Coin ZERO = new Coin(BigInteger.ZERO); 33  34  private final BigInteger value; 35  36  public Coin(byte[] value) { 37  this(new BigInteger(1, value)); 38  } 39  40  public Coin(BigInteger value) { 41  this.value = value; 42  } 43  44  public byte[] getBytes() { 45  return value.toByteArray(); 46  } 47  48  public BigInteger asBigInteger() { 49  return value; 50  } 51  52  public Coin negate() { 53  return new Coin(value.negate()); 54  } 55  56  public Coin add(Coin val) { 57  return new Coin(value.add(val.value)); 58  } 59  60  public Coin subtract(Coin val) { 61  return new Coin(value.subtract(val.value)); 62  } 63  64  public Coin multiply(BigInteger val) { 65  return new Coin(value.multiply(val)); 66  } 67  68  public Coin divide(BigInteger val) { 69  return new Coin(value.divide(val)); 70  } 71  72  public Coin[] divideAndRemainder(BigInteger val) { 73  BigInteger[] raw = value.divideAndRemainder(val); 74  return new Coin[]{new Coin(raw[0]), new Coin(raw[1])}; 75  } 76  77  /** 78  * @return the value denominated in Bitcoin, according to the 2-way peg convertibility rules. 79  */ 80  public co.rsk.bitcoinj.core.Coin toBitcoin() { 81  return co.rsk.bitcoinj.core.Coin.valueOf(Denomination.weisToSatoshis(value).longValue()); 82  } 83  84  @Override 85  public boolean equals(Object other) { 86  if (this == other) { 87  return true; 88  } 89  90  if (other == null || this.getClass() != other.getClass()) { 91  return false; 92  } 93  94  Coin otherCoin = (Coin) other; 95  return value.equals(otherCoin.value); 96  } 97  98  @Override 99  public int hashCode() { 100  return value.hashCode(); 101  } 102  103  @Override 104  public int compareTo(@Nonnull Coin other) { 105  return value.compareTo(other.value); 106  } 107  108  /** 109  * @return a DEBUG representation of the value, mainly used for logging. 110  */ 111  @Override 112  public String toString() { 113  return value.toString(); 114  } 115  116  public static Coin valueOf(long val) { 117  return new Coin(BigInteger.valueOf(val)); 118  } 119  120  public static Coin fromBitcoin(co.rsk.bitcoinj.core.Coin val) { 121  return new Coin(Denomination.satoshisToWeis(val.getValue())); 122  } 123  124  public static Coin max(Coin a, Coin b) { 125  return a.compareTo(b) > 0 ? a : b; 126  } 127 }