KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
11  * <p>
12  * This class stores the information about a single MadLib
13  * fillin.
14  * </p>
15  *
16  * @author Brian Pontarelli
17  */

18 public class MadLibFillin implements Comparable JavaDoc {
19     private boolean word;
20     private int index;
21     private String JavaDoc caption;
22     private String JavaDoc format;
23     private int startIndex;
24     private int endIndex;
25
26
27     /**
28      * Construct a new <code>MadLibfillin</code>
29      */

30     public MadLibFillin(boolean word, int index, String JavaDoc caption, String JavaDoc format,
31             int startIndex, int endIndex) {
32         this.word = word;
33         this.index = index;
34         this.caption = caption;
35         this.startIndex = startIndex;
36         this.endIndex = endIndex;
37     }
38
39
40     /**
41      * Gets the index of the current fillin in the user input
42      *
43      * @return The index
44      */

45     public int getIndex() {
46         return index;
47     }
48
49     /**
50      * Gets whether or not this fillin is for a word or number
51      *
52      * @return True if the fillin is a word, false if it is a number
53      */

54     public boolean isWord() {
55         return word;
56     }
57
58     /**
59      * Gets the caption for this fillin
60      *
61      * @return The caption for this fillin
62      */

63     public String JavaDoc getCaption() {
64         return caption;
65     }
66
67     /**
68      * Gets the format for this fillin
69      *
70      * @return The format for this fillin or null
71      */

72     public String JavaDoc getFormat() {
73         return format;
74     }
75
76     /**
77      * Gets the starting index of the fillin inside the raw text
78      *
79      * @return The starting index
80      */

81     public int getStartIndex() {
82         return startIndex;
83     }
84
85     /**
86      * Gets the ending index of the fillin inside the raw text
87      *
88      * @return The ending index
89      */

90     public int getEndIndex() {
91         return endIndex;
92     }
93
94     /**
95      * Returns the distance between this fillin's index and the given fillin's
96      * index
97      *
98      * @param object The fillin to compare with
99      * @return The distance between the indices
100      */

101     public int compareTo(Object JavaDoc object) {
102         MadLibFillin fillin = (MadLibFillin) object;
103
104         // put numbers before words
105
if (word != fillin.isWord()) {
106             return (word) ? 1 : -1;
107         }
108
109         return index - fillin.getIndex();
110     }
111
112     /**
113      * Determines if this MadLibFillin is equal to the object given.
114      *
115      * @param object The object to compare
116      * @return True if they are equal, false otherwise
117      */

118     public boolean equals(Object JavaDoc object) {
119         if (this == object) {
120             return true;
121         }
122
123         if (!(object instanceof MadLibFillin)) {
124             return false;
125         }
126
127         final MadLibFillin madLibFillin = (MadLibFillin) object;
128
129         return ((index == madLibFillin.index) && (word == madLibFillin.word));
130     }
131
132     /**
133      * Returns a valid hashCode of this MadLibFillin
134      *
135      * @return A valid hashcode
136      */

137     public int hashCode() {
138         int result;
139         result = (word ? 1 : 0);
140         result = 119 * result + index;
141
142         return result;
143     }
144 }
Popular Tags