001: /*
002: * $Id: WordGenerator.java 3364 2006-07-10 10:33:29Z gbevin $ $Revision: 3364 $
003: * $Date: 2006-04-16 15:36:52 +0200 (Sun, 16 Apr 2006) $
004: *
005: * ==================================================================== Licensed
006: * under the Apache License, Version 2.0 (the "License"); you may not use this
007: * file except in compliance with the License. You may obtain a copy of the
008: * License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package model;
019:
020: import com.uwyn.rife.tools.FileUtils;
021: import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
022: import java.io.Serializable;
023: import java.net.URL;
024: import java.util.Arrays;
025: import java.util.Collections;
026: import java.util.List;
027:
028: /**
029: * The word generator is responsible for reading in a list of words from a data
030: * file and serving them up in a random order. The generator keeps a state
031: * record of which words it has served and randomises them again when the last
032: * word has been served.
033: *
034: * @author Chris Turner
035: * @author Geert Bevin
036: * @version 1.0
037: */
038: public class WordGenerator implements Serializable {
039: /** List of words */
040: private final List words;
041:
042: /** Index into words */
043: private int index;
044:
045: /**
046: * Create the word generator, loading the words and preparing them for
047: * serving.
048: */
049: public WordGenerator() {
050: try {
051:
052: URL resource = getClass().getClassLoader().getResource(
053: "model/WordList.txt");
054: final String wordlist = FileUtils.readString(resource);
055: this .words = Arrays.asList(wordlist.split("\\s+"));
056: shuffle();
057: } catch (FileUtilsErrorException e) {
058: throw new RuntimeException("Couldn't read word lis.", e);
059: }
060: }
061:
062: /**
063: * Create the word generator using the supplied array of words as the word
064: * source to use.
065: *
066: * @param words
067: * The words to use
068: */
069: public WordGenerator(final String[] words) {
070: this .words = Arrays.asList(words);
071: shuffle();
072: }
073:
074: /**
075: * Returns the next word from the word generator.
076: *
077: * @return The next word
078: */
079: public Word next() {
080: if (index == words.size()) {
081: shuffle();
082: }
083: return new Word((String) words.get(index++));
084: }
085:
086: /**
087: * Get the number of words that were discovered.
088: *
089: * @return The number of words
090: */
091: public int size() {
092: return words.size();
093: }
094:
095: /**
096: * Randomises the list of loaded words and sets the index back to the
097: * beginning of the word list.
098: */
099: private void shuffle() {
100: Collections.shuffle(words);
101: index = 0;
102: }
103: }
|