Skip to content

Commit 1840df7

Browse files
committed
Adds equals content feature and tests
1 parent 0160c98 commit 1840df7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,26 @@ public boolean equals(Object o) {
12471247
return byteOrder != null ? byteOrder.equals(bytes.byteOrder) : bytes.byteOrder == null;
12481248
}
12491249

1250+
/**
1251+
* Checks only for internal array content
1252+
*
1253+
* @param other to compare to
1254+
* @return true if the internal array are equals (see {@link Arrays#equals(byte[], byte[])})
1255+
*/
1256+
public boolean equalsContent(Bytes other) {
1257+
return other != null && equalsContent(other.internalArray());
1258+
}
1259+
1260+
/**
1261+
* Checks only for internal array content
1262+
*
1263+
* @param array to compare to
1264+
* @return true if the internal array are equals (see {@link Arrays#equals(byte[], byte[])})
1265+
*/
1266+
public boolean equalsContent(byte[] array) {
1267+
return array != null && Arrays.equals(internalArray(), array);
1268+
}
1269+
12501270
@Override
12511271
public int hashCode() {
12521272
int result = Arrays.hashCode(byteArray);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import org.junit.Test;
2525

26+
import java.nio.ByteOrder;
27+
2628
import static org.junit.Assert.*;
2729

2830
public class BytesMiscTest extends ABytesTest {
@@ -56,11 +58,22 @@ public void testEquals() throws Exception {
5658
assertTrue(Bytes.wrap(new byte[0]).equals(Bytes.wrap(new byte[0])));
5759
assertTrue(Bytes.wrap(new byte[16]).equals(Bytes.wrap(new byte[16])));
5860
assertTrue(Bytes.wrap(example_bytes_seven).equals(Bytes.from(example_bytes_seven)));
61+
assertFalse(Bytes.wrap(example_bytes_seven).byteOrder(ByteOrder.BIG_ENDIAN).equals(Bytes.from(example_bytes_seven).byteOrder(ByteOrder.LITTLE_ENDIAN)));
5962
assertTrue(Bytes.wrap(example2_bytes_seven).equals(Bytes.from(example2_bytes_seven)));
6063
assertFalse(Bytes.wrap(example_bytes_seven).equals(Bytes.wrap(example2_bytes_seven)));
6164
assertFalse(Bytes.wrap(example_bytes_eight).equals(Bytes.wrap(example2_bytes_seven)));
6265
}
6366

67+
@Test
68+
public void testEqualsContent() throws Exception {
69+
assertTrue(Bytes.wrap(new byte[0]).byteOrder(ByteOrder.BIG_ENDIAN).equalsContent(Bytes.wrap(new byte[0]).byteOrder(ByteOrder.LITTLE_ENDIAN)));
70+
assertTrue(Bytes.from(example_bytes_seven).byteOrder(ByteOrder.BIG_ENDIAN).equalsContent(Bytes.from(example_bytes_seven).byteOrder(ByteOrder.LITTLE_ENDIAN)));
71+
assertTrue(Bytes.from(example_bytes_seven).mutable().equalsContent(Bytes.from(example_bytes_seven).byteOrder(ByteOrder.LITTLE_ENDIAN)));
72+
assertTrue(Bytes.from(example_bytes_seven).mutable().equalsContent(Bytes.from(example_bytes_seven)));
73+
assertTrue(Bytes.from(example_bytes_seven).readOnly().equalsContent(Bytes.from(example_bytes_seven)));
74+
assertTrue(Bytes.from(example_bytes_seven).readOnly().equalsContent(example_bytes_seven));
75+
}
76+
6477
@Test
6578
public void testCompareTo() throws Exception {
6679
byte[] b1 = new byte[]{0x01};

0 commit comments

Comments
 (0)