001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: DxArrayBag.java,v 1.1 2001/12/18 10:31:30 per_nyfelt Exp $
008:
009: package org.ozoneDB.DxLib;
010:
011: import java.util.*;
012: import java.io.*;
013:
014: /**
015: *
016: *
017: * @author <a href="http://www.softwarebuero.de/">SMB</a>
018: * @version $Revision: 1.1 $Date: 2001/12/18 10:31:30 $
019: */
020: public class DxArrayBag extends DxAbstractBag implements DxVector,
021: DxVectorCollection {
022:
023: final static long serialVersionUID = 1L;
024:
025: private transient Vector vector;
026:
027: private transient int itemCount = 0;
028:
029: public DxArrayBag() {
030: this (32);
031: }
032:
033: public DxArrayBag(int initSpace) {
034: vector = new Vector(initSpace);
035: itemCount = 0;
036: }
037:
038: public synchronized boolean addFront(Object obj) {
039: insertAtIndex(obj, 0);
040: return true;
041: }
042:
043: public synchronized boolean addBack(Object obj) {
044: insertAtIndex(obj, size());
045: return true;
046: }
047:
048: /**
049: * Sets the component at the specified index of this vector to be the
050: * specified object. The previous component at that position is returned.
051: * The index must be a value greater than or equal to 0. The size of the
052: * array will grow if the specified index is greater than the current size.
053: */
054: public synchronized Object addAtIndex(Object obj, int index) {
055: if (index >= vector.size()) {
056: vector.setSize(index + 1);
057: }
058: Object old = vector.elementAt(index);
059: vector.setElementAt(obj, index);
060: if (old == null) {
061: itemCount++;
062: }
063: return old;
064: }
065:
066: public Object elementAtIndex(int index) {
067: return vector.elementAt(index);
068: }
069:
070: /**
071: * Sets the component at the specified index to be null. The previous
072: * component at that position is returned. The index must be a value
073: * greater than or equal to 0 and less than the current size.
074: */
075: public synchronized Object removeAtIndex(int index) {
076: Object old = vector.elementAt(index);
077: vector.setElementAt(null, index);
078: if (old != null) {
079: itemCount--;
080: }
081: return old;
082: }
083:
084: /**
085: * Inserts the specified object as a component at the specified index. Each
086: * component with an index greater or equal to the specified index is
087: * shifted upward to have an index one greater than the value it had
088: * previously. The index must be a value greater than or equal to 0 and less
089: * than or equal to the current size of the vector.
090: */
091: public synchronized void insertAtIndex(Object obj, int index) {
092: if (index >= vector.size()) {
093: vector.setSize(index);
094: }
095: vector.insertElementAt(obj, index);
096: itemCount++;
097: }
098:
099: /**
100: * Deletes the component at the specified index. Each component in this
101: * array with an index greater or equal to the specified index is shifted
102: * downward to have an index one smaller than the value it had previously.
103: * The index must be a value greater than or equal to 0 and less than the
104: * current size.
105: */
106: public synchronized Object deleteAtIndex(int index) {
107: Object old = vector.elementAt(index);
108: if (old != null) {
109: itemCount--;
110: }
111: vector.removeElementAt(index);
112: return old;
113: }
114:
115: public int space() {
116: return vector.capacity();
117: }
118:
119: public DxIterator iterator() {
120: return new DxVectorIterator(this );
121: }
122:
123: public int count() {
124: return itemCount;
125: }
126:
127: public int size() {
128: return vector.size();
129: }
130:
131: public boolean isEmpty() {
132: return vector.isEmpty();
133: }
134:
135: public synchronized void clear() {
136: vector.removeAllElements();
137: itemCount = 0;
138: }
139:
140: public Vector internalVector() {
141: return vector;
142: }
143:
144: public void decCounter() {
145: itemCount--;
146: }
147:
148: }
|