KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pj > util > PjObjectVector


1 package com.etymon.pj.util;
2
3 import java.util.*;
4 import com.etymon.pj.object.*;
5
6 public class PjObjectVector {
7
8     public PjObjectVector() {
9         _v = new Vector();
10         _free = 1;
11     }
12
13     public PjObjectVector(int initialCapacity) {
14         _v = new Vector(initialCapacity);
15         _free = 1;
16     }
17
18     public int getFirstFree() {
19         synchronized (this) {
20             return _free;
21         }
22     }
23     
24     public PjObject objectAt(int index) {
25         synchronized (this) {
26             if (index >= _v.size()) {
27                 return null;
28             } else {
29                 return (PjObject)(_v.elementAt(index));
30             }
31         }
32     }
33     
34     public void setObjectAt(PjObject obj, int index) {
35         synchronized (this) {
36             if (index >= _v.size()) {
37                 _v.setSize(index + 1);
38             }
39             _v.setElementAt(obj, index);
40             if (index == _free) {
41                 _free = findFirstFree(index + 1);
42             }
43         }
44     }
45     
46     public int size() {
47         synchronized (this) {
48             return _v.size();
49         }
50     }
51
52     
53     protected int findFirstFree(int start) {
54         synchronized (this) {
55             int x = start;
56             int size = _v.size();
57             while ( (x < size) && (_v.elementAt(x) != null) ) {
58                 x++;
59             }
60             return x;
61         }
62     }
63
64     protected Vector _v;
65     protected int _free;
66     
67 }
68
Popular Tags