cache the hash of the Hash

This commit is contained in:
zzz
2009-11-27 16:29:15 +00:00
parent ea0747171f
commit 8682e7deb5

View File

@@ -32,6 +32,7 @@ public class Hash extends DataStructureImpl {
private volatile String _stringified; private volatile String _stringified;
private volatile String _base64ed; private volatile String _base64ed;
private /* FIXME final FIXME */ Map _xorCache; private /* FIXME final FIXME */ Map _xorCache;
private int _cachedHashCode;
public final static int HASH_LENGTH = 32; public final static int HASH_LENGTH = 32;
public final static Hash FAKE_HASH = new Hash(new byte[HASH_LENGTH]); public final static Hash FAKE_HASH = new Hash(new byte[HASH_LENGTH]);
@@ -54,6 +55,7 @@ public class Hash extends DataStructureImpl {
_data = data; _data = data;
_stringified = null; _stringified = null;
_base64ed = null; _base64ed = null;
_cachedHashCode = calcHashCode();
} }
/** /**
@@ -133,6 +135,7 @@ public class Hash extends DataStructureImpl {
_base64ed = null; _base64ed = null;
int read = read(in, _data); int read = read(in, _data);
if (read != HASH_LENGTH) throw new DataFormatException("Not enough bytes to read the hash"); if (read != HASH_LENGTH) throw new DataFormatException("Not enough bytes to read the hash");
_cachedHashCode = calcHashCode();
} }
public void writeBytes(OutputStream out) throws DataFormatException, IOException { public void writeBytes(OutputStream out) throws DataFormatException, IOException {
@@ -150,6 +153,11 @@ public class Hash extends DataStructureImpl {
/** a Hash is a hash, so just use the first 4 bytes for speed */ /** a Hash is a hash, so just use the first 4 bytes for speed */
@Override @Override
public int hashCode() { public int hashCode() {
return _cachedHashCode;
}
/** a Hash is a hash, so just use the first 4 bytes for speed */
private int calcHashCode() {
int rv = 0; int rv = 0;
if (_data != null) { if (_data != null) {
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)