KickJava   Java API By Example, From Geeks To Geeks.

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


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: DxAbstractCollection.java,v 1.1 2001/12/18 10:31:30 per_nyfelt Exp $
8

9 package org.ozoneDB.DxLib;
10
11 import org.ozoneDB.io.stream.ResolvingObjectInputStream;
12
13 import java.io.*;
14
15
16 /**
17  * Common super class of all collection classes.
18  */

19 public abstract class DxAbstractCollection extends DxObject implements DxCollection, Externalizable {
20
21     final static long serialVersionUID = 2L;
22
23
24     public DxAbstractCollection() {
25     }
26
27     /**
28      * Construct the collection out of an array of objects.
29      */

30     // public DxAbstractCollection (Object[] objs) {
31
// for (int i=0; i<objs.length; i++)
32
// add (objs[i]);
33
// }
34

35
36     public Object JavaDoc clone( DxCollection newInstance ) {
37         try {
38             DxIterator it = iterator();
39             Object JavaDoc obj;
40             while ((obj = it.next()) != null) {
41                 newInstance.add( obj );
42             }
43             return newInstance;
44         } catch (Exception JavaDoc e) {
45             throw new RuntimeException JavaDoc( e.toString() );
46         }
47     }
48
49
50     /**
51      * erzeugt einen clone der collection und der objekte;
52      * Achtung: alle objekte in der collection muessen DxCompatible sein
53      */

54     public DxCollection valueClone() {
55         try {
56             DxCollection coll = (DxCollection)getClass().newInstance();
57             DxIterator it = iterator();
58             while (it.next() != null) {
59                 Object JavaDoc obj = it.object();
60
61                 if (obj instanceof DxCompatible) {
62                     coll.add( ((DxCompatible)obj).clone() );
63                 } else if (obj instanceof Serializable) {
64
65                     ByteArrayOutputStream bout = new ByteArrayOutputStream( 4096 );
66                     ObjectOutputStream out = new ObjectOutputStream( bout );
67                     out.writeObject( obj );
68                     out.close();
69
70                     ObjectInputStream in = new ResolvingObjectInputStream( new ByteArrayInputStream( bout.toByteArray() ) );
71                     coll.add( in.readObject() );
72                 } else {
73
74                     throw new CloneNotSupportedException JavaDoc();
75                 }
76             }
77             return coll;
78         } catch (Exception JavaDoc e) {
79             throw new RuntimeException JavaDoc( e.toString() );
80         }
81     }
82
83
84     public Object JavaDoc[] toArray() {
85         Object JavaDoc[] result = new Object JavaDoc[count()];
86         DxIterator it = iterator();
87         Object JavaDoc obj;
88         for (int i = 0; (obj = it.next()) != null; i++) {
89             result[i] = obj;
90         }
91         return result;
92     }
93
94
95     /**
96      * Compares two collections for equality.
97      * You have to override this method in the implementation of an actual
98      * collections in order to provide another behaviour than the one implemented in
99      * Objects.equals().
100      */

101     public boolean equals( Object JavaDoc obj ) {
102         return super.equals( obj );
103
104     // if (obj instanceof DxCollection && obj != null) {
105
//
106
// if (this == obj)
107
// return true;
108
//
109
// boolean answer = true;
110
// DxIterator it = iterator();
111
// while (it.next() != null) {
112
// if (!((DxCollection)obj).contains (it.object())) {
113
// answer = false;
114
// break;
115
// }
116
// }
117
// if (answer) {
118
// it = ((DxCollection)obj).iterator();
119
// while (it.next() != null) {
120
// if (!contains (it.object())) {
121
// answer = false;
122
// break;
123
// }
124
// }
125
// }
126
// return answer;
127
// }
128
// else
129
// return false;
130
}
131
132
133     public synchronized boolean addAll( DxCollection coll ) {
134         boolean answer = false;
135         DxIterator it = coll.iterator();
136         Object JavaDoc obj;
137         while ((obj = it.next()) != null) {
138             if (add( obj )) {
139                 answer = true;
140             }
141         }
142         return answer;
143     }
144
145
146     public synchronized boolean addAll( Object JavaDoc[] objs ) {
147         boolean answer = false;
148         for (int i = 0; i < objs.length; i++) {
149             if (add( objs[i] )) {
150                 answer = true;
151             }
152         }
153         return answer;
154     }
155
156
157     /**
158      * Remove the first occurence of an object that equals the the
159      * specified object.
160      */

161     public synchronized boolean remove( Object JavaDoc obj ) {
162         boolean answer = false;
163         DxIterator it = iterator();
164         Object JavaDoc cursor;
165         while ((cursor = it.next()) != null) {
166             if (obj.equals( cursor )) {
167                 it.removeObject();
168                 answer = true;
169             }
170         }
171         return answer;
172     }
173
174
175     public synchronized boolean removeAll( DxCollection coll ) {
176         boolean answer = false;
177         if (!coll.isEmpty() && !isEmpty()) {
178             DxIterator it = coll.iterator();
179             Object JavaDoc obj;
180             while ((obj = it.next()) != null) {
181                 if (remove( obj )) {
182                     answer = true;
183                 }
184             }
185         }
186         return answer;
187     }
188
189
190     /**
191      * Returns true is this collection contains an object that equals
192      * to the specified object.
193      */

194     public boolean contains( Object JavaDoc obj ) {
195         DxIterator it = iterator();
196         Object JavaDoc cursor;
197         while ((cursor = it.next()) != null) {
198             if (obj.equals( cursor )) {
199                 return true;
200             }
201         }
202         return false;
203     }
204
205
206     public boolean containsAll( DxCollection coll ) {
207         DxIterator it = coll.iterator();
208         Object JavaDoc cursor;
209         while ((cursor = it.next()) != null) {
210             if (!contains( cursor )) {
211                 return false;
212             }
213         }
214         return true;
215     }
216
217
218     public void writeExternal( ObjectOutput out ) throws IOException {
219         // System.out.println (getClass().getName() + ".writeExternal()...");
220
out.writeInt( count() );
221         DxIterator it = iterator();
222         Object JavaDoc obj;
223         while ((obj = it.next()) != null) {
224             out.writeObject( obj );
225         }
226     }
227
228
229     public synchronized void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException JavaDoc {
230         int count = in.readInt();
231         for (; count > 0; count--) {
232             add( in.readObject() );
233         }
234     }
235
236 }
237
Popular Tags