KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > examples > madlib > MadLib


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.examples.madlib;
8
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.SortedSet JavaDoc;
13 import java.util.TreeSet JavaDoc;
14
15
16 /**
17  * <p>
18  * This describes a MadLib and can also be used to generate
19  * the final MadLib after all the input has been taken.
20  * </p>
21  *
22  * @author Brian Pontarelli
23  */

24 public class MadLib {
25
26     private String JavaDoc rawText;
27     private List JavaDoc fillins;
28     private MadLibFillin[] display;
29     private int numberOfWords;
30     private int numberOfNumbers;
31
32
33     /**
34      * Construsts a new <code>MadLib</code> with the given raw text and fillin
35      * objects
36      *
37      * @param rawText The raw text of the MadLib
38      * @param fillins A List of fillin objects
39      */

40     public MadLib(String JavaDoc rawText, List JavaDoc fillins) {
41         this.rawText = rawText;
42         this.fillins = fillins;
43
44         SortedSet JavaDoc set = new TreeSet JavaDoc(fillins);
45         this.display = (MadLibFillin []) set.toArray(new MadLibFillin[set.size()]);
46
47         // count the number of words and numbers
48
this.numberOfNumbers = 0;
49         this.numberOfWords = 0;
50         for (int i = 0; i < display.length; i++) {
51             if (display[i].isWord()) {
52                 numberOfWords++;
53             } else {
54                 numberOfNumbers++;
55             }
56         }
57     }
58
59
60     /**
61      * Returns the raw text of the MadLib
62      *
63      * @return The raw text
64      */

65     public String JavaDoc getRawText() {
66         return rawText;
67     }
68
69     /**
70      * Returns the total number of numbers required
71      *
72      * @return The total number of numbers required
73      */

74     public int getNumberOfNumbers() {
75         return numberOfNumbers;
76     }
77
78     /**
79      * Returns the total number of words required
80      *
81      * @return The total number of words required
82      */

83     public int getNumberOfWords() {
84         return numberOfWords;
85     }
86
87     /**
88      * Returns the fillin at the given index
89      *
90      * @param index The index in the list of MadLibFillin objects to return
91      * @return The MadLibFillin or null if the index is invalid
92      */

93     public MadLibFillin getMadLibFillin(int index) {
94         if (index < 0 || index > fillins.size()) {
95             return null;
96         }
97
98         return (MadLibFillin) fillins.get(index);
99     }
100
101     /**
102      * Returns a copy of the complete list of MadLibFillins, in the order they
103      * are specified in the raw text String
104      *
105      * @return A copy of the fillin list
106      */

107     public List JavaDoc getRawMadLibFillins() {
108         return new ArrayList JavaDoc(fillins);
109     }
110
111     /**
112      * Returns a copy of the MadLibFillin list, with the duplicates thrown out
113      * and then sorted by numbers/words and then by index
114      *
115      * @return A copy of the fillin list, sorted
116      */

117     public MadLibFillin [] getDisplayMadLibFillins() {
118         return display;
119     }
120
121     /**
122      * Returns the MadLibFillin at the given index and word boolean
123      *
124      * @param index The index of the fillin
125      * @param word Determines if the fillin is a word or not
126      * @return The MadLibFillin or null
127      */

128     public MadLibFillin getMadLibFillin(int index, boolean word) {
129         MadLibFillin fillin = null;
130         for (int i = 0; i < display.length; i++) {
131             if (display[i].getIndex() == index && display[i].isWord() == word) {
132                 fillin = display[i];
133             }
134         }
135
136         return fillin;
137     }
138 }
Popular Tags