Source code: com/eireneh/bible/book/raw/ItemsMem.java
1
2 package com.eireneh.bible.book.raw;
3
4 import java.io.*;
5 import java.util.*;
6
7 /**
8 * ItemsMem is a Base implementation of the Items interface using the in
9 * memory model (Mem).
10 *
11 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
12 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
13 * Distribution Licence:<br />
14 * Project B is free software; you can redistribute it
15 * and/or modify it under the terms of the GNU General Public License,
16 * version 2 as published by the Free Software Foundation.<br />
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.<br />
21 * The License is available on the internet
22 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
23 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
25 * The copyright to this program is held by it's authors.
26 * </font></td></tr></table>
27 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
28 * @see docs.Licence
29 * @author Joe Walker
30 * @version D0.I0.T0
31 */
32 public abstract class ItemsMem extends Mem implements Items
33 {
34 /**
35 * Create a WordResource from a File that contains the dictionary.
36 * @param raw Reference to the RawBible that is using us
37 * @param filename The leaf name to read/write
38 * @param create Should we start all over again
39 */
40 public ItemsMem(RawBible raw, String leafname, boolean create) throws Exception
41 {
42 super(raw, leafname, create);
43 }
44
45 /**
46 * Create a WordResource from a File that contains the dictionary.
47 * @param raw Reference to the RawBible that is using us
48 * @param filename The leaf name to read/write
49 * @param create Should we start all over again
50 * @param messages We append stuff here if something went wrong
51 */
52 public ItemsMem(RawBible raw, String leafname, boolean create, StringBuffer messages)
53 {
54 super(raw, leafname, create, messages);
55 }
56
57 /**
58 * How many items are there in this index?
59 * @return The number of items that we must remember
60 */
61 public abstract int getMaxItems();
62
63 /**
64 * Start all over again and clear the decks for more data.
65 */
66 public void init()
67 {
68 hash = new Hashtable(getMaxItems());
69 array = new String[getMaxItems()];
70 }
71
72 /**
73 * Load the Resource from a stream. This has been renamed from the
74 * default load() to ensure that the custom versions are called.
75 * @param in The stream to read from
76 */
77 protected void defaultLoad(InputStream in) throws IOException, ClassNotFoundException
78 {
79 ObjectInputStream obj_in = new ObjectInputStream(in);
80
81 hash = (Hashtable) obj_in.readObject();
82 array = (String[]) obj_in.readObject();
83 count = obj_in.readInt();
84 obj_in.close();
85 }
86
87 /**
88 * Ensure that all changes to the index of words are written to a
89 * stream. This has been renamed from the default save() to ensure
90 * that the custom versions are called.
91 * @param out The stream to write to
92 */
93 protected void defaultSave(OutputStream out) throws IOException
94 {
95 ObjectOutputStream obj_out = new ObjectOutputStream(out);
96
97 obj_out.writeObject(hash);
98 obj_out.writeObject(array);
99 obj_out.writeInt(count);
100 obj_out.close();
101 }
102
103 /**
104 * Get an Enumeration through the words
105 * @return An Enumeration
106 */
107 public Enumeration getEnumeration()
108 {
109 return hash.keys();
110 }
111
112 /**
113 * Fetch an item from the dictionary by an id.
114 * @param index The id of the word to fetch
115 * @exception NoSuchWordException
116 */
117 public String getItem(int index) throws NoSuchResourceException
118 {
119 try
120 {
121 return array[index];
122 }
123 catch (ArrayIndexOutOfBoundsException ex)
124 {
125 return "#"+index+"#";
126 }
127 }
128
129 /**
130 * This method is called during the creation of the index to add a
131 * word to the index or to get a current id. If the IndexedResource
132 * was created without create=true then we do not create a new id
133 * we just return -1
134 * @param data The word to find/create an id for
135 * @return The (new) id for the item, or -1
136 */
137 public int getIndex(String data)
138 {
139 Object obj = hash.get(data);
140 if (obj != null)
141 return ((Integer) obj).intValue();
142
143 if (create)
144 {
145 // So we have to add the word in
146 array[count] = data;
147 hash.put(data, new Integer(count));
148
149 return count++;
150 }
151 else
152 {
153 return -1;
154 }
155 }
156
157 /**
158 * Set a list of word indexes as the test to a Verse
159 * @param verse The Verse to set the words for
160 * @param data The array of wordd to be indexed
161 */
162 public int[] getIndex(String[] data)
163 {
164 int len = data.length;
165 int[] indexes = new int[len];
166
167 for (int i=0; i<len; i++)
168 {
169 indexes[i] = getIndex(data[i]);
170 }
171
172 return indexes;
173 }
174
175 /**
176 * How many items are there in the current dictionary
177 * @return the Item count
178 */
179 public int size()
180 {
181 return hash.size();
182 }
183
184 /** Map of word to their indexes */
185 protected Hashtable hash;
186
187 /** Converting indexes to Words - this is about the number of words in the Bible */
188 protected String[] array;
189
190 /** The number of items so far */
191 protected int count = 0;
192 }