Skip to content

Commit dc915bd

Browse files
SPiluwatar
SP
authored andcommitted
Added: UUID generator snippet #248
248: updated formatting 248: updated formatting
1 parent 8597b61 commit dc915bd

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,6 +2336,36 @@ public class FormatBytesSnippet {
23362336
}
23372337
```
23382338

2339+
### UUID Generator
2340+
```java
2341+
public class UuidGeneratorSnippet {
2342+
2343+
/**
2344+
* Generates a random UUID (version 4).
2345+
*
2346+
* @return A string representation of a UUID.
2347+
*/
2348+
public static String generateUUID() {
2349+
return UUID.randomUUID().toString();
2350+
}
2351+
2352+
/**
2353+
* Validates if a string is a valid UUID.
2354+
*
2355+
* @param input the string to validate
2356+
* @return true if valid UUID, false otherwise
2357+
*/
2358+
public static boolean isValidUUID(String input) {
2359+
try {
2360+
UUID.fromString(input);
2361+
return true;
2362+
} catch (IllegalArgumentException e) {
2363+
return false;
2364+
}
2365+
}
2366+
}
2367+
```
2368+
23392369
## Thread
23402370

23412371
### Thread Pool
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2022 Ilkka Seppälä
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package string;
26+
27+
import java.util.UUID;
28+
29+
/**
30+
* UuidGeneratorSnippet.
31+
*/
32+
public class UuidGeneratorSnippet {
33+
34+
/**
35+
* Generates a random UUID (version 4).
36+
*
37+
* @return A string representation of a UUID.
38+
*/
39+
public static String generateUUID() {
40+
return UUID.randomUUID().toString();
41+
}
42+
43+
/**
44+
* Validates if a string is a valid UUID.
45+
*
46+
* @param input the string to validate
47+
* @return true if valid UUID, false otherwise
48+
*/
49+
public static boolean isValidUUID(String input) {
50+
try {
51+
UUID.fromString(input);
52+
return true;
53+
} catch (IllegalArgumentException e) {
54+
return false;
55+
}
56+
}
57+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2022 Ilkka Seppälä
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package string;
26+
27+
import org.junit.jupiter.api.Test;
28+
29+
import static org.junit.jupiter.api.Assertions.assertFalse;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
32+
class UuidGeneratorSnippetTest {
33+
34+
@Test
35+
void generateUUID_shouldReturnValidUUID() {
36+
String uuid = UuidGeneratorSnippet.generateUUID();
37+
assertTrue(UuidGeneratorSnippet.isValidUUID(uuid));
38+
}
39+
40+
@Test
41+
void isValidUUID_shouldReturnTrueForValidUUID() {
42+
assertTrue(UuidGeneratorSnippet.isValidUUID("123e4567-e89b-12d3-a456-426614174000"));
43+
}
44+
45+
@Test
46+
void isValidUUID_shouldReturnFalseForInvalidUUID() {
47+
assertFalse(UuidGeneratorSnippet.isValidUUID("not-a-uuid"));
48+
assertFalse(UuidGeneratorSnippet.isValidUUID("1234"));
49+
}
50+
51+
}

0 commit comments

Comments
 (0)