Replace equals and compareTo with FastByteComparison

This commit is contained in:
nicksavers 2014-06-26 19:51:24 +02:00
parent dfe4a878ea
commit 279e380727
1 changed files with 35 additions and 43 deletions

View File

@ -1,56 +1,48 @@
package org.ethereum.db; package org.ethereum.db;
import com.google.common.primitives.UnsignedBytes;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator;
import org.ethereum.util.FastByteComparisons;
/** /**
* www.ethereumJ.com * www.ethereumJ.com
* *
* @author: Roman Mandeleil * @author: Roman Mandeleil Created on: 11/06/2014 15:02
* Created on: 11/06/2014 15:02
*/ */
public class ByteArrayWrapper implements Comparable<ByteArrayWrapper> {
public class ByteArrayWrapper implements Comparable{
private final byte[] data; private final byte[] data;
public ByteArrayWrapper(byte[] data) public ByteArrayWrapper(byte[] data) {
{ if (data == null) {
if (data == null)
{
throw new NullPointerException(); throw new NullPointerException();
} }
this.data = data; this.data = data;
} }
@Override public boolean equals(Object other) {
public boolean equals(Object other) if (!(other instanceof ByteArrayWrapper)) {
{
if (!(other instanceof ByteArrayWrapper))
{
return false; return false;
} }
return Arrays.equals(data, ((ByteArrayWrapper)other).data); byte[] otherData = ((ByteArrayWrapper) other).getData();
return FastByteComparisons.compareTo(
data, 0, data.length,
otherData, 0, otherData.length) == 0;
} }
@Override @Override
public int hashCode() public int hashCode() {
{
return Arrays.hashCode(data); return Arrays.hashCode(data);
} }
@Override
public int compareTo(ByteArrayWrapper o) {
return FastByteComparisons.compareTo(
data, 0, data.length,
o.getData(), 0, o.getData().length);
}
public byte[] getData() { public byte[] getData() {
return data; return data;
} }
@Override
public int compareTo(Object second) {
Comparator<byte[]> comparator = UnsignedBytes.lexicographicalComparator();
return comparator.compare(this.data, ((ByteArrayWrapper)second).getData());
}
} }