<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Milton Sermoud</title>
    <description>The latest articles on DEV Community by Milton Sermoud (@miltonsermoud).</description>
    <link>https://dev.to/miltonsermoud</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F671788%2Fe5018a04-f31c-4989-b37f-76b4c1c2813f.jpeg</url>
      <title>DEV Community: Milton Sermoud</title>
      <link>https://dev.to/miltonsermoud</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/miltonsermoud"/>
    <language>en</language>
    <item>
      <title>Checking if String UUID is valid with Java</title>
      <dc:creator>Milton Sermoud</dc:creator>
      <pubDate>Thu, 30 Jun 2022 20:49:39 +0000</pubDate>
      <link>https://dev.to/miltonsermoud/checking-if-string-uuid-is-valid-with-java-37da</link>
      <guid>https://dev.to/miltonsermoud/checking-if-string-uuid-is-valid-with-java-37da</guid>
      <description>&lt;h2&gt;
  
  
  First words
&lt;/h2&gt;

&lt;p&gt;All credits to Daniel Heid. This code is based on his &lt;a href="https://github.com/hibernate/hibernate-validator/pull/1199"&gt;PR&lt;/a&gt; in the hibernate-validator repository on Github. Thank you, Daniel!&lt;/p&gt;

&lt;h2&gt;
  
  
  The code
&lt;/h2&gt;

&lt;p&gt;If you don't want to read me, just copy the code below and follow the instructions I leave up in the first lines of the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* 
 * This code is based on this PR: https://github.com/hibernate/hibernate-validator/pull/1199
 * All credits to Daniel Heid (dheid). I just made some adjustments.
 * In order to apply this on your situation, just create this class and call the method "isValidStringUUID". As its param, pass in your string UUID.
*/

public class UUIDValidator {
    enum LetterCase {

        /**
         * Only lower case is valid
         */
        LOWER_CASE,

        /**
         * Only upper case is valid
         */
        UPPER_CASE,

        /**
         * Every letter case is valid
         */
        INSENSITIVE

    }

    private static LetterCase letterCase = LetterCase.LOWER_CASE;

    private static final int[] GROUP_LENGTHS = { 8, 4, 4, 4, 12 };

    public static boolean isValidStringUUID(String value) {
        if ( value == null ) {
            return false;
        }
        int valueLength = value.length();
        if ( valueLength == 0 ) {
            return false;
        }
        else if ( valueLength != 36 ) {
            return false;
        }

        int groupIndex = 0;
        int groupLength = 0;
        int checksum = 0;
        for ( int charIndex = 0; charIndex &amp;lt; valueLength; charIndex++ ) {

            char ch = value.charAt( charIndex );

            if ( ch == '-' ) {
                groupIndex++;
                groupLength = 0;
            }
            else {

                groupLength++;
                if ( groupLength &amp;gt; GROUP_LENGTHS[groupIndex] ) {
                    return false;
                }

                int numericValue = Character.digit( ch, 16 );
                if ( numericValue == -1 ) {
                    // not a hex digit
                    return false;
                }
                if ( numericValue &amp;gt; 9 &amp;amp;&amp;amp; !hasCorrectLetterCase( ch ) ) {
                    return false;
                }
                checksum += numericValue;
            }
        }
        // NIL UUID
        if ( checksum == 0 ) {
            return false;
        }
        return true;
    }
    private static boolean hasCorrectLetterCase(char ch) {
        if ( letterCase == null ) {
            return true;
        }
        if ( letterCase == LetterCase.LOWER_CASE &amp;amp;&amp;amp; !Character.isLowerCase( ch ) ) {
            return false;
        }
        return letterCase != LetterCase.UPPER_CASE || Character.isUpperCase( ch );
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to make it work, just create this class in your project and call the method &lt;code&gt;UUIDValidator.isValidStringUUID(String your_UUID_String_Goes_Here)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this code we have all kind of validations: for if it's null, if it's a blank string, if its length is incorrect, if the digits of the UUID String contains only hex digits, if each group of the UUID contains the right amount of characters, etc, etc. &lt;/p&gt;

&lt;p&gt;If you want a more specialized validator, don't forget to check the PR I mentioned before. It also contains how to get the UUID version and its variation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Good to read
&lt;/h3&gt;

&lt;p&gt;If you want to dive in on this subject (it's definitely so rich and interesting), you can check &lt;a href="https://www.uuidtools.com/decode"&gt;this link&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  ...The end!
&lt;/h2&gt;

&lt;p&gt;That's all! Thank you for this reading 😁&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>uuid</category>
      <category>token</category>
    </item>
  </channel>
</rss>
