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: DxAbstractCollection.java,v 1.1 2001/12/18 10:31:30 per_nyfelt Exp $
008:
009: package org.ozoneDB.DxLib;
010:
011: import java.io.*;
012:
013: /**
014: * Common super class of all collection classes.
015: */
016: public abstract class DxAbstractCollection extends DxObject implements
017: DxCollection, Externalizable {
018:
019: final static long serialVersionUID = 2L;
020:
021: public DxAbstractCollection() {
022: }
023:
024: /**
025: * Construct the collection out of an array of objects.
026: */
027: // public DxAbstractCollection (Object[] objs) {
028: // for (int i=0; i<objs.length; i++)
029: // add (objs[i]);
030: // }
031:
032: public Object clone(DxCollection newInstance) {
033: try {
034: DxIterator it = iterator();
035: Object obj;
036: while ((obj = it.next()) != null) {
037: newInstance.add(obj);
038: }
039: return newInstance;
040: } catch (Exception e) {
041: throw new RuntimeException(e.toString());
042: }
043: }
044:
045: /**
046: * erzeugt einen clone der collection und der objekte;
047: * Achtung: alle objekte in der collection muessen DxCompatible sein
048: */
049: public DxCollection valueClone() {
050: try {
051: DxCollection coll = (DxCollection) getClass().newInstance();
052: DxIterator it = iterator();
053: while (it.next() != null) {
054: Object obj = it.object();
055:
056: if (obj instanceof DxCompatible) {
057: coll.add(((DxCompatible) obj).clone());
058: } else if (obj instanceof Serializable) {
059:
060: ByteArrayOutputStream bout = new ByteArrayOutputStream(
061: 4096);
062: ObjectOutputStream out = new ObjectOutputStream(
063: bout);
064: out.writeObject(obj);
065: out.close();
066:
067: ObjectInputStream in = new ObjectInputStream(
068: new ByteArrayInputStream(bout.toByteArray()));
069: coll.add(in.readObject());
070: } else {
071:
072: throw new CloneNotSupportedException();
073: }
074: }
075: return coll;
076: } catch (Exception e) {
077: throw new RuntimeException(e.toString());
078: }
079: }
080:
081: public Object[] toArray() {
082: Object[] result = new Object[count()];
083: DxIterator it = iterator();
084: Object obj;
085: for (int i = 0; (obj = it.next()) != null; i++) {
086: result[i] = obj;
087: }
088: return result;
089: }
090:
091: /**
092: * Compares two collections for equality.
093: * You have to override this method in the implementation of an actual
094: * collections in order to provide another behaviour than the one implemented in
095: * Objects.equals().
096: */
097: public boolean equals(Object obj) {
098: return super .equals(obj);
099:
100: // if (obj instanceof DxCollection && obj != null) {
101: //
102: // if (this == obj)
103: // return true;
104: //
105: // boolean answer = true;
106: // DxIterator it = iterator();
107: // while (it.next() != null) {
108: // if (!((DxCollection)obj).contains (it.object())) {
109: // answer = false;
110: // break;
111: // }
112: // }
113: // if (answer) {
114: // it = ((DxCollection)obj).iterator();
115: // while (it.next() != null) {
116: // if (!contains (it.object())) {
117: // answer = false;
118: // break;
119: // }
120: // }
121: // }
122: // return answer;
123: // }
124: // else
125: // return false;
126: }
127:
128: public synchronized boolean addAll(DxCollection coll) {
129: boolean answer = false;
130: DxIterator it = coll.iterator();
131: Object obj;
132: while ((obj = it.next()) != null) {
133: if (add(obj)) {
134: answer = true;
135: }
136: }
137: return answer;
138: }
139:
140: public synchronized boolean addAll(Object[] objs) {
141: boolean answer = false;
142: for (int i = 0; i < objs.length; i++) {
143: if (add(objs[i])) {
144: answer = true;
145: }
146: }
147: return answer;
148: }
149:
150: /**
151: * Remove the first occurence of an object that equals the the
152: * specified object.
153: */
154: public synchronized boolean remove(Object obj) {
155: boolean answer = false;
156: DxIterator it = iterator();
157: Object cursor;
158: while ((cursor = it.next()) != null) {
159: if (obj.equals(cursor)) {
160: it.removeObject();
161: answer = true;
162: }
163: }
164: return answer;
165: }
166:
167: public synchronized boolean removeAll(DxCollection coll) {
168: boolean answer = false;
169: if (!coll.isEmpty() && !isEmpty()) {
170: DxIterator it = coll.iterator();
171: Object obj;
172: while ((obj = it.next()) != null) {
173: if (remove(obj)) {
174: answer = true;
175: }
176: }
177: }
178: return answer;
179: }
180:
181: /**
182: * Returns true is this collection contains an object that equals
183: * to the specified object.
184: */
185: public boolean contains(Object obj) {
186: DxIterator it = iterator();
187: Object cursor;
188: while ((cursor = it.next()) != null) {
189: if (obj.equals(cursor)) {
190: return true;
191: }
192: }
193: return false;
194: }
195:
196: public boolean containsAll(DxCollection coll) {
197: DxIterator it = coll.iterator();
198: Object cursor;
199: while ((cursor = it.next()) != null) {
200: if (!contains(cursor)) {
201: return false;
202: }
203: }
204: return true;
205: }
206:
207: public void writeExternal(ObjectOutput out) throws IOException {
208: // System.out.println (getClass().getName() + ".writeExternal()...");
209: out.writeInt(count());
210: DxIterator it = iterator();
211: Object obj;
212: while ((obj = it.next()) != null) {
213: out.writeObject(obj);
214: }
215: }
216:
217: public synchronized void readExternal(ObjectInput in)
218: throws IOException, ClassNotFoundException {
219: int count = in.readInt();
220: for (; count > 0; count--) {
221: add(in.readObject());
222: }
223: }
224:
225: }
|