From 27f926cfc7775bf58855028ff6d1e04526a0e204 Mon Sep 17 00:00:00 2001 From: NayXegp Date: Sun, 28 Oct 2018 12:04:27 +1100 Subject: [PATCH] #2539: Optimize dash free UUID parse --- api/src/main/java/net/md_5/bungee/Util.java | 3 +- .../java/net/md_5/bungee/util/UUIDTest.java | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 api/src/test/java/net/md_5/bungee/util/UUIDTest.java diff --git a/api/src/main/java/net/md_5/bungee/Util.java b/api/src/main/java/net/md_5/bungee/Util.java index 86a00555..cb84917e 100644 --- a/api/src/main/java/net/md_5/bungee/Util.java +++ b/api/src/main/java/net/md_5/bungee/Util.java @@ -1,6 +1,7 @@ package net.md_5.bungee; import com.google.common.base.Joiner; +import com.google.common.primitives.UnsignedLongs; import java.net.InetSocketAddress; import java.net.URI; import java.net.URISyntaxException; @@ -78,6 +79,6 @@ public class Util */ public static UUID getUUID(String uuid) { - return UUID.fromString( uuid.substring( 0, 8 ) + "-" + uuid.substring( 8, 12 ) + "-" + uuid.substring( 12, 16 ) + "-" + uuid.substring( 16, 20 ) + "-" + uuid.substring( 20, 32 ) ); + return new UUID( UnsignedLongs.parseUnsignedLong( uuid.substring( 0, 16 ), 16 ), UnsignedLongs.parseUnsignedLong( uuid.substring( 16 ), 16 ) ); } } diff --git a/api/src/test/java/net/md_5/bungee/util/UUIDTest.java b/api/src/test/java/net/md_5/bungee/util/UUIDTest.java new file mode 100644 index 00000000..9f2d58ed --- /dev/null +++ b/api/src/test/java/net/md_5/bungee/util/UUIDTest.java @@ -0,0 +1,29 @@ +package net.md_5.bungee.util; + +import java.util.UUID; +import net.md_5.bungee.Util; +import org.junit.Assert; +import org.junit.Test; + +public class UUIDTest +{ + + @Test + public void testSingle() + { + UUID uuid = UUID.fromString( "af74a02d-19cb-445b-b07f-6866a861f783" ); + UUID uuid1 = Util.getUUID( "af74a02d19cb445bb07f6866a861f783" ); + Assert.assertEquals( uuid, uuid1 ); + } + + @Test + public void testMany() + { + for ( int i = 0; i < 1000; i++ ) + { + UUID expected = UUID.randomUUID(); + UUID actual = Util.getUUID( expected.toString().replace( "-", "" ) ); + Assert.assertEquals( "Could not parse UUID " + expected, expected, actual ); + } + } +}