-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
93 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/com/erikmafo/btviewer/util/UUIDConverterUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.erikmafo.btviewer.util; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import java.nio.ByteBuffer; | ||
import java.util.UUID; | ||
|
||
public class UUIDConverterUtil { | ||
|
||
@NotNull | ||
public static byte[] convertUUIDToBytes(@NotNull UUID uuid) { | ||
var buffer = ByteBuffer.wrap(new byte[16]); | ||
buffer.putLong(uuid.getMostSignificantBits()); | ||
buffer.putLong(uuid.getLeastSignificantBits()); | ||
return buffer.array(); | ||
} | ||
|
||
@NotNull | ||
public static UUID convertBytesToUUID(byte[] bytes) { | ||
var buffer = ByteBuffer.wrap(bytes); | ||
var mostSignificantBits = buffer.getLong(); | ||
var leastSignificantBits = buffer.getLong(); | ||
return new UUID(mostSignificantBits, leastSignificantBits); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/com/erikmafo/btviewer/util/UUIDConverterUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.erikmafo.btviewer.util; | ||
|
||
import org.junit.Test; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.UUID; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class UUIDConverterUtilTest { | ||
|
||
@Test | ||
public void convertUUIDToBytesReturnsMostSignificantBitsThenLeastSignificantBits() { | ||
// given | ||
var uuid = UUID.randomUUID(); | ||
|
||
// when | ||
var bytes = UUIDConverterUtil.convertUUIDToBytes(uuid); | ||
|
||
// then | ||
var buffer = ByteBuffer.wrap(bytes); | ||
assertEquals(uuid.getMostSignificantBits(), buffer.getLong()); | ||
assertEquals(uuid.getLeastSignificantBits(), buffer.getLong()); | ||
} | ||
|
||
@Test | ||
public void convertBytesToUUIDReturnsOriginalUUID() { | ||
// given | ||
var uuid = UUID.randomUUID(); | ||
var bytes = UUIDConverterUtil.convertUUIDToBytes(uuid); | ||
|
||
// when | ||
var convertedUUID = UUIDConverterUtil.convertBytesToUUID(bytes); | ||
|
||
// then | ||
assertEquals(uuid, convertedUUID); | ||
} | ||
} |