Source code: com/memoire/fu/FuVectorPublic.java
1 /**
2 * @modification $Date: 2002/12/16 18:56:26 $
3 * @statut unstable
4 * @file FuVectorPublic.java
5 * @version 0.36
6 * @author Guillaume Desnoix
7 * @email guillaume@desnoix.com
8 * @license GNU General Public License 2 (GPL2)
9 * @copyright 1998-2001 Guillaume Desnoix
10 */
11
12 package com.memoire.fu;
13
14 import com.memoire.fu.*;
15
16
17 import java.io.*;
18 import java.util.*;
19
20 /**
21 * A simple vector with public fields.
22 * Needed for Yapod serialization.
23 */
24 public class FuVectorPublic
25 implements Cloneable, Serializable
26 {
27 public Object data_[];
28 public int count_;
29 public int increment_;
30
31 public FuVectorPublic(int _capacity,int _increment)
32 {
33 super();
34 data_ =new Object[_capacity];
35 increment_=_increment;
36 }
37
38 public FuVectorPublic(int _capacity)
39 {
40 this(_capacity,0);
41 }
42
43 public FuVectorPublic()
44 {
45 this(11,0);
46 }
47
48 public final synchronized void copyInto(Object[] _array)
49 {
50 for(int i=0;i<count_;i++)
51 _array[i]=data_[i];
52 }
53
54 public final synchronized void trimToSize()
55 {
56 int old_capacity=data_.length;
57 if(count_<old_capacity)
58 {
59 Object[] old_data=data_;
60 data_=new Object[count_];
61 System.arraycopy(old_data,0,data_,0,count_);
62 }
63 }
64
65 public final synchronized void ensureCapacity(int _min)
66 {
67 if(_min>data_.length)
68 resize0(_min);
69 }
70
71 private void resize0(int _min)
72 {
73 int old_capacity=data_.length;
74 Object[] old_data =data_;
75
76 int new_capacity=
77 ( (increment_>0)
78 ? (old_capacity+increment_)
79 : (old_capacity*2));
80
81 if(new_capacity<_min)
82 new_capacity=_min;
83
84 data_=new Object[new_capacity];
85 System.arraycopy(old_data,0,data_,0,count_);
86 }
87
88 public final synchronized void setSize(int _new)
89 {
90 if((_new>count_)&&(_new>data_.length))
91 resize0(_new);
92 else
93 for(int i=_new;i<count_;i++)
94 data_[i]=null;
95
96 count_=_new;
97 }
98
99 public final int capacity()
100 {
101 return data_.length;
102 }
103
104 public final int size()
105 {
106 return count_;
107 }
108
109 public final boolean isEmpty()
110 {
111 return count_==0;
112 }
113
114 public final synchronized Enumeration elements()
115 {
116 return new Enumerator(this);
117 }
118
119 public final synchronized Enumeration reverseElements()
120 {
121 return new ReverseEnumerator(this);
122 }
123
124 public final boolean contains(Object _o)
125 {
126 return (indexOf(_o,0)>=0);
127 }
128
129 public final int indexOf(Object _o)
130 {
131 return indexOf(_o,0);
132 }
133
134 public final synchronized int indexOf(Object _o, int _index)
135 {
136 for(int i=_index; i<count_; i++)
137 if(_o.equals(data_[i]))
138 return i;
139
140 return -1;
141 }
142
143 public final int lastIndexOf(Object _o)
144 {
145 return lastIndexOf(_o,count_-1);
146 }
147
148 public final synchronized int lastIndexOf(Object _o, int _index)
149 {
150 for(int i=_index; i>=0; i--)
151 if(_o.equals(data_[i]))
152 return i;
153
154 return -1;
155 }
156
157 public final synchronized Object elementAt(int _index)
158 {
159 return data_[_index];
160 }
161
162 public final synchronized Object firstElement()
163 {
164 if(count_==0) throw new NoSuchElementException();
165 return data_[0];
166 }
167
168 public final synchronized Object lastElement()
169 {
170 if(count_==0) throw new NoSuchElementException();
171 return data_[count_-1];
172 }
173
174 public final synchronized void setElementAt(Object _o,int _index)
175 {
176 if(_index>=count_) throw new ArrayIndexOutOfBoundsException();
177 data_[_index]=_o;
178 }
179
180 public final synchronized void removeElementAt(int _index)
181 {
182 int j=count_-1-_index;
183 if(j>0)
184 System.arraycopy(data_,_index+1,data_,_index,j);
185
186 count_--;
187 data_[count_]=null;
188 }
189
190 public final synchronized void insertElementAt(Object _o, int _index)
191 {
192 if(_index==count_)
193 addElement(_o);
194 else
195 {
196 if(count_>=data_.length)
197 resize0(count_+1);
198 count_++;
199 System.arraycopy(data_,_index,data_,
200 _index+1,count_-1-_index);
201 data_[_index]=_o;
202 }
203 }
204
205 public final synchronized void addElement(Object _o)
206 {
207 if(count_>=data_.length)
208 resize0(count_+1);
209 data_[count_]=_o;
210 count_++;
211 }
212
213 public final synchronized boolean removeElement(Object _o)
214 {
215 int i=indexOf(_o);
216 if(i>=0)
217 {
218 removeElementAt(i);
219 return true;
220 }
221 return false;
222 }
223
224 public final synchronized void removeAllElements()
225 {
226 for(int i=0; i<count_; i++)
227 data_[i]=null;
228 count_=0;
229 }
230
231 public final synchronized Object clone()
232 {
233 FuVectorPublic r=null;
234
235 try
236 {
237 r=(FuVectorPublic)super.clone();
238 r.data_=new Object[count_];
239 System.arraycopy(data_,0,r.data_,0,count_);
240 }
241 catch(CloneNotSupportedException ex)
242 {
243 throw new InternalError();
244 }
245
246 return r;
247 }
248
249 public final String toString()
250 {
251 return "FuVectorPublic("+count_+")";
252 }
253
254 private static final class Enumerator
255 implements Enumeration
256 {
257 private FuVectorPublic vector;
258 private int count;
259
260 Enumerator(FuVectorPublic _v)
261 {
262 vector=_v;
263 count =0;
264 }
265
266 public final boolean hasMoreElements()
267 {
268 return count<vector.count_;
269 }
270
271 public final Object nextElement()
272 {
273 if(count<vector.count_)
274 return vector.data_[count++];
275 throw new NoSuchElementException("FuVectorPublic.Enumerator");
276 }
277 }
278
279 private static final class ReverseEnumerator
280 implements Enumeration
281 {
282 private FuVectorPublic vector;
283 private int count;
284
285 ReverseEnumerator(FuVectorPublic _v)
286 {
287 vector=_v;
288 count =vector.size()-1;
289 }
290
291 public final boolean hasMoreElements()
292 {
293 return count>=0;
294 }
295
296 public final Object nextElement()
297 {
298 if((count>=0)&&(count<vector.size()))
299 return vector.elementAt(count--);
300 throw new NoSuchElementException("FuVectorPublic.ReverseEnumerator");
301 }
302 }
303 }