Source code: com/virtuosotechnologies/asaph/modelutils/impl/BlockListData.java
1 /*
2 ================================================================================
3
4 FILE: BlockListData.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 Raw data of a list of blocks
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
53 /**
54 * Raw data of a list of blocks
55 */
56 public class BlockListData
57 implements Serializable
58 {
59 private static final String LINE_DELIMITER = System.getProperty("line.separator");
60
61 public static class BlockData
62 {
63 private LineListData data_;
64 private int indent_;
65
66
67 private BlockData(
68 LineListData data,
69 int indent)
70 {
71 data_ = data;
72 indent_ = indent;
73 }
74
75
76 public LineListData getLineListData()
77 {
78 return data_;
79 }
80
81
82 public int getIndent()
83 {
84 return indent_;
85 }
86 }
87
88
89 private int length_;
90 private transient BlockData[] array_;
91
92
93 public BlockListData()
94 {
95 length_ = 0;
96 array_ = new BlockData[10];
97 }
98
99
100 public void addBlock(
101 LineListData data,
102 int indent)
103 {
104 if (length_ == array_.length)
105 {
106 BlockData[] narray = new BlockData[length_*2+1];
107 System.arraycopy(array_, 0, narray, 0, length_);
108 array_ = narray;
109 }
110 array_[length_++] = new BlockData(data, indent);
111 }
112
113
114 public Iterator iterator()
115 {
116 return new Iterator()
117 {
118 private int index_ = 0;
119
120 public boolean hasNext()
121 {
122 return index_ < length_;
123 }
124
125 public Object next()
126 {
127 if (index_ >= length_)
128 {
129 throw new NoSuchElementException();
130 }
131 return array_[index_++];
132 }
133
134 public void remove()
135 {
136 throw new UnsupportedOperationException();
137 }
138 };
139 }
140
141
142 public int size()
143 {
144 return length_;
145 }
146
147
148 public String createStringRepresentation()
149 {
150 StringBuffer buf = new StringBuffer();
151 for (int i=0; i<length_; ++i)
152 {
153 buf.append(array_[i].getLineListData().createStringRepresentation());
154 buf.append(LINE_DELIMITER);
155 }
156 return new String(buf);
157 }
158
159
160 private synchronized void writeObject(
161 ObjectOutputStream s)
162 throws IOException
163 {
164 s.defaultWriteObject();
165 s.writeInt(array_.length);
166 for (int i=0; i<length_; i++)
167 {
168 s.writeObject(array_[i].getLineListData());
169 s.writeInt(array_[i].getIndent());
170 }
171 }
172
173
174 private synchronized void readObject(
175 ObjectInputStream s)
176 throws
177 IOException,
178 ClassNotFoundException
179 {
180 s.defaultReadObject();
181 int arrayLength = s.readInt();
182 array_ = new BlockData[arrayLength];
183 for (int i=0; i<length_; i++)
184 {
185 LineListData data = (LineListData)s.readObject();
186 int indent = s.readInt();
187 array_[i] = new BlockData(data, indent);
188 }
189 }
190 }