org.catools.common.facker.util.CLoremIpsum Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-randomize Show documentation
Show all versions of common-randomize Show documentation
The Common Randomize Implementation
package org.catools.common.facker.util;
import org.apache.commons.text.RandomStringGenerator;
import org.catools.common.collections.CList;
import org.catools.common.collections.CSet;
import org.catools.common.exception.CInvalidRangeException;
import org.catools.common.facker.CRandom;
import org.catools.common.text.CStringUtil;
import java.util.Arrays;
import static org.apache.commons.text.CharacterPredicates.DIGITS;
import static org.apache.commons.text.CharacterPredicates.LETTERS;
public class CLoremIpsum {
private final static CSet specialChars = new CSet<>(Arrays.asList("!?,;......".split(CStringUtil.EMPTY)));
public static String getParagraph(int minWordLength,
int maxWordLength,
int minStatementLength,
int maxStatementLength,
int minParagraphLength,
int maxParagraphLength) {
if (minParagraphLength > maxParagraphLength) {
throw new CInvalidRangeException("Min paragraph length value must be smaller or equal to max paragraph length.");
}
if (minParagraphLength < 0) {
throw new CInvalidRangeException("Both paragraph length range values must be non-negative.");
}
StringBuilder sb = new StringBuilder();
int length = CRandom.Int.next(minParagraphLength, maxParagraphLength);
while (sb.toString().trim().length() < length) {
sb.append(getStatement(minWordLength, maxWordLength, minStatementLength, maxStatementLength).trim() + " ");
}
return sb.toString().trim().substring(0, length - 1) + specialChars.getAny();
}
public static String getParagraphs(int minWordLength,
int maxWordLength,
int minStatementLength,
int maxStatementLength,
int minParagraphLength,
int maxParagraphLength,
int count) {
CList sb = new CList<>();
while (count-- > 0) {
sb.add(getParagraph(minWordLength, maxWordLength, minStatementLength, maxStatementLength, minParagraphLength, maxParagraphLength));
}
return sb.join(" ");
}
public static String getStatement(int minWordLength, int maxWordLength, int minStatementLength, int maxStatementLength) {
if (minStatementLength > maxStatementLength) {
throw new CInvalidRangeException("Min statement length value must be smaller or equal to max statement length.");
}
if (minStatementLength < 0) {
throw new CInvalidRangeException("Both statement length range values must be non-negative.");
}
StringBuilder sb = new StringBuilder();
sb.append(new CList<>("ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(CStringUtil.EMPTY)).getAny());
int length = CRandom.Int.next(minStatementLength, maxStatementLength);
while (sb.toString().trim().length() < length) {
sb.append(getWord(minWordLength, maxWordLength) + " ");
}
return sb.toString().trim().substring(0, length - 1) + specialChars.getAny();
}
public static String getStatements(int minWordLength, int maxWordLength, int minStatementLength, int maxStatementLength, int count) {
CList sb = new CList<>();
while (count-- > 0) {
sb.add(getStatement(minWordLength, maxWordLength, minStatementLength, maxStatementLength));
}
return sb.join(" ");
}
public static String getWord(int minWordLength, int maxWordLength) {
if (minWordLength > maxWordLength) {
throw new CInvalidRangeException("Min word length value must be smaller or equal to max word length.");
}
if (minWordLength < 0) {
throw new CInvalidRangeException("Both word length range values must be non-negative.");
}
return new RandomStringGenerator.Builder().withinRange('a', 'z').filteredBy(LETTERS, DIGITS).build().generate(minWordLength, maxWordLength);
}
public static String getWords(int minWordLength, int maxWordLength, int count) {
CList sb = new CList<>();
while (count-- > 0) {
sb.add(getWord(minWordLength, maxWordLength));
}
return sb.join(" ");
}
}