Coverage Summary for Class: ByteArrayWrapper (org.ethereum.db)

Class Class, % Method, % Line, %
ByteArrayWrapper 100% (1/1) 57.1% (4/7) 68.8% (11/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.db; 21  22 import org.ethereum.util.ByteUtil; 23 import org.ethereum.util.FastByteComparisons; 24  25 import java.io.Serializable; 26 import java.util.Arrays; 27  28 /** 29  * @author Roman Mandeleil 30  * @since 11.06.2014 31  */ 32 public class ByteArrayWrapper implements Comparable<ByteArrayWrapper>, Serializable { 33  34  private final byte[] data; 35  private int hashCode = 0; 36  37  public ByteArrayWrapper(byte[] data) { 38  if (data == null) { 39  throw new NullPointerException("Data must not be null"); 40  } 41  this.data = data; 42  this.hashCode = Arrays.hashCode(data); 43  } 44  45  public boolean equals(Object other) { 46  if (!(other instanceof ByteArrayWrapper)) { 47  return false; 48  } 49  byte[] otherData = ((ByteArrayWrapper) other).data; 50  return ByteUtil.fastEquals(data, otherData); 51  } 52  53  @Override 54  public int hashCode() { 55  return hashCode; 56  } 57  58  @Override 59  public int compareTo(ByteArrayWrapper o) { 60  return FastByteComparisons.compareTo( 61  data, 0, data.length, 62  o.data, 0, o.data.length); 63  } 64  65  public byte[] getData() { 66  return data; 67  } 68  69  @Override 70  public String toString() { 71  return ByteUtil.toHexString(data); 72  } 73  74  public boolean equalsToByteArray(byte[] otherData) { 75  return otherData != null && ByteUtil.fastEquals(data, otherData); 76  } 77 }