Source code: com/virtuosotechnologies/asaph/modelutils/impl/LineFragmentData.java
1 /*
2 ================================================================================
3
4 FILE: LineFragmentData.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 Raw data of part of a line
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.asaph.modelutils.impl;
43
44
45 import java.io.ObjectOutputStream;
46 import java.io.ObjectInputStream;
47 import java.io.IOException;
48 import java.io.Serializable;
49 import java.util.Iterator;
50 import java.util.NoSuchElementException;
51
52 import com.virtuosotechnologies.asaph.model.Song;
53 import com.virtuosotechnologies.asaph.model.ChordSet;
54
55
56 /**
57 * Raw data of part of a line
58 */
59 public class LineFragmentData
60 implements Serializable
61 {
62 public static class TextData
63 implements Serializable
64 {
65 private String data_;
66
67 public TextData(
68 String data)
69 {
70 data_ = data;
71 }
72
73 public String getData()
74 {
75 return data_;
76 }
77 }
78
79 public static class CommentData
80 implements Serializable
81 {
82 private String data_;
83
84 public CommentData(
85 String data)
86 {
87 data_ = data;
88 }
89
90 public String getData()
91 {
92 return data_;
93 }
94 }
95
96 public static class UntypedStringData
97 implements Serializable
98 {
99 private String data_;
100
101 public UntypedStringData(
102 String data)
103 {
104 data_ = data;
105 }
106
107 public String getData()
108 {
109 return data_;
110 }
111 }
112
113 public static class ChordData
114 implements Serializable
115 {
116 private String chordSetID_;
117 private String pre_;
118 private String main_;
119 private String post_;
120 private boolean isCurrentChordSet_;
121
122 public ChordData(
123 ChordSet chordSet,
124 String pre,
125 String main,
126 String post,
127 boolean isCurrentChordSet)
128 {
129 chordSetID_ = chordSet.getSerializableID();
130 pre_ = pre;
131 main_ = main;
132 post_ = post;
133 isCurrentChordSet_ = isCurrentChordSet;
134 }
135
136 public ChordSet getChordSetIn(
137 Song song)
138 {
139 return song.getChordSetForSerializableID(chordSetID_);
140 }
141
142 public boolean isCurrentChordSet()
143 {
144 return isCurrentChordSet_;
145 }
146
147 public String getPre()
148 {
149 return pre_;
150 }
151
152 public String getMain()
153 {
154 return main_;
155 }
156
157 public String getPost()
158 {
159 return post_;
160 }
161 }
162
163
164 private int length_;
165 private transient Object[] array_;
166
167
168 public LineFragmentData()
169 {
170 length_ = 0;
171 array_ = new Object[10];
172 }
173
174
175 public void addText(
176 String text)
177 {
178 addImpl(new TextData(text));
179 }
180
181
182 public void addComment(
183 String text)
184 {
185 addImpl(new CommentData(text));
186 }
187
188
189 public void addUntypedString(
190 String text)
191 {
192 addImpl(new UntypedStringData(text));
193 }
194
195
196 public void addChord(
197 ChordSet chordSet,
198 String pre,
199 String main,
200 String post,
201 boolean isCurrentChordSet)
202 {
203 addImpl(new ChordData(chordSet, pre, main, post, isCurrentChordSet));
204 }
205
206
207 private void addImpl(
208 Object obj)
209 {
210 if (length_ == array_.length)
211 {
212 Object[] narray = new Object[length_*2+1];
213 System.arraycopy(array_, 0, narray, 0, length_);
214 array_ = narray;
215 }
216 array_[length_++] = obj;
217 }
218
219
220 public Iterator iterator()
221 {
222 return new Iterator()
223 {
224 private int index_ = 0;
225
226 public boolean hasNext()
227 {
228 return index_ < length_;
229 }
230
231 public Object next()
232 {
233 if (index_ >= length_)
234 {
235 throw new NoSuchElementException();
236 }
237 return array_[index_++];
238 }
239
240 public void remove()
241 {
242 throw new UnsupportedOperationException();
243 }
244 };
245 }
246
247
248 public int size()
249 {
250 return length_;
251 }
252
253
254 public String createStringRepresentation()
255 {
256 StringBuffer buf = new StringBuffer();
257 for (int i=0; i<length_; ++i)
258 {
259 Object obj = array_[i];
260 if (obj instanceof TextData)
261 {
262 buf.append(((TextData)obj).getData());
263 }
264 else if (obj instanceof CommentData)
265 {
266 buf.append(((CommentData)obj).getData());
267 }
268 }
269 return new String(buf);
270 }
271
272
273 private synchronized void writeObject(
274 ObjectOutputStream s)
275 throws IOException
276 {
277 s.defaultWriteObject();
278 s.writeInt(array_.length);
279 for (int i=0; i<length_; i++)
280 {
281 s.writeObject(array_[i]);
282 }
283 }
284
285
286 private synchronized void readObject(
287 ObjectInputStream s)
288 throws
289 IOException,
290 ClassNotFoundException
291 {
292 s.defaultReadObject();
293 int arrayLength = s.readInt();
294 array_ = new Object[arrayLength];
295 for (int i=0; i<length_; i++)
296 {
297 array_[i] = s.readObject();
298 }
299 }
300 }