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