Skip to content

Commit 7bb6254

Browse files
committed
Add caching for hashcode function
1 parent d0aab59 commit 7bb6254

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* add support for base64 url safe encoding #15
1010
* use EMPTY constant instance for empty byte array to safe memory #16
1111
* add `startsWith()` and `endsWidth()` methods #12
12+
* add cache for calculating the hashCode
1213

1314
## v0.6.0
1415

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121

2222
package at.favre.lib.bytes;
2323

24-
import java.io.*;
24+
import java.io.ByteArrayInputStream;
25+
import java.io.DataInput;
26+
import java.io.File;
27+
import java.io.InputStream;
28+
import java.io.Serializable;
2529
import java.math.BigInteger;
2630
import java.nio.ByteBuffer;
2731
import java.nio.ByteOrder;
@@ -31,7 +35,14 @@
3135
import java.nio.charset.StandardCharsets;
3236
import java.security.SecureRandom;
3337
import java.text.Normalizer;
34-
import java.util.*;
38+
import java.util.Arrays;
39+
import java.util.BitSet;
40+
import java.util.Collection;
41+
import java.util.Iterator;
42+
import java.util.List;
43+
import java.util.Objects;
44+
import java.util.Random;
45+
import java.util.UUID;
3546

3647
/**
3748
* Bytes is wrapper class for an byte-array that allows a lot of convenience operations on it:
@@ -52,7 +63,7 @@
5263
* <p>
5364
* <strong>Example:</strong>
5465
* <pre>
55-
* Bytes b = Bytes.from(array);
66+
* Bytes b = Bytes.from(array).mutable();
5667
* b.not();
5768
* System.out.println(b.encodeHex());
5869
* </pre>
@@ -609,6 +620,7 @@ public static Bytes random(int length, Random random) {
609620
private final byte[] byteArray;
610621
private final ByteOrder byteOrder;
611622
private final BytesFactory factory;
623+
private transient int hashCodeCache;
612624

613625
Bytes(byte[] byteArray, ByteOrder byteOrder) {
614626
this(byteArray, byteOrder, new Factory());
@@ -1867,13 +1879,15 @@ public boolean equalsContent(Bytes other) {
18671879

18681880
@Override
18691881
public int hashCode() {
1870-
int result = Arrays.hashCode(byteArray);
1871-
result = 31 * result + (byteOrder != null ? byteOrder.hashCode() : 0);
1872-
return result;
1882+
if (hashCodeCache == 0) {
1883+
hashCodeCache = Arrays.hashCode(byteArray);
1884+
hashCodeCache = 31 * hashCodeCache + (byteOrder != null ? byteOrder.hashCode() : 0);
1885+
}
1886+
return hashCodeCache;
18731887
}
18741888

18751889
/**
1876-
* A memory safe toString implementation, which only shows the byte length and at most 8 bytes preview in hex
1890+
* A constant length output toString() implementation, which only shows the byte length and at most 8 bytes preview in hex
18771891
* representation.
18781892
*
18791893
* @return string representation

src/test/java/at/favre/lib/bytes/BytesMiscTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ private void testToString(Bytes bytes) {
5252

5353
@Test
5454
public void testHashcode() {
55+
Bytes instance = Bytes.wrap(example_bytes_seven);
56+
assertEquals(instance.hashCode(), instance.hashCode());
57+
assertNotEquals(0, instance.hashCode());
5558
assertEquals(Bytes.wrap(example_bytes_seven).hashCode(), Bytes.from(example_bytes_seven).hashCode());
5659
assertEquals(Bytes.wrap(example2_bytes_seven).hashCode(), Bytes.from(example2_bytes_seven).hashCode());
5760
assertNotEquals(Bytes.wrap(example_bytes_seven).hashCode(), Bytes.wrap(example2_bytes_seven).hashCode());

0 commit comments

Comments
 (0)