Skip to content

Commit 8574b3e

Browse files
committed
Fix throw correct exception in toUUID()
Also fix javadoc for incorrect throws explanation.
1 parent c956722 commit 8574b3e

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

CHANGELOG

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

55
* add `from()` constructor from `float[]`
66
* add `from()` constructor from `double[]`
7+
* fix throwing `IllegalArgumentException` instead of `IllegalStateException` in `.toUUID()`
78

89
## v1.3.0
910
* improve hex encoder performance by factor 5

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,11 +1832,10 @@ public BigInteger toBigInteger() {
18321832
* to a {@link UUID} constructor.
18331833
*
18341834
* @return newly created UUID
1835+
* @throws IllegalArgumentException if byte array has length not equal to 16
18351836
*/
18361837
public UUID toUUID() {
1837-
if (length() != 16) {
1838-
throw new IllegalStateException("creating UUID requires internal array to be exactly 16 bytes, was " + length());
1839-
}
1838+
Util.Validation.checkExactLength(length(), 16, "UUID");
18401839
ByteBuffer byteBuffer = buffer();
18411840
return new UUID(byteBuffer.getLong(), byteBuffer.getLong());
18421841
}
@@ -1848,7 +1847,7 @@ public UUID toUUID() {
18481847
* If you just want to get the first element as {@code byte}, see {@link #byteAt(int)}, using index zero.
18491848
*
18501849
* @return the byte representation
1851-
* @throws IllegalStateException if byte array has length not equal to 1
1850+
* @throws IllegalArgumentException if byte array has length not equal to 1
18521851
* @see <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">Primitive Types</a>
18531852
*/
18541853
public byte toByte() {
@@ -1863,7 +1862,7 @@ public byte toByte() {
18631862
* If you just want to get the first element as {@code byte}, see {@link #byteAt(int)}, using index zero.
18641863
*
18651864
* @return the unsigned byte representation wrapped in an int
1866-
* @throws IllegalStateException if byte array has length not equal to 1
1865+
* @throws IllegalArgumentException if byte array has length not equal to 1
18671866
* @see <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">Primitive Types</a>
18681867
*/
18691868
public int toUnsignedByte() {
@@ -1878,7 +1877,7 @@ public int toUnsignedByte() {
18781877
* If you just want to get the first 2 bytes as {@code char}, see {@link #charAt(int)} using index zero.
18791878
*
18801879
* @return the int representation
1881-
* @throws IllegalStateException if byte array has length not equal to 2
1880+
* @throws IllegalArgumentException if byte array has length not equal to 2
18821881
* @see <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">Primitive Types</a>
18831882
*/
18841883
public char toChar() {
@@ -1893,7 +1892,7 @@ public char toChar() {
18931892
* If you just want to get the first 2 bytes as {@code short}, see {@link #shortAt(int)} using index zero.
18941893
*
18951894
* @return the int representation
1896-
* @throws IllegalStateException if byte array has length not equal to 2
1895+
* @throws IllegalArgumentException if byte array has length not equal to 2
18971896
* @see <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">Primitive Types</a>
18981897
*/
18991898
public short toShort() {
@@ -1908,7 +1907,7 @@ public short toShort() {
19081907
* If you just want to get the first 4 bytes as {@code int}, see {@link #intAt(int)} using index zero.
19091908
*
19101909
* @return the int representation
1911-
* @throws IllegalStateException if byte array has length not equal to 4
1910+
* @throws IllegalArgumentException if byte array has length not equal to 4
19121911
* @see <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">Primitive Types</a>
19131912
*/
19141913
public int toInt() {
@@ -1942,7 +1941,7 @@ public int[] toIntArray() {
19421941
* If you just want to get the first 4 bytes as {@code long}, see {@link #longAt(int)} using index zero.
19431942
*
19441943
* @return the long representation
1945-
* @throws IllegalStateException if byte array has length not equal to 8
1944+
* @throws IllegalArgumentException if byte array has length not equal to 8
19461945
* @see <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">Primitive Types</a>
19471946
*/
19481947
public long toLong() {
@@ -1974,7 +1973,7 @@ public long[] toLongArray() {
19741973
* representation for a Java float value. The output is dependent on the set {@link #byteOrder()}.
19751974
*
19761975
* @return the float representation
1977-
* @throws IllegalStateException if byte array has length not equal to 4
1976+
* @throws IllegalArgumentException if byte array has length not equal to 4
19781977
* @see <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">Primitive Types</a>
19791978
*/
19801979
public float toFloat() {
@@ -2006,7 +2005,7 @@ public float[] toFloatArray() {
20062005
* representation for a Java double value. The output is dependent on the set {@link #byteOrder()}.
20072006
*
20082007
* @return the double representation
2009-
* @throws IllegalStateException if byte array has length not equal to 8
2008+
* @throws IllegalArgumentException if byte array has length not equal to 8
20102009
* @see <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">Primitive Types</a>
20112010
*/
20122011
public double toDouble() {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,17 @@ public void toDouble() {
222222
}
223223
}
224224

225-
@Test(expected = IllegalStateException.class)
225+
@Test(expected = IllegalArgumentException.class)
226226
public void testToUUIDToLong() {
227227
Bytes.random(17).toUUID();
228228
}
229229

230-
@Test(expected = IllegalStateException.class)
230+
@Test(expected = IllegalArgumentException.class)
231231
public void testToUUIDToShort() {
232232
Bytes.random(15).toUUID();
233233
}
234234

235-
@Test(expected = IllegalStateException.class)
235+
@Test(expected = IllegalArgumentException.class)
236236
public void testToUUIDEmpty() {
237237
Bytes.allocate(0).toUUID();
238238
}

0 commit comments

Comments
 (0)