KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > collections > DBagImpl


1 package org.apache.ojb.odmg.collections;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.ojb.broker.PBKey;
19 import org.odmg.DBag;
20
21 import java.util.Iterator JavaDoc;
22
23 /**
24  * The {@link org.odmg.DBag} implementation class.
25  */

26 public class DBagImpl extends DListImpl implements org.odmg.DBag
27 {
28     private static final long serialVersionUID = -4937635522392824190L;
29     
30     public DBagImpl()
31     {
32         super();
33     }
34
35     /**
36      * DBagImpl constructor comment.
37      */

38     public DBagImpl(PBKey key)
39     {
40         super(key);
41     }
42
43     /**
44      * A new <code>DBag</code> instance is created that contains the difference of
45      * this object and the <code>DBag</code> instance referenced by <code>otherBag</code>.
46      * This method is similar to the <code>removeAll</code> method in <code>Collection</code>,
47      * except that this method creates a new collection and <code>removeAll</code>
48      * modifies the object to contain the result.
49      * @param otherBag The other bag to use in creating the difference.
50      * @return A <code>DBag</code> instance that contains the elements of this object
51      * minus the elements in <code>otherBag</code>.
52      */

53     public DBag difference(DBag otherBag)
54     {
55         DBagImpl result = new DBagImpl(getPBKey());
56         Iterator JavaDoc iter = this.iterator();
57         while (iter.hasNext())
58         {
59             Object JavaDoc candidate = iter.next();
60             if (!otherBag.contains(candidate))
61             {
62                 result.add(candidate);
63             }
64         }
65         return result;
66     }
67
68     /**
69      * A new <code>DBag</code> instance is created that contains the intersection of
70      * this object and the <code>DBag</code> referenced by <code>otherBag</code>.
71      * This method is similar to the <code>retainAll</code> method in <code>Collection</code>,
72      * except that this method creates a new collection and <code>retainAll</code>
73      * modifies the object to contain the result.
74      * @param otherBag The other bag to use in creating the intersection.
75      * @return A <code>DBag</code> instance that contains the intersection of this
76      * object and <code>otherBag</code>.
77      */

78     public DBag intersection(DBag otherBag)
79     {
80         DBagImpl result = new DBagImpl(getPBKey());
81         Iterator JavaDoc iter = otherBag.iterator();
82         while (iter.hasNext())
83         {
84             Object JavaDoc candidate = iter.next();
85             if (this.contains(candidate))
86             {
87                 result.add(candidate);
88             }
89         }
90         return result;
91     }
92
93     /**
94      * This method returns the number of occurrences of the object <code>obj</code>
95      * in the <code>DBag</code> collection.
96      * @param obj The value that may have elements in the collection.
97      * @return The number of occurrences of <code>obj</code> in this collection.
98      */

99     public int occurrences(Object JavaDoc obj)
100     {
101         int count = 0;
102         for (int i = 0; i < this.size(); i++)
103         {
104             if ((obj == null) ? this.get(i) == null : this.get(i).equals(obj))
105             {
106                 count++;
107             }
108         }
109         return count;
110     }
111
112     /**
113      * A new <code>DBag</code> instance is created that is the union of this object
114      * and <code>otherBag</code>.
115      * This method is similar to the <code>addAll</code> method in <code>Collection</code>,
116      * except that this method creates a new collection and <code>addAll</code>
117      * modifies the object to contain the result.
118      * @param otherBag The other bag to use in the union operation.
119      * @return A <code>DBag</code> instance that contains the union of this object
120      * and <code>otherBag</code>.
121      */

122     public DBag union(DBag otherBag)
123     {
124         return (DBagImpl) concat((DBagImpl) otherBag);
125     }
126 }
127
Popular Tags