#2539: Optimize dash free UUID parse

This commit is contained in:
NayXegp 2018-10-28 12:04:27 +11:00 committed by md_5
parent cb4108c1b4
commit 27f926cfc7
2 changed files with 31 additions and 1 deletions

View File

@ -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 ) );
}
}

View File

@ -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 );
}
}
}