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: private static final long serialVersionUID = -4937635522392824190L;
028:
029: public DBagImpl() {
030: super ();
031: }
032:
033: /**
034: * DBagImpl constructor comment.
035: */
036: public DBagImpl(PBKey key) {
037: super (key);
038: }
039:
040: /**
041: * A new <code>DBag</code> instance is created that contains the difference of
042: * this object and the <code>DBag</code> instance referenced by <code>otherBag</code>.
043: * This method is similar to the <code>removeAll</code> method in <code>Collection</code>,
044: * except that this method creates a new collection and <code>removeAll</code>
045: * modifies the object to contain the result.
046: * @param otherBag The other bag to use in creating the difference.
047: * @return A <code>DBag</code> instance that contains the elements of this object
048: * minus the elements in <code>otherBag</code>.
049: */
050: public DBag difference(DBag otherBag) {
051: DBagImpl result = new DBagImpl(getPBKey());
052: Iterator iter = this .iterator();
053: while (iter.hasNext()) {
054: Object candidate = iter.next();
055: if (!otherBag.contains(candidate)) {
056: result.add(candidate);
057: }
058: }
059: return result;
060: }
061:
062: /**
063: * A new <code>DBag</code> instance is created that contains the intersection of
064: * this object and the <code>DBag</code> referenced by <code>otherBag</code>.
065: * This method is similar to the <code>retainAll</code> method in <code>Collection</code>,
066: * except that this method creates a new collection and <code>retainAll</code>
067: * modifies the object to contain the result.
068: * @param otherBag The other bag to use in creating the intersection.
069: * @return A <code>DBag</code> instance that contains the intersection of this
070: * object and <code>otherBag</code>.
071: */
072: public DBag intersection(DBag otherBag) {
073: DBagImpl result = new DBagImpl(getPBKey());
074: Iterator iter = otherBag.iterator();
075: while (iter.hasNext()) {
076: Object candidate = iter.next();
077: if (this .contains(candidate)) {
078: result.add(candidate);
079: }
080: }
081: return result;
082: }
083:
084: /**
085: * This method returns the number of occurrences of the object <code>obj</code>
086: * in the <code>DBag</code> collection.
087: * @param obj The value that may have elements in the collection.
088: * @return The number of occurrences of <code>obj</code> in this collection.
089: */
090: public int occurrences(Object obj) {
091: int count = 0;
092: for (int i = 0; i < this .size(); i++) {
093: if ((obj == null) ? this .get(i) == null : this .get(i)
094: .equals(obj)) {
095: count++;
096: }
097: }
098: return count;
099: }
100:
101: /**
102: * A new <code>DBag</code> instance is created that is the union of this object
103: * and <code>otherBag</code>.
104: * This method is similar to the <code>addAll</code> method in <code>Collection</code>,
105: * except that this method creates a new collection and <code>addAll</code>
106: * modifies the object to contain the result.
107: * @param otherBag The other bag to use in the union operation.
108: * @return A <code>DBag</code> instance that contains the union of this object
109: * and <code>otherBag</code>.
110: */
111: public DBag union(DBag otherBag) {
112: return (DBagImpl) concat((DBagImpl) otherBag);
113: }
114: }
|