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

Class Class, % Method, % Line, %
BlockDifficulty 100% (1/1) 70% (7/10) 70% (14/20)


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 javax.annotation.Nonnull; 22 import java.io.Serializable; 23 import java.math.BigInteger; 24  25 /** 26  * A block's difficulty, stored internally as a {@link java.math.BigInteger}. 27  */ 28 public class BlockDifficulty implements Comparable<BlockDifficulty>, Serializable { 29  public static final BlockDifficulty ZERO = new BlockDifficulty(BigInteger.ZERO); 30  public static final BlockDifficulty ONE = new BlockDifficulty(BigInteger.ONE); 31  32  private final BigInteger value; 33  34  /** 35  * @param value the difficulty value, which should be positive. 36  */ 37  public BlockDifficulty(BigInteger value) { 38  if (value.signum() < 0) { 39  throw new RuntimeException("A block difficulty must be positive or zero"); 40  } 41  42  this.value = value; 43  } 44  45  public byte[] getBytes() { 46  return value.toByteArray(); 47  } 48  49  public BigInteger asBigInteger() { 50  return value; 51  } 52  53  @Override 54  public boolean equals(Object other) { 55  if (this == other) { 56  return true; 57  } 58  59  if (other == null || this.getClass() != other.getClass()) { 60  return false; 61  } 62  63  BlockDifficulty otherDifficulty = (BlockDifficulty) other; 64  return value.equals(otherDifficulty.value); 65  } 66  67  @Override 68  public int hashCode() { 69  return value.hashCode(); 70  } 71  72  /** 73  * @return a DEBUG representation of the difficulty, mainly used for logging. 74  */ 75  @Override 76  public String toString() { 77  return value.toString(); 78  } 79  80  public BlockDifficulty add(BlockDifficulty other) { 81  return new BlockDifficulty(value.add(other.value)); 82  } 83  84  public BlockDifficulty subtract(BlockDifficulty other) { 85  return new BlockDifficulty(value.subtract(other.value)); 86  } 87  88  89  @Override 90  public int compareTo(@Nonnull BlockDifficulty other) { 91  return value.compareTo(other.value); 92  } 93 }