Skip to content
This repository has been archived by the owner on Jan 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from jansu76/master
Browse files Browse the repository at this point in the history
optimized performance by using apache commons randomstringutils
  • Loading branch information
petkivim committed Oct 23, 2015
2 parents 86ce8bd + dae4344 commit 7902f3b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@
<version>${log4j.version}</version>
</dependency>

<!-- commons lang for random strings -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.RandomStringUtils;

/**
* This class offers helper methods for the application.
Expand All @@ -12,7 +13,6 @@
public class ApplicationHelper {

private static final Logger logger = LoggerFactory.getLogger(ApplicationHelper.class);
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";

/**
* Returns a random string of given length.
Expand All @@ -22,13 +22,9 @@ public class ApplicationHelper {
*/
public static String getRandomString(int length) {
logger.debug("Generate random string of {} charaters.", length);
Random random = new Random();
StringBuilder sb = new StringBuilder();
while (sb.toString().getBytes().length < length) {
sb.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
}
logger.debug("String generated.");
return sb.toString();
String s = RandomStringUtils.randomAlphanumeric(length);
logger.debug("String generated");
return s;
}

/**
Expand Down

0 comments on commit 7902f3b

Please sign in to comment.