-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
63 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/goolbitg/api/util/RandomIdGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.goolbitg.api.util; | ||
|
||
public class RandomIdGenerator { | ||
|
||
private static final int ID_LENGTH = 10; | ||
|
||
public static String generate(int input) { | ||
StringBuilder sb; | ||
String chars = "abcdefghijklmnopqrstuvwxyz"; | ||
|
||
sb = new StringBuilder(); | ||
int value = input; | ||
for (int i = 0; i < ID_LENGTH; i++) { | ||
int index = value % chars.length(); | ||
sb.append(chars.charAt(index)); | ||
value /= chars.length(); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TABLE `sequences` ( | ||
`name` VARCHAR(50) NOT NULL, | ||
`curval` BIGINT UNSIGNED DEFAULT 0, | ||
PRIMARY KEY (`name`) | ||
); | ||
|
||
INSERT INTO `sequences` ( `name` ) | ||
VALUES ( 'id_seq' ); |
20 changes: 20 additions & 0 deletions
20
src/test/java/com/goolbitg/api/util/RandomIdGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.goolbitg.api.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* RandomIdGeneratorTest | ||
*/ | ||
public class RandomIdGeneratorTest { | ||
|
||
@Test | ||
void get_random_id() { | ||
int src = 26; | ||
String randomId = RandomIdGenerator.generate(src); | ||
|
||
assertEquals("abaaaaaaaa", randomId); | ||
} | ||
|
||
} |