Commit ff8db080 authored by John Vester's avatar John Vester
Browse files

Version 1.5 updates, introducing RandomGeneratorData

parent 181a836a
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -89,4 +89,13 @@ RATING_LEVEL_HIGH = enables the most aggressive preference toward selecting high
The ratingLevel option provides more granularity than the original useRating Boolean option. Use of the useRating Boolean
is still supported and utilizes the RATING_LEVEL_LOW weighting logic.
Created by [John Vester](https://www.linkedin.com/in/johnjvester), because I truly enjoy writing code.
### About `RandomGeneratorData`
New with RandomGenerator 1.5 is the ability to have RandomGenerator return a random list of data via the `RandomGeneratorData` class.
This functionality is currently limited to standard (English) names with a return `List<String>` size limited to the maximum size of a
`java.lang.Integer`. The names can be returned in `NameFormat.FIRST_LAST` (John Doe) format or `NameFormat.LAST_FIRST` (Doe, John) format.
Please review the JavaDocs for additional information on this feature.
Created by [John Vester](https://www.linkedin.com/in/johnjvester), because I truly ♥ writing code.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gitlab.johnjvester</groupId>
<artifactId>random-generator</artifactId>
<version>1.4-SNAPSHOT</version>
<version>1.4</version>
<packaging>jar</packaging>
......@@ -43,7 +42,7 @@
</licenses>
<scm>
<url>https://gitlab.com/johnjvester/RandomGenerator/tags/random-generator-1.3</url>
<url>https://gitlab.com/johnjvester/RandomGenerator/tags/random-generator-1.4</url>
</scm>
<dependencies>
......
#release configuration
#Thu Feb 23 09:46:32 EST 2017
scm.tag=random-generator-1.4
scm.url=scm\:svn\:https\://svn.sonatype.org/spice/tags/oss-parent-7/random-generator
pushChanges=true
preparationGoals=clean verify
project.scm.com.gitlab.johnjvester\:random-generator.developerConnection=scm\:svn\:https\://svn.sonatype.org/spice/tags/oss-parent-7/random-generator
project.dev.com.gitlab.johnjvester\:random-generator=1.5-SNAPSHOT
project.rel.com.gitlab.johnjvester\:random-generator=1.4
project.scm.com.gitlab.johnjvester\:random-generator.tag=HEAD
remoteTagging=true
scm.commentPrefix=[maven-release-plugin]
project.scm.com.gitlab.johnjvester\:random-generator.url=https\://gitlab.com/johnjvester/RandomGenerator/tags/random-generator-1.3
exec.additionalArguments=-Psonatype-oss-release -P default,ossrh
completedPhase=run-preparation-goals
project.scm.com.gitlab.johnjvester\:random-generator.connection=scm\:svn\:http\://svn.sonatype.org/spice/tags/oss-parent-7/random-generator
package com.gitlab.johnjvester.randomizer;
public enum ErrorCode {
UNKNOWN_ERROR,
UNKNOWN_NAME_FORMAT
}
......@@ -7,6 +7,7 @@ import org.yaml.snakeyaml.Yaml;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
public class Main {
......@@ -23,6 +24,8 @@ public class Main {
* <ul>
* <li>-delimiter = the custom delimiter to use</li>
* <li>-returnSize = the number of elements to return</li>
* <li>-randomData = used to call RandomGeneratorData options</li>
* <li>-nameFormat = one of the possible NameFormat enum options</li>
* </ul>
* <p>examples:</p>
* <ul>
......@@ -38,15 +41,18 @@ public class Main {
* <li>java -jar random-generator-version One^^Two^^Three^^Four^^Five^^ -delimiter ^^ -returnSize 3<br>
* Randomizes a list of five elements using a custom delimiter (^^), returning only three elements
* </li>
* <li>java -jar random-generator-version -randomData fullNames -nameFormat FIRST_LAST -returnSize 101<br>
* Returns a list of 101 random full names in first name last name format.
* </li>
* </ul>
*
* @param args requires at least the listString object, but can also include -delimiter and -returnSize run-time parameters as well
* @throws FileNotFoundException a File Not Found Exception will be returned if the properties.yml file does not exist
* @param args see examples for possible usage options.
* @since 1.0
*/
public static void main(String[] args) throws FileNotFoundException {
public static void main(String[] args) {
InputStream is = null;
RandomGenerator randomGenerator = new RandomGenerator();
RandomGeneratorData randomGeneratorData = new RandomGeneratorData();
try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
......@@ -138,6 +144,43 @@ public class Main {
writeMessage("Randomized string:", false);
writeMessage(returnString, false);
} else if (args != null && args.length == 6) {
String randomDataMode = null;
String dataType = null;
NameFormat nameFormat = null;
Integer returnSize = null;
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-randomData")) {
randomDataMode = "randomData";
i++;
if (args[i].equals("fullNames")) {
dataType = "fullNames";
}
} else if (args[i].equals("-nameFormat")) {
i++;
if (NameFormat.valueOf(args[i]).equals(NameFormat.FIRST_LAST) || NameFormat.valueOf(args[i]).equals(NameFormat.LAST_FIRST)) {
nameFormat = NameFormat.valueOf(args[i]);
}
} else if (args[i].equals("-returnSize")) {
i++;
returnSize = Integer.valueOf(args[i]);
}
}
if (randomDataMode == null || dataType == null || nameFormat == null || returnSize == null) {
getHelpText(randomGenerator.DEFAULT_DELIMITER);
}
writeMessage("Returning a random list of full names containing " + args[5] + " value(s) in " + args[3] + " format.", true);
List<String> fullNames = randomGeneratorData.randomizeFullNames(nameFormat, returnSize);
returnString = String.join("\n", fullNames);
logger.debug("returnString = " + returnString);
writeMessage("Randomized names:", false);
writeMessage(returnString, false);
} else {
getHelpText(randomGenerator.DEFAULT_DELIMITER);
}
......@@ -156,7 +199,6 @@ public class Main {
}
}
}
}
private static void getHelpText(String defaultDelimiter) {
......@@ -176,12 +218,15 @@ public class Main {
writeMessage(" Randomizes a list of five elements using the default delimiter (" + defaultDelimiter + "), returning only three elements", true);
writeMessage(" java -jar random-generator-version One^^Two^^Three^^Four^^Five^^ -delimiter ^^ -returnSize 3", false);
writeMessage(" Randomizes a list of five elements using a custom delimiter (^^), returning only three elements", true);
writeMessage(" java -jar random-generator-version -randomData fullNames -nameFormat FIRST_LAST -returnSize 101", false);
writeMessage(" Returns a list of 101 random full names in first name last name format", true);
}
private static void writeMessage(String message, boolean newLine) {
System.out.println(message);
if (newLine) {
System.out.println("");
System.out.println();
}
}
}
package com.gitlab.johnjvester.randomizer;
public enum NameFormat {
FIRST_LAST,
LAST_FIRST
}
package com.gitlab.johnjvester.randomizer;
import java.util.ArrayList;
import java.util.List;
/**
* Static data objects.
*/
public class RandomData {
/**
* Returns a list of unique first names.
*
* @return {@link List}<{@link String}>
*/
public static List<String> getFirstNames() {
List<String> list = new ArrayList<>();
list.add("Abhi");
list.add("Adam");
list.add("Alex");
list.add("Alice");
list.add("Audrey");
list.add("Bella");
list.add("Billie");
list.add("Bob");
list.add("Bobby");
list.add("Brad");
list.add("Brian");
list.add("Carol");
list.add("Cassidy");
list.add("Chang");
list.add("Chris");
list.add("Christie");
list.add("Cindy");
list.add("Craig");
list.add("Dallas");
list.add("Daly");
list.add("Dana");
list.add("Daniel");
list.add("Danyel");
list.add("Dayna");
list.add("Darren");
list.add("Dave");
list.add("David");
list.add("Debbie");
list.add("Denver");
list.add("Drew");
list.add("Dylan");
list.add("Eduardo");
list.add("Eric");
list.add("Ethan");
list.add("Finnegan");
list.add("Gail");
list.add("Greg");
list.add("Gretchen");
list.add("Harshini");
list.add("Heather");
list.add("Hershell");
list.add("Ila");
list.add("Indira");
list.add("Jackie");
list.add("James");
list.add("Jan");
list.add("Jeff");
list.add("Jim");
list.add("John");
list.add("Jon");
list.add("Jonathan");
list.add("June");
list.add("Justin");
list.add("Kalyani");
list.add("Karen");
list.add("Kristen");
list.add("Laverne");
list.add("Lisa");
list.add("Liz");
list.add("Marsha");
list.add("Maureen");
list.add("Mia");
list.add("Michelle");
list.add("Natalie");
list.add("Nicole");
list.add("Paducah");
list.add("Pamela");
list.add("Peter");
list.add("Phoenix");
list.add("Praveena");
list.add("Preetham");
list.add("Randie");
list.add("Rick");
list.add("Rodney");
list.add("Ronnie");
list.add("Russell");
list.add("Sam");
list.add("Sarah");
list.add("Seth");
list.add("Shirley");
list.add("Stacy");
list.add("Sydney");
list.add("Tamara");
list.add("Tina");
list.add("Tim");
list.add("Todd");
list.add("Toni");
list.add("Tracy");
list.add("Trevor");
list.add("Verona");
list.add("Veronica");
list.add("William");
list.add("Zach");
return list;
}
/**
* Returns a list of unique last names.
*
* @return {@link List}<{@link String}>
*/
public static List<String> getLastNames() {
List<String> list = new ArrayList<>();
list.add("Adams");
list.add("Allen");
list.add("Anderson");
list.add("Andrews");
list.add("Bailey");
list.add("Baker");
list.add("Barnes");
list.add("Bell");
list.add("Brown");
list.add("Burch");
list.add("Coleman");
list.add("Cook");
list.add("Cooper");
list.add("Davis");
list.add("Foster");
list.add("Gray");
list.add("Green");
list.add("Hall");
list.add("Hill");
list.add("Henderson");
list.add("Jenkins");
list.add("Johnson");
list.add("Jones");
list.add("King");
list.add("Lee");
list.add("Li");
list.add("Long");
list.add("Marshall");
list.add("Miller");
list.add("Mills");
list.add("Moore");
list.add("Morgan");
list.add("Morris");
list.add("Murphy");
list.add("Perry");
list.add("Powell");
list.add("Price");
list.add("Reed");
list.add("Rogers");
list.add("Scott");
list.add("Smith");
list.add("Stewart");
list.add("Taylor");
list.add("Thomas");
list.add("Thompson");
list.add("Walsh");
list.add("Williams");
list.add("Wong");
list.add("Wood");
list.add("Wright");
list.add("Young");
return list;
}
}
......@@ -146,7 +146,7 @@ public class RandomGenerator<T> {
*
* @param tList
* @param ratingLevel
* @return
* @return {@link List}
* @since 1.4
*/
public List<T> randomize(List<T> tList, int ratingLevel) {
......@@ -387,7 +387,7 @@ public class RandomGenerator<T> {
}
private List<T> convertToGenericList(List<RandomListItem> randomListItems) {
List<T> tList = new ArrayList<T>();
List<T> tList = new ArrayList<>();
for (RandomListItem randomListItem : randomListItems) {
tList.add(randomListItem.thisT);
......
package com.gitlab.johnjvester.randomizer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class RandomGeneratorData {
final static Logger logger = LogManager.getLogger(RandomGenerator.class);
RandomGenerator randomGenerator = new RandomGenerator();
/**
* For a given {@link Integer} returnSize, return a list of random full names using
* the specified {@link NameFormat} nameFormat.
*
* @param nameFormat {@link NameFormat} nameFormat to utilize
* @param returnSize {@link Integer} number of names to return
* @return {@link List}<{@link String}>
* @throws RandomGeneratorException when the {@link NameFormat} cannot be determined
*/
public List<String> randomizeFullNames(NameFormat nameFormat, Integer returnSize) throws RandomGeneratorException {
logger.debug("getting random list of " + returnSize + " full names");
List<String> fullNames = new ArrayList<>();
List<String> firstNames = randomGenerator.randomize(getFirstNames(returnSize));
List<String> lastNames = randomGenerator.randomize(getLastNames(returnSize));
for (int i = 0; i < (returnSize); i++) {
String firstName = firstNames.get(i);
String lastName = lastNames.get(i);
if (nameFormat != null) {
if (nameFormat.equals(NameFormat.FIRST_LAST)) {
fullNames.add(firstName + " " + lastName);
} else if (nameFormat.equals(NameFormat.LAST_FIRST)) {
fullNames.add(lastName + ", " + firstName);
}
} else {
throw new RandomGeneratorException(ErrorCode.UNKNOWN_NAME_FORMAT, "The nameformat parameter cannot be null.");
}
}
logger.debug("fullNames.size() = " + fullNames.stream());
return fullNames;
}
private List<String> getFirstNames(Integer returnSize) {
List<String> firstNames = RandomData.getFirstNames();
logger.debug("returnSize = " + returnSize);
logger.debug("firstNames.size() = " + firstNames.size());
if (returnSize > firstNames.size()) {
double value = returnSize / firstNames.size();
logger.debug("value = " + value);
for (int i = 0; i < (int) value; i++) {
firstNames.addAll(RandomData.getFirstNames());
}
}
return firstNames.stream().limit(returnSize).collect(Collectors.toList());
}
private List<String> getLastNames(Integer returnSize) {
List<String> lastNames = RandomData.getLastNames();
logger.debug("returnSize = " + returnSize);
logger.debug("lastNames.size() = " + lastNames.size());
if (returnSize > lastNames.size()) {
double value = returnSize / lastNames.size();
logger.debug("value = " + value);
for (int i = 0; i < (int) value; i++) {
lastNames.addAll(RandomData.getLastNames());
}
}
return lastNames.stream().limit(returnSize).collect(Collectors.toList());
}
}
package com.gitlab.johnjvester.randomizer;
import lombok.Data;
@Data
public class RandomGeneratorException extends Exception {
private ErrorCode errorCode;
private String message;
public RandomGeneratorException() {
this.errorCode = ErrorCode.UNKNOWN_ERROR;
}
public RandomGeneratorException(ErrorCode errorCode) {
this.errorCode = errorCode;
}
public RandomGeneratorException(ErrorCode errorCode, String message) {
this.errorCode = errorCode;
this.message = message;
}
}
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
---
version: 1.4
version: 1.5
......@@ -27,7 +27,6 @@ public class MainTest {
System.setErr(new PrintStream(errContent));
InputStream is = null;
RandomGenerator randomGenerator = new RandomGenerator();
try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
......@@ -60,237 +59,180 @@ public class MainTest {
@Test
public void testWithoutAnyArgs() {
try {
String expectedResult = "\n"
+ "RandomGenerator version " + currentVersion + "\n"
+ "\n"
+ "Usage: java -jar random-generator-version listString [options]\n"
+ "\n"
+ " where:\n"
+ " listString = a concatenated list using either the default delimiter (~~~)\n"
+ " or the value of the delimiterString provided.\n"
+ "\n"
+ " options:\n"
+ " -delimiter = the custom delimiter to use.\n"
+ "\n"
+ " -returnSize = the number of elements to return.\n"
+ "\n"
+ " examples:\n"
+ " java -jar random-generator-version One~~~Two~~~Three~~Four~~~Five~~~\n"
+ " Randomizes a list of five elements using the default delimiter (~~~)\n"
+ "\n"
+ " java -jar random-generator-version One^^Two^^Three^^Four^^Five^^ -delimiter ^^\n"
+ " Randomizes a list of five elements using a custom delimiter (^^)\n"
+ "\n"
+ " java -jar random-generator-version One~~~Two~~~Three~~~Four~~~Five~~~ -returnSize 3\n"
+ " Randomizes a list of five elements using the default delimiter (~~~), returning only three elements\n"
+ "\n"
+ " java -jar random-generator-version One^^Two^^Three^^Four^^Five^^ -delimiter ^^ -returnSize 3\n"
+ " Randomizes a list of five elements using a custom delimiter (^^), returning only three elements\n"
+ "\n";
String[] args = null;
Main.main(args);
String result = outContent.toString();
assertEquals(expectedResult, result);
} catch (FileNotFoundException e) {
fail();
}
String expectedResult = "\n"
+ "RandomGenerator version " + currentVersion + "\n"