aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/util/Vector.java
blob: b49f482e5607085e1e32ad4a4aee0b1c0572db80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* Copyright (C) 1998, 1999, 2000  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.util;

import java.io.Serializable;

/**
 * @author Warren Levy <warrenl@cygnus.com>
 * @date August 17, 1998.
 */
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
 * "The Java Language Specification", ISBN 0-201-63451-1
 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
 * Status:  Believed complete and correct
 */

final class VectorEnumeration implements Enumeration
{
  private int enumIndex;
  private Vector enumVec;

  public VectorEnumeration(Vector vec)
  {
    enumVec = vec;
    enumIndex = 0;
  }

  public boolean hasMoreElements()
  {
    return enumIndex < enumVec.size();
  }

  public Object nextElement()
  {
    if (! (enumIndex < enumVec.size()))
      throw new NoSuchElementException();

    return enumVec.elementData[enumIndex++];
  }
}

// TODO12:
// public class Vector extends AbstractList
//	implements List, Cloneable, Serializable

public class Vector implements Cloneable, Serializable
{
  /* The size of the increment to use when growing this vector.
     The default of 0 means to double the capacity when growing. */
  protected int capacityIncrement;

  /* The number of elements currently in elementData */
  protected int elementCount;

  /* The buffer in which elements of this vector are stored */
  protected Object[] elementData;

  public Vector()
  {
    this(10, 0);
  }

  public Vector(int initCap)
  {
    this(initCap, 0);
  }

  public Vector(int initCap, int capIncrement)
  {
    if (initCap < 0)
      throw new IllegalArgumentException ();
    elementData = new Object[initCap];
    capacityIncrement = capIncrement;
    elementCount = 0;
  }

  public final synchronized void addElement(Object obj)
  {
    // Make sure there's room for a new element
    if (elementCount == elementData.length)
      ensureCapacity(elementCount+1);

    elementData[elementCount++] = obj;
  }

  public final int capacity()
  {
    return elementData.length;
  }

  public synchronized Object clone()
  {
    // New vector needs to have same size, capacity and capacityIncrement
    Vector newVec = new Vector(elementData.length, capacityIncrement);

    System.arraycopy(elementData, 0, newVec.elementData, 0, elementCount);
    newVec.elementCount = elementCount;
    return newVec;
  }

  public final boolean contains(Object obj)
  {
    for (int i = 0; i < elementCount; i++)
      {
	if (obj == null
	    ? elementData[i] == null
	    : obj.equals(elementData[i]))
	  return true;
      }

    return false;
  }

  public final synchronized void copyInto(Object[] objArray)
  {
    System.arraycopy(elementData, 0, objArray, 0, elementCount);
  }

  public final synchronized Object elementAt(int idx)
  {
    if (idx < 0 || idx >= size())
      throw new ArrayIndexOutOfBoundsException();

    return elementData[idx];
  }

  public final synchronized Enumeration elements()
  {
    return new VectorEnumeration(this);
  }

  public final synchronized void ensureCapacity(int minCap)
  {
    // Increasing the vector could make it much larger than minCap;
    // e.g. if minCap is just larger than the vector, size may double.
    // If someone cares about this possibility they should set capacityIncrement
    if (minCap > elementData.length)
      {
	// Increase the vector; double it if capacityIncrement is zero
	int newSize = elementData.length;
	newSize +=
	  (capacityIncrement > 0) ? capacityIncrement : elementData.length;

	// Make sure newSize is at least minCap
	if (newSize < minCap)
	  newSize = minCap;

	Object[] newArray = new Object[newSize];
	System.arraycopy(elementData, 0, newArray, 0, elementCount);
	elementData = newArray;
      }
  }

  public final synchronized Object firstElement()
  {
    if (elementCount == 0)
      throw new NoSuchElementException();

    return elementData[0];
  }

  public final int indexOf(Object obj)
  {
    return indexOf(obj, 0);
  }

  public final synchronized int indexOf(Object obj, int idx)
  {
    if (idx < 0)
      throw new IllegalArgumentException ();
    for (int i = idx; i < elementCount; i++)
      {
	if (obj == null
	    ? elementData[i] == null
	    : obj.equals(elementData[i]))
	  return i;
      }

    return -1;
  }

  public final synchronized void insertElementAt(Object obj, int idx)
  {
    if (idx < 0 || idx > size())
      throw new ArrayIndexOutOfBoundsException();
    else if (idx == size())		// Spec says just use addElement()
      addElement(obj);
    else
      {
	// Make sure there's room for a new element
	if (elementCount == elementData.length)
	  ensureCapacity(elementCount+1);

	// Shift the existing elements up and increment elementCount
	for (int i = elementCount++; i > idx; --i)
	  elementData[i] = elementData[i-1];

	elementData[idx] = obj;
      }
  }

  public final boolean isEmpty()
  {
    return elementCount == 0;
  }

  public final synchronized Object lastElement()
  {
    if (elementCount == 0)
      throw new NoSuchElementException();

    return elementData[elementCount - 1];
  }

  public final int lastIndexOf(Object obj)
  {
    return lastIndexOf(obj, size()-1);
  }

  public final synchronized int lastIndexOf(Object obj, int idx)
  {
    if (idx < 0)
      throw new IllegalArgumentException ();
    for (int i = idx; i >= 0; --i)
      {
	if (obj == null
	    ? elementData[i] == null
	    : obj.equals(elementData[i]))
	  return i;
      }

    return -1;
  }

  public final synchronized void removeAllElements()
  {
    // Remove elements now to assist the gc in early cleanup
    for (int i = elementCount-1; i >= 0; --i)
	elementData[i] = null;
    elementCount = 0;
  }

  public final synchronized boolean removeElement(Object obj)
  {
    for (int i = 0; i < elementCount; i++)
      {
	if (obj == null
	    ? elementData[i] == null
	    : obj.equals(elementData[i]))
	  {
	    int j;

	    // Decrement count first to ensure we don't walk off end of array
	    --elementCount;

	    for (j = i; j < elementCount; j++)
	      elementData[j] = elementData[j+1];

	    // At this point, j was incrememented and points to old last element
	    // Remove element now to assist the gc in early cleanup
	    elementData[j] = null;
	    return true;
	  }
      }

    return false;
  }

  public final synchronized void removeElementAt(int idx)
  {
    int i;

    if (idx < 0 || idx >= size())
      throw new ArrayIndexOutOfBoundsException();

    // Decrement count first to ensure we don't walk off the end of the array
    --elementCount;

    for (i = idx; i < elementCount; i++)
      elementData[i] = elementData[i+1];

    // At this point, i was incrememented and now points to the old last element
    // Remove element now to assist the gc in early cleanup
    elementData[i] = null;
  }

  public final synchronized void setElementAt(Object obj, int idx)
  {
    if (idx < 0 || idx >= size())
      throw new ArrayIndexOutOfBoundsException();

    elementData[idx] = obj;
  }

  public final synchronized void setSize(int newSize)
  {
    if (newSize < 0)
      throw new ArrayIndexOutOfBoundsException();

    // Java Lang Spec p. 658 says to remove the excess elements and discard
    // when new size is smaller than old size.
    // When truncating, we could alternatively just reset elementCount instead
    // of freeing up the memory if the spec hadn't specified.  The spec makes
    // sense though; if someone cares enough to call a setSize() function
    // they probably are doing so to free memory.
    if (newSize < elementCount)
      {
	elementCount = newSize;
	trimToSize();
      }
    else if (newSize > elementCount)	// Skip == case
      {
	// TBD: ensureCapacity() may create a vector much larger than newSize;
	// do we want to make the vector exactly newSize?  Spec is unclear.
	ensureCapacity(newSize);

	// Make sure to null out new elements of grown vector
	for (int i = elementCount; i < newSize; i++)
	  elementData[i] = null;
	elementCount = newSize;
      }
  }

  public final int size()
  {
    return elementCount;
  }

  public final synchronized String toString()
  {
    // Following the Java Lang Spec p. 656

    // Prepend first element with open bracket
    StringBuffer result = new StringBuffer("[");

    if (elementCount > 0)	// add first element if one exists
      result.append(elementData[0].toString());

    // Prepend subsequent elements with ", "
    for (int i = 1; i < elementCount; i++)
      result.append(", ").append(elementData[i].toString());

    // Append last element with closing bracket
    result.append("]");
    return result.toString();
  }

  public final synchronized void trimToSize()
  {
    // Give up excess storage capacity to save memory
    //
    // Don't bother checking for the case where size() == the capacity of the
    // vector since that is a much less likely case; it's more efficient to
    // not do the check and lose a bit of performance in that infrequent case
    Object[] newArray = new Object[elementCount];
    System.arraycopy(elementData, 0, newArray, 0, elementCount);
    elementData = newArray;
  }

  // TODO12:
  // public Vector(Collection c)
  // {
  // }

  // TODO12:
  // public public boolean add(Object o)
  // {
  // }

  // TODO12:
  // public void add(int index, Object element)
  // {
  // }

  // TODO12:
  // public boolean addAll(Collection c)
  // {
  // }

  // TODO12:
  // public boolean addAll(int index, Collection c)
  // {
  // }

  // TODO12:
  // public void clear()
  // {
  // }

  // TODO12:
  // public boolean containsAll(Collection c)
  // {
  // }

  // TODO12:
  // public boolean equals(Object o)
  // {
  // }

  // TODO12:
  // public int hashCode()
  // {
  // }

  // TODO12:
  // public Object get(int index)
  // {
  // }

  // TODO12:
  // public boolean remove(Object o)
  // {
  // }

  // TODO12:
  // public Object remove(int index)
  // {
  // }

  // TODO12:
  // public boolean removeAll(Collection c)
  // {
  // }

  // TODO12:
  // public boolean retainAll(Collection c)
  // {
  // }

  // TODO12:
  // public Object set(int index, Object element)
  // {
  // }

  // TODO12:
  // public Object[] toArray()
  // {
  // }

  // TODO12:
  // public Object[] toArray(Object[] a)
  // {
  // }
}