/* * Copyright © 2013 - Elliott Frisch * * THIS SOFTWARE IS PROVIDED UNDER THE CREATIVE COMMONS * LICENSE 3.0 "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR * A PARTICULAR PURPOSE. * * To use this software you must agree to the complete * license terms available at: * http://creativecommons.org/licenses/by/3.0/us/deed.en_US * * It is the intent of the author(s) that you may use or * modify this software for any purpose (including your own * commercial gain) provided that this notice remains in its * entirety. * * Created by Elliott Frisch - www.frischcode.com */ package com.frischcode.util; import java.security.SecureRandom; import java.util.Iterator; import java.util.Random; import java.util.Set; import java.util.TreeSet; /** * <b>Rationale:</b> You need dynamic text that stresses * your application. Or, you just need to fill in a few * blanks. Or, you've always loved <a * href="http://www.madlibs.com/">mad libs</a>. * * @author Elliott Frisch */ public class LoremIpsumGenerator { public LoremIpsumGenerator() { this(false); } public LoremIpsumGenerator(boolean classic) { super(); random.setSeed(System.currentTimeMillis()); addAll(colorSet, "red", "white", "blue", "purple", "pink", "orange", "green", "yellow"); addAll(verbSet, "walked", "ran", "talked", "jogged", "hiked", "mozied"); addAll(verbSet2, "passed a", "saw a", "didn't see the", "was surprised by a", "was startled by a", "was amazed by a", "was impressed by a"); addAll(nounSet, "Tom", "Jim", "Joe", "Bill", "Bob", "Jane", "Jill", "Sam"); addAll(placeSet, "the house", "work", "the store", "the gym", "the pool", "the library", "the daycare", "Walla Walla, WA"); addAll(nounSet2, "boat", "car", "house", "plane", "bicycle", "moped", "motorcycle"); addAll(punctSet, "!", "?", ".", "\u203d"); this.classic = classic; } public String getText() { if (classic) { return A_WELL_KNOWN_STRING; } StringBuilder sb = new StringBuilder(); sb.append(getRandomEntry(random, nounSet)); sb.append(" "); sb.append(getRandomEntry(random, verbSet)); sb.append(" to "); sb.append(getRandomEntry(random, placeSet)); sb.append(" and "); sb.append(getRandomEntry(random, verbSet2)); sb.append(" "); sb.append(getRandomEntry(random, colorSet)); sb.append(" "); sb.append(getRandomEntry(random, nounSet2)); sb.append(getRandomEntry(random, punctSet)); return sb.toString(); } public boolean isClassic() { return classic; } public void setClassic(boolean classic) { this.classic = classic; } public static void main(String[] args) { LoremIpsumGenerator lig = new LoremIpsumGenerator( false); for (int i = 0; i < 10; ++i) { System.out.println(lig.getText()); } System.out.flush(); } protected boolean classic = false; private Set<String> colorSet = new TreeSet<String>(); private Set<String> verbSet = new TreeSet<String>(); private Set<String> verbSet2 = new TreeSet<String>(); private Set<String> nounSet = new TreeSet<String>(); private Set<String> nounSet2 = new TreeSet<String>(); private Set<String> placeSet = new TreeSet<String>(); private Set<String> punctSet = new TreeSet<String>(); private Random random = new SecureRandom(); private static String getRandomEntry(Random random, Set<String> set) { if (set == null || set.size() == 0) { return ""; } Iterator<String> iter = set.iterator(); int p = random.nextInt(set.size()); while (p > 0) { iter.next(); --p; } return iter.next(); } private static void addAll(Set<String> set, String... in) { for (String t : in) { set.add(t); } } private static final String A_WELL_KNOWN_STRING = // "Lorem ipsum dolor sit amet, consectetur adipisici elit, " + "sed do eiusmod tempor incididunt ut labore et dolore " + "magna aliqua. Ut enim ad minim veniam, quis nostrud " + "exercitation ullamco laboris nisi ut aliquip ex ea " + "commodo consequat. Duis aute irure dolor in " + "reprehenderit in voluptate velit esse cillum dolore " + "eu fugiat nulla pariatur. Excepteur sint occaecat " + "cupidatat non proident, sunt in culpa qui officia " + "deserunt mollit anim id est laborum."; }
Code Pretty Print Script
Thursday, November 14, 2013
Mocking Up Text
Do you need to generate a significant ammount of text dynamically? Perhaps you're creating mock data to test your UI; maybe you've been using Lorem Ipsum; perhaps you know about "=rand()" in Word; or you could try a LoremIpsumGenerator.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment