-
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
1 parent
cd2a8e3
commit 0816868
Showing
4 changed files
with
61 additions
and
19 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
51 changes: 51 additions & 0 deletions
51
...n/java/com/playkuround/playkuroundserver/domain/auth/email/application/CodeGenerator.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,51 @@ | ||
package com.playkuround.playkuroundserver.domain.auth.email.application; | ||
|
||
import java.util.Objects; | ||
import java.util.Random; | ||
import java.util.Set; | ||
|
||
public class CodeGenerator { | ||
|
||
public String generateCode(Set<CodeType> codeTypeSet, long codeLength) { | ||
validateParameters(codeTypeSet, codeLength); | ||
|
||
String characters = createCharacters(codeTypeSet); | ||
|
||
StringBuilder codeBuilder = new StringBuilder(); | ||
Random random = new Random(); | ||
for (int i = 0; i < codeLength; i++) { | ||
int index = random.nextInt(characters.length()); | ||
char randomChar = characters.charAt(index); | ||
codeBuilder.append(randomChar); | ||
} | ||
return codeBuilder.toString(); | ||
} | ||
|
||
private void validateParameters(Set<CodeType> codeTypeSet, long codeLength) { | ||
Objects.requireNonNull(codeTypeSet, "codeTypeSet must not be null"); | ||
if (codeTypeSet.isEmpty()) { | ||
throw new IllegalArgumentException("codeTypeSet must not be empty"); | ||
} | ||
if (codeLength <= 0) { | ||
throw new IllegalArgumentException("codeLength must be greater than 0"); | ||
} | ||
} | ||
|
||
private String createCharacters(Set<CodeType> codeTypeSet) { | ||
StringBuilder characters = new StringBuilder(); | ||
if (codeTypeSet.contains(CodeType.NUMBER)) { | ||
characters.append("0123456789"); | ||
} | ||
if (codeTypeSet.contains(CodeType.ALPHABET_LOWERCASE)) { | ||
characters.append("abcdefghijklmnopqrstuvwxyz"); | ||
} | ||
if (codeTypeSet.contains(CodeType.ALPHABET_UPPERCASE)) { | ||
characters.append("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); | ||
} | ||
return characters.toString(); | ||
} | ||
|
||
public enum CodeType { | ||
NUMBER, ALPHABET_LOWERCASE, ALPHABET_UPPERCASE | ||
} | ||
} |
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
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