Skip to content

Added: UUID generator snippet #248 #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2336,6 +2336,36 @@ public class FormatBytesSnippet {
}
```

### UUID Generator
```java
public class UuidGeneratorSnippet {

/**
* Generates a random UUID (version 4).
*
* @return A string representation of a UUID.
*/
public static String generateUUID() {
return UUID.randomUUID().toString();
}

/**
* Validates if a string is a valid UUID.
*
* @param input the string to validate
* @return true if valid UUID, false otherwise
*/
public static boolean isValidUUID(String input) {
try {
UUID.fromString(input);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
}
```

## Thread

### Thread Pool
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/string/UuidGeneratorSnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package string;

import java.util.UUID;

/**
* UuidGeneratorSnippet.
*/
public class UuidGeneratorSnippet {

/**
* Generates a random UUID (version 4).
*
* @return A string representation of a UUID.
*/
public static String generateUuid() {
return UUID.randomUUID().toString();
}

/**
* Validates if a string is a valid UUID.
*
* @param input the string to validate
* @return true if valid UUID, false otherwise
*/
public static boolean isValidUuid(String input) {
try {
UUID.fromString(input);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
}
51 changes: 51 additions & 0 deletions src/test/java/string/UuidGeneratorSnippetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package string;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class UuidGeneratorSnippetTest {

@Test
void generateUuid_shouldReturnValidUuid() {
String uuid = UuidGeneratorSnippet.generateUuid();
assertTrue(UuidGeneratorSnippet.isValidUuid(uuid));
}

@Test
void isValidUuid_shouldReturnTrueForValidUuid() {
assertTrue(UuidGeneratorSnippet.isValidUuid("123e4567-e89b-12d3-a456-426614174000"));
}

@Test
void isValidUuid_shouldReturnFalseForInvalidUuid() {
assertFalse(UuidGeneratorSnippet.isValidUuid("not-a-uuid"));
assertFalse(UuidGeneratorSnippet.isValidUuid("1234"));
}

}