KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > DxLib > DxArrayBag


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: DxArrayBag.java,v 1.12 2001/02/20 11:23:28 daniela Exp $
8

9 package org.ozoneDB.DxLib;
10
11 import java.util.*;
12 import java.io.*;
13
14 /**
15  *
16  *
17  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
18  * @version $Revision: 1.12 $Date: 2001/02/20 11:23:28 $
19  */

20 public class DxArrayBag extends DxAbstractBag implements DxVector, DxVectorCollection {
21     
22     final static long serialVersionUID = 1L;
23     
24     private transient Vector vector;
25     
26     private transient int itemCount = 0;
27     
28     
29     public DxArrayBag() {
30         this( 32 );
31     }
32     
33     
34     public DxArrayBag( int initSpace ) {
35         vector = new Vector( initSpace );
36         itemCount = 0;
37     }
38     
39
40     public synchronized boolean addFront( Object JavaDoc obj ) {
41         insertAtIndex( obj, 0 );
42         return true;
43     }
44     
45
46     public synchronized boolean addBack( Object JavaDoc obj ) {
47         insertAtIndex( obj, size() );
48         return true;
49     }
50     
51     
52     /**
53      * Sets the component at the specified index of this vector to be the
54      * specified object. The previous component at that position is returned.
55      * The index must be a value greater than or equal to 0. The size of the
56      * array will grow if the specified index is greater than the current size.
57      */

58     public synchronized Object JavaDoc addAtIndex( Object JavaDoc obj, int index ) {
59         if (index >= vector.size()) {
60             vector.setSize( index + 1 );
61         }
62         Object JavaDoc old = vector.elementAt( index );
63         vector.setElementAt( obj, index );
64         if (old == null) {
65             itemCount++;
66         }
67         return old;
68     }
69     
70     
71     public Object JavaDoc elementAtIndex( int index ) {
72         return vector.elementAt( index );
73     }
74     
75     
76     /**
77      * Sets the component at the specified index to be null. The previous
78      * component at that position is returned. The index must be a value
79      * greater than or equal to 0 and less than the current size.
80      */

81     public synchronized Object JavaDoc removeAtIndex( int index ) {
82         Object JavaDoc old = vector.elementAt( index );
83         vector.setElementAt( null, index );
84         if (old != null) {
85             itemCount--;
86         }
87         return old;
88     }
89
90
91     /**
92      * Inserts the specified object as a component at the specified index. Each
93      * component with an index greater or equal to the specified index is
94      * shifted upward to have an index one greater than the value it had
95      * previously. The index must be a value greater than or equal to 0 and less
96      * than or equal to the current size of the vector.
97      */

98     public synchronized void insertAtIndex( Object JavaDoc obj, int index ) {
99         if (index >= vector.size()) {
100             vector.setSize( index );
101         }
102         vector.insertElementAt( obj, index );
103         itemCount++;
104     }
105
106
107     /**
108      * Deletes the component at the specified index. Each component in this
109      * array with an index greater or equal to the specified index is shifted
110      * downward to have an index one smaller than the value it had previously.
111      * The index must be a value greater than or equal to 0 and less than the
112      * current size.
113      */

114     public synchronized Object JavaDoc deleteAtIndex( int index ) {
115         Object JavaDoc old = vector.elementAt( index );
116         if (old != null) {
117             itemCount--;
118         }
119         vector.removeElementAt( index );
120         return old;
121     }
122     
123     
124     public int space() {
125         return vector.capacity();
126     }
127     
128     
129     public DxIterator iterator() {
130         return new DxVectorIterator( this );
131     }
132     
133     
134     public int count() {
135         return itemCount;
136     }
137
138     
139     public int size() {
140         return vector.size();
141     }
142     
143     
144     public boolean isEmpty() {
145         return vector.isEmpty();
146     }
147     
148     
149     public synchronized void clear() {
150         vector.removeAllElements();
151         itemCount = 0;
152     }
153
154     
155     public Vector internalVector() {
156         return vector;
157     }
158
159     public void decCounter() {
160         itemCount--;
161     }
162
163
164 }
165
Popular Tags