001 package org.apache.ojb.odmg.collections;
002
003 /* Copyright 2002-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import org.apache.ojb.broker.PBKey;
019 import org.odmg.DBag;
020
021 import java.util.Iterator;
022
023 /**
024 * The {@link org.odmg.DBag} implementation class.
025 */
026 public class DBagImpl extends DListImpl implements org.odmg.DBag
027 {
028 private static final long serialVersionUID = -4937635522392824190L;
029
030 public DBagImpl()
031 {
032 super();
033 }
034
035 /**
036 * DBagImpl constructor comment.
037 */
038 public DBagImpl(PBKey key)
039 {
040 super(key);
041 }
042
043 /**
044 * A new <code>DBag</code> instance is created that contains the difference of
045 * this object and the <code>DBag</code> instance referenced by <code>otherBag</code>.
046 * This method is similar to the <code>removeAll</code> method in <code>Collection</code>,
047 * except that this method creates a new collection and <code>removeAll</code>
048 * modifies the object to contain the result.
049 * @param otherBag The other bag to use in creating the difference.
050 * @return A <code>DBag</code> instance that contains the elements of this object
051 * minus the elements in <code>otherBag</code>.
052 */
053 public DBag difference(DBag otherBag)
054 {
055 DBagImpl result = new DBagImpl(getPBKey());
056 Iterator iter = this.iterator();
057 while (iter.hasNext())
058 {
059 Object candidate = iter.next();
060 if (!otherBag.contains(candidate))
061 {
062 result.add(candidate);
063 }
064 }
065 return result;
066 }
067
068 /**
069 * A new <code>DBag</code> instance is created that contains the intersection of
070 * this object and the <code>DBag</code> referenced by <code>otherBag</code>.
071 * This method is similar to the <code>retainAll</code> method in <code>Collection</code>,
072 * except that this method creates a new collection and <code>retainAll</code>
073 * modifies the object to contain the result.
074 * @param otherBag The other bag to use in creating the intersection.
075 * @return A <code>DBag</code> instance that contains the intersection of this
076 * object and <code>otherBag</code>.
077 */
078 public DBag intersection(DBag otherBag)
079 {
080 DBagImpl result = new DBagImpl(getPBKey());
081 Iterator iter = otherBag.iterator();
082 while (iter.hasNext())
083 {
084 Object candidate = iter.next();
085 if (this.contains(candidate))
086 {
087 result.add(candidate);
088 }
089 }
090 return result;
091 }
092
093 /**
094 * This method returns the number of occurrences of the object <code>obj</code>
095 * in the <code>DBag</code> collection.
096 * @param obj The value that may have elements in the collection.
097 * @return The number of occurrences of <code>obj</code> in this collection.
098 */
099 public int occurrences(Object 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 }