KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > collections > AbstractOzoneSet


1 /*
2  * $Id: AbstractOzoneSet.java,v 1.8 2003/12/02 15:31:18 leomekenkamp Exp $
3  * This file is based on AbstractSet.java from GNU Classpath. Quote:
4
5 AbstractSet.java -- Abstract implementation of most of Set
6 Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.
7
8 This file is part of GNU Classpath.
9
10 GNU Classpath is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 GNU Classpath is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GNU Classpath; see the file COPYING. If not, write to the
22 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 02111-1307 USA.
24
25 Linking this library statically or dynamically with other modules is
26 making a combined work based on this library. Thus, the terms and
27 conditions of the GNU General Public License cover the whole
28 combination.
29
30 As a special exception, the copyright holders of this library give you
31 permission to link this library with independent modules to produce an
32 executable, regardless of the license terms of these independent
33 modules, and to copy and distribute the resulting executable under
34 terms of your choice, provided that you also meet, for each linked
35 independent module, the terms and conditions of the license of that
36 module. An independent module is a module which is not derived from
37 or based on this library. If you modify this library, you may extend
38 this exception to your version of the library, but you are not
39 obligated to do so. If you do not wish to do so, delete this
40 exception statement from your version.
41
42  * end quote.
43  *
44  * This file is licenced under the same conditions as its original (GPL +
45  * "special exception").
46  */

47
48 package org.ozoneDB.collections;
49
50 import java.util.Collection JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.Set JavaDoc;
53
54 /**
55  * An abstract implementation of Set to make it easier to create your own
56  * implementations. In order to create a Set, subclass AbstractOzoneSet and
57  * implement the same methods that are required for AbstractOzoneCollection
58  * (although these methods must of course meet the requirements that Set puts
59  * on them - specifically, no element may be in the set more than once). This
60  * class simply provides implementations of equals() and hashCode() to fulfil
61  * the requirements placed on them by the Set interface.
62  *
63  * @author Original author unknown
64  * @author Eric Blake <ebb9@email.byu.edu>
65  * @author <a HREF="mailto:ozoneATmekenkampD0Tcom">Leo Mekenkamp (mind the anti-sp@m)</a> (adaptation for ozone)
66  * @see java.util.AbstractSet
67  */

68 public abstract class AbstractOzoneSet extends AbstractOzoneCollection implements OzoneSet {
69
70     private static final long serialVersionUID = 1L;
71
72     protected AbstractOzoneSet() {
73     }
74
75     /**
76      * Tests whether the given object is equal to this Set. This implementation
77      * first checks whether this set <em>is</em> the given object, and returns
78      * true if so. Otherwise, if o is a Set and is the same size as this one, it
79      * returns the result of calling containsAll on the given Set. Otherwise, it
80      * returns false.
81      *
82      * @param o the Object to be tested for equality with this Set
83      * @return true if the given object is equal to this Set
84      */

85     public boolean equals(Object JavaDoc o) {
86         return super.equals(o) || (o instanceof Set JavaDoc && ((Set JavaDoc) o).size() == size() && containsAll((Collection JavaDoc) o));
87     }
88
89     /**
90      * Returns a hash code for this Set. The hash code of a Set is the sum of the
91      * hash codes of all its elements, except that the hash code of null is
92      * defined to be zero. This implementation obtains an Iterator over the Set,
93      * and sums the results.
94      *
95      * @return a hash code for this Set
96      */

97     public int hashCode() {
98         Iterator JavaDoc itr = _org_ozoneDB_internalIterator();
99         int hash = 0;
100         int pos = size();
101         while (--pos >= 0)
102             hash += hashCode(itr.next());
103         return hash;
104     }
105
106     /**
107      * Removes from this set all elements in the given collection (optional
108      * operation). This implementation uses <code>size()</code> to determine
109      * the smaller collection. Then, if this set is smaller, it iterates
110      * over the set, calling Iterator.remove if the collection contains
111      * the element. If this set is larger, it iterates over the collection,
112      * calling Set.remove for all elements in the collection. Note that
113      * this operation will fail if a remove methods is not supported.
114      *
115      * @param c the collection of elements to remove
116      * @return true if the set was modified as a result
117      * @throws UnsupportedOperationException if remove is not supported
118      * @throws NullPointerException if the collection is null
119      * @see java.util.AbstractCollection#remove(Object)
120      * @see Collection#contains(Object)
121      * @see Iterator#remove()
122      */

123     public boolean removeAll(Collection JavaDoc c) {
124         int oldsize = size();
125         int count = c.size();
126         Iterator JavaDoc i;
127         if (oldsize < count) {
128             for (i = _org_ozoneDB_internalIterator(), count = oldsize; count > 0; count--) {
129                 if (c.contains(i.next())) {
130                     i.remove();
131                 }
132             }
133         } else {
134             Iterator JavaDoc itr;
135             if (c instanceof OzoneCollection) {
136                 itr = ((OzoneCollection) c)._org_ozoneDB_internalIterator();
137             } else {
138                 itr = c.iterator();
139             }
140             for (; count > 0; count--) {
141                 remove(itr.next());
142             }
143         }
144         return oldsize != size();
145     }
146
147     /** <p>Returns a non-ozone <code>Set</code> that contains the same
148      * entries as this persistent one; it is (by nature of the client-server
149      * enviromnent) always a 'deep' copy of this <code>OzoneSet</code>.
150      * I.e. the contents of this <code>OzoneSet</code> instance are
151      * always copied to the client by use of serialization.</p>
152      * <p>This means that if this instance holds non-ozone objects, these
153      * objects are send to the calling client by means of serialization. If
154      * this instance holds ozone objects, it actually holds proxies to these
155      * objects. These proxies are copied and send to the client, resulting in
156      * different proxies to the same ozone objects.</p>
157      *
158      */

159     public Set JavaDoc getClientSet() {
160         throw new UnsupportedOperationException JavaDoc();
161     }
162
163 }
Popular Tags