Skip to content

Commit e4aa56b

Browse files
committed
Add overloaded equals in Bytes
1 parent 9256264 commit e4aa56b

File tree

5 files changed

+85
-4
lines changed

5 files changed

+85
-4
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* add feature for gathering parts of the array as primitives (e.g. intAt(int position))
66
* add to unsigned byte conversations
7+
* add overloaded equals
78

89
## v0.4.3
910

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,15 @@ for (Byte aByte : bytesInstance) {
350350
}
351351
```
352352

353+
The `equals` method has overloaded versions for `byte[]`, `Byte[]` and `ByteBuffer` which can be used to directly
354+
compare the inner array:
355+
356+
```java
357+
byte[] primitiveArray1 = ...
358+
byte[] primitiveArray2 = ...
359+
Bytes.wrap(primitiveArray1).equals(primitiveArray2); //compares primitiveArray1 with primitiveArray2
360+
```
361+
353362
### Validation
354363

355364
A simple validation framework which can be used to check the internal byte array:

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ public int unsignedByteAt(int index) {
10021002
*/
10031003
public char charAt(int index) {
10041004
Util.checkIndexBounds(length(), index, 2, "char");
1005-
return ((ByteBuffer) ByteBuffer.wrap(internalArray()).order(byteOrder).position(index)).getChar();
1005+
return ByteBuffer.wrap(internalArray()).order(byteOrder).position(index).getChar();
10061006
}
10071007

10081008
/**
@@ -1015,7 +1015,7 @@ public char charAt(int index) {
10151015
*/
10161016
public short shortAt(int index) {
10171017
Util.checkIndexBounds(length(), index, 2, "short");
1018-
return ((ByteBuffer) ByteBuffer.wrap(internalArray()).order(byteOrder).position(index)).getShort();
1018+
return ByteBuffer.wrap(internalArray()).order(byteOrder).position(index).getShort();
10191019
}
10201020

10211021
/**
@@ -1028,7 +1028,7 @@ public short shortAt(int index) {
10281028
*/
10291029
public int intAt(int index) {
10301030
Util.checkIndexBounds(length(), index, 4, "int");
1031-
return ((ByteBuffer) ByteBuffer.wrap(internalArray()).order(byteOrder).position(index)).getInt();
1031+
return ByteBuffer.wrap(internalArray()).order(byteOrder).position(index).getInt();
10321032
}
10331033

10341034
/**
@@ -1041,7 +1041,7 @@ public int intAt(int index) {
10411041
*/
10421042
public long longAt(int index) {
10431043
Util.checkIndexBounds(length(), index, 8, "long");
1044-
return ((ByteBuffer) ByteBuffer.wrap(internalArray()).order(byteOrder).position(index)).getLong();
1044+
return ByteBuffer.wrap(internalArray()).order(byteOrder).position(index).getLong();
10451045
}
10461046

10471047
/**
@@ -1478,6 +1478,12 @@ public int compareTo(Bytes o) {
14781478
return internalBuffer().compareTo(o.internalBuffer());
14791479
}
14801480

1481+
/**
1482+
* Checks if this instance is equal to given other instance o
1483+
*
1484+
* @param o other instance
1485+
* @return if the instance are equal
1486+
*/
14811487
@Override
14821488
public boolean equals(Object o) {
14831489
if (this == o) return true;
@@ -1489,6 +1495,37 @@ public boolean equals(Object o) {
14891495
return byteOrder != null ? byteOrder.equals(bytes.byteOrder) : bytes.byteOrder == null;
14901496
}
14911497

1498+
/**
1499+
* Compares the inner array with given array
1500+
*
1501+
* @param anotherArray to compare with
1502+
* @return true if {@link Arrays#equals(byte[], byte[])} returns true on given and internal array
1503+
*/
1504+
public boolean equals(byte[] anotherArray) {
1505+
return anotherArray != null && Arrays.equals(internalArray(), anotherArray);
1506+
}
1507+
1508+
/**
1509+
* Compares the inner array with given array.
1510+
* Note: a <code>null</code> Byte will not be equal to a <code>0</code> byte
1511+
*
1512+
* @param anotherArray to compare with
1513+
* @return true if both array have same length and every byte element is the same
1514+
*/
1515+
public boolean equals(Byte[] anotherArray) {
1516+
return Util.equals(internalArray(), anotherArray);
1517+
}
1518+
1519+
/**
1520+
* Compares the inner array with the inner array of given ByteBuffer
1521+
*
1522+
* @param buffer to compare with
1523+
* @return true if both array have same length and every byte element is the same
1524+
*/
1525+
public boolean equals(ByteBuffer buffer) {
1526+
return buffer != null && Arrays.equals(internalArray(), buffer.array());
1527+
}
1528+
14921529
/**
14931530
* Checks only for internal array content
14941531
*

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,17 @@ static void checkExactLength(int length, int expectedLength, String type) {
426426
}
427427
}
428428

429+
static boolean equals(byte[] obj, Byte[] anotherArray) {
430+
if (anotherArray == null) return false;
431+
if (obj.length != anotherArray.length) return false;
432+
for (int i = 0; i < obj.length; i++) {
433+
if (anotherArray[i] == null || obj[i] != anotherArray[i]) {
434+
return false;
435+
}
436+
}
437+
return true;
438+
}
439+
429440
/*
430441
=================================================================================================
431442
Copyright 2011 Twitter, Inc.

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.Test;
2525

2626
import java.math.BigInteger;
27+
import java.nio.ByteBuffer;
2728
import java.nio.ByteOrder;
2829
import java.nio.ReadOnlyBufferException;
2930
import java.util.NoSuchElementException;
@@ -69,6 +70,28 @@ public void testEquals() throws Exception {
6970
assertFalse(Bytes.wrap(example_bytes_eight).equals(Bytes.wrap(example2_bytes_seven)));
7071
}
7172

73+
@Test
74+
public void testEqualsWithArray() throws Exception {
75+
assertTrue(Bytes.allocate(4).equals(new byte[4]));
76+
assertFalse(Bytes.allocate(4).equals(new byte[3]));
77+
assertFalse(Bytes.random(16).equals(new byte[16]));
78+
}
79+
80+
@Test
81+
public void testEqualsWithObjectArray() throws Exception {
82+
assertFalse(Bytes.allocate(4).equals(new Byte[4]));
83+
assertTrue(Bytes.allocate(4).equals(new Byte[]{0, 0, 0, 0}));
84+
assertFalse(Bytes.allocate(4).equals(new Byte[3]));
85+
assertFalse(Bytes.random(16).equals(new Byte[16]));
86+
}
87+
88+
@Test
89+
public void testEqualsWithByteBuffer() throws Exception {
90+
assertTrue(Bytes.allocate(4).equals(ByteBuffer.wrap(new byte[4])));
91+
assertFalse(Bytes.allocate(4).equals(ByteBuffer.wrap(new byte[3])));
92+
assertFalse(Bytes.random(16).equals(ByteBuffer.wrap(new byte[16])));
93+
}
94+
7295
@Test
7396
public void testEqualsContent() throws Exception {
7497
assertTrue(Bytes.wrap(new byte[0]).byteOrder(ByteOrder.BIG_ENDIAN).equalsContent(Bytes.wrap(new byte[0]).byteOrder(ByteOrder.LITTLE_ENDIAN)));

0 commit comments

Comments
 (0)