Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,10 @@ lorem.getCountry();
```
Generates a country name

#### Custom Category

```java
lorem.getCustomValue("nouns");
```
Generates a random value from the seeded file value passed to the Lorem constructor.
If the category is not loaded `CategoryNotExist` exception will be thrown
3 changes: 3 additions & 0 deletions src/main/java/com/thedeanda/lorem/Lorem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thedeanda.lorem;

import com.thedeanda.lorem.exceptions.CategoryNotExist;

import java.time.Duration;
import java.time.LocalDateTime;

Expand Down Expand Up @@ -76,4 +78,5 @@ public interface Lorem {

public LocalDateTime getFutureDate(Duration maxDurationFromNow);

public String getCustomValue(String category) throws CategoryNotExist;
}
127 changes: 95 additions & 32 deletions src/main/java/com/thedeanda/lorem/LoremIpsum.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,52 @@
package com.thedeanda.lorem;

import com.thedeanda.lorem.exceptions.CategoryNotExist;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Map;
import java.util.HashMap;

/**
* The MIT License (MIT)
*
*
* Copyright (c) 2015 Miguel De Anda
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
* @author mdeanda
*
*
*/
public class LoremIpsum implements Lorem {
/*
* this command was useful:
*
*
* cat lorem.txt | sed -e 's/[,;.]//g' | sed -e 's/ /\n/g' | sed -e \
* 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' | sort | \
* uniq > lorem.txt.2
Expand All @@ -57,6 +63,7 @@ public class LoremIpsum implements Lorem {
private List<String> stateFull;
private List<String> cities;
private List<String> countries;
private Map<String, List<String>> customCategories;

private String[] URL_HOSTS = new String[] { "https://www.google.com/#q=%s",
"http://www.bing.com/search?q=%s",
Expand All @@ -77,14 +84,22 @@ public static LoremIpsum getInstance() {
public LoremIpsum() {
this(new Random());
}

public LoremIpsum(Long seed) {
this(seed == null ? new Random() : new Random(seed));
}

public LoremIpsum(Map<String, String> customCategToFile){
this(new Random(), customCategToFile);
}

public LoremIpsum(Random random) {
this(random, null);
}

public LoremIpsum(Random random, Map<String, String> customCategToFile){
this.random = random;

words = readLines("lorem.txt");
maleNames = readLines("male_names.txt");
femaleNames = readLines("female_names.txt");
Expand All @@ -97,6 +112,19 @@ public LoremIpsum(Random random) {
stateAbbr = readLines("state_abbr.txt");
stateFull = readLines("state_full.txt");
countries = readLines("countries.txt");

if(customCategToFile != null && !customCategToFile.isEmpty()){
this.customCategories = loadCustomCategories(customCategToFile);
}
}

private Map<String, List<String>> loadCustomCategories(Map<String, String> customCategToFile){
Map<String, List<String>> customCateg = new HashMap<String, List<String>>();
for(String key : customCategToFile.keySet()){
List<String> customList = readExternalFiles(customCategToFile.get(key));
customCateg.put(key, customList);
}
return customCateg;
}

private List<String> readLines(String file) {
Expand All @@ -123,9 +151,33 @@ private List<String> readLines(String file) {
return ret;
}

private List<String> readExternalFiles(String filePath){
List<String> ret = new ArrayList<String>();
BufferedReader br = null;
try{
Path path = Paths.get(filePath);
br = Files.newBufferedReader(path, StandardCharsets.UTF_8);
String line;
while((line = br.readLine()) != null){
ret.add(line.trim());
}
}catch(IOException ex){
ex.printStackTrace();
}finally {
if(br != null){
try {
br.close();
}catch (IOException ex){
ex.printStackTrace();
}
}
}
return ret;
}

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getCity()
*/
@Override
Expand All @@ -135,7 +187,7 @@ public String getCity() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getCountry()
*/
@Override
Expand All @@ -145,7 +197,7 @@ public String getCountry() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getEmail()
*/
@Override
Expand All @@ -162,7 +214,7 @@ public String getEmail() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getFirstName()
*/
@Override
Expand All @@ -172,7 +224,7 @@ public String getFirstName() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getFirstNameMale()
*/
@Override
Expand All @@ -182,7 +234,7 @@ public String getFirstNameMale() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getFirstNameFemale()
*/
@Override
Expand All @@ -192,7 +244,7 @@ public String getFirstNameFemale() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getLastName()
*/
@Override
Expand All @@ -202,7 +254,7 @@ public String getLastName() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getName()
*/
@Override
Expand All @@ -212,7 +264,7 @@ public String getName() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getNameMale()
*/
@Override
Expand All @@ -222,7 +274,7 @@ public String getNameMale() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getNameFemale()
*/
@Override
Expand All @@ -232,7 +284,7 @@ public String getNameFemale() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getTitle(int)
*/
@Override
Expand All @@ -242,7 +294,7 @@ public String getTitle(int count) {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getTitle(int, int)
*/
@Override
Expand All @@ -261,7 +313,7 @@ private int getCount(int min, int max) {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getHtmlParagraphs(int, int)
*/
@Override
Expand All @@ -278,7 +330,7 @@ public String getHtmlParagraphs(int min, int max) {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getParagraphs(int, int)
*/
@Override
Expand All @@ -303,7 +355,7 @@ public String getParagraphs(int min, int max) {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getUrl()
*/
@Override
Expand All @@ -322,7 +374,7 @@ private String getWords(int min, int max, boolean title) {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getWords(int)
*/
@Override
Expand All @@ -332,7 +384,7 @@ public String getWords(int count) {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getWords(int, int)
*/
@Override
Expand Down Expand Up @@ -366,7 +418,7 @@ private String getRandom(List<String> list) {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getPhone()
*/
@Override
Expand Down Expand Up @@ -394,7 +446,7 @@ public String getPhone() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getStateAbbr()
*/
@Override
Expand All @@ -404,7 +456,7 @@ public String getStateAbbr() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getStateFull()
*/
@Override
Expand All @@ -414,7 +466,7 @@ public String getStateFull() {

/*
* (non-Javadoc)
*
*
* @see com.thedeanda.lorem.Lorem#getZipCode()
*/
@Override
Expand Down Expand Up @@ -442,4 +494,15 @@ public LocalDateTime getFutureDate(Duration maxDurationFromNow) {
return result;
}

@Override
public String getCustomValue(String categoryName) throws CategoryNotExist {
return getRandom(getCustomCategList(categoryName));
}

private List<String> getCustomCategList(String categoryName) throws CategoryNotExist {
if(this.customCategories == null || !this.customCategories.containsKey(categoryName)){
throw new CategoryNotExist(categoryName);
}
return this.customCategories.get(categoryName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.thedeanda.lorem.exceptions;

public class CategoryNotExist extends Exception{

public CategoryNotExist(String categoryName){
super(categoryName + " category is not loaded");
}
}
Loading