001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.blackboard;
028:
029: import java.util.ArrayList;
030: import java.util.Collection;
031: import java.util.Collections;
032: import java.util.Enumeration;
033: import java.util.HashSet;
034: import java.util.List;
035: import java.util.Set;
036:
037: import org.cougaar.util.Empty;
038: import org.cougaar.util.Enumerator;
039: import org.cougaar.util.UnaryPredicate;
040:
041: /**
042: * A {@link CollectionSubscription} that records add/change/remove
043: * deltas.
044: */
045: public class IncrementalSubscription extends CollectionSubscription {
046:
047: public IncrementalSubscription(UnaryPredicate p, Collection c) {
048: super (p, c);
049: }
050:
051: private Set myAddedSet = null;
052: private List myRemovedList = null;
053:
054: protected void resetChanges() {
055: super .resetChanges();
056: if (myAddedSet != null)
057: myAddedSet.clear();
058: if (myRemovedList != null)
059: myRemovedList.clear();
060: }
061:
062: /**
063: * @return an enumeration of the objects that have been added
064: * since the last transaction.
065: */
066: public Enumeration getAddedList() {
067: checkTransactionOK("getAddedList()");
068: if (myAddedSet == null || myAddedSet.isEmpty())
069: return Empty.enumeration;
070: return new Enumerator(myAddedSet);
071: }
072:
073: /**
074: * @return a possibly empty collection of objects that have been
075: * added since the last transaction. Will not return null.
076: */
077: public Collection getAddedCollection() {
078: return (myAddedSet != null) ? myAddedSet
079: : Collections.EMPTY_SET;
080: }
081:
082: /**
083: * @return a enumeration of the objects that have been removed
084: * since the last transaction.
085: */
086: public Enumeration getRemovedList() {
087: checkTransactionOK("getRemovedList()");
088: if (myRemovedList == null || myRemovedList.isEmpty())
089: return Empty.enumeration;
090: return new Enumerator(myRemovedList);
091: }
092:
093: /**
094: * @return a possibly empty collection of objects that have been
095: * removed since the last transaction. Will not return null.
096: */
097: public Collection getRemovedCollection() {
098: return (myRemovedList != null) ? myRemovedList
099: : Collections.EMPTY_LIST;
100: }
101:
102: /**
103: * @return an enumeration of the objects that have been changed
104: * since the last transaction.
105: * @see #getChangeReports(Object)
106: */
107: public Enumeration getChangedList() {
108: checkTransactionOK("getChangedList()");
109: return super .privateGetChangedList();
110: }
111:
112: /**
113: * @return a possibly empty collection of objects that have been
114: * changed since the last transaction. Will not return null.
115: * @see #getChangeReports(Object)
116: */
117: public Collection getChangedCollection() {
118: return super .privateGetChangedCollection();
119: }
120:
121: /** override this for sorted sets */
122: protected Set createAddedSet() {
123: return new HashSet(5);
124: }
125:
126: /** called by privateAdd */
127: private void addToAddedList(Object o) {
128: if (myAddedSet == null)
129: myAddedSet = createAddedSet();
130: myAddedSet.add(o);
131: }
132:
133: /** called by privateRemove */
134: private void addToRemovedList(Object o) {
135: if (myRemovedList == null)
136: myRemovedList = new ArrayList(3);
137: myRemovedList.add(o);
138: }
139:
140: protected void privateAdd(Object o, boolean isVisible) {
141: super .privateAdd(o, isVisible);
142: if (isVisible) {
143: setChanged(true);
144: addToAddedList(o);
145: }
146: }
147:
148: protected void privateRemove(Object o, boolean isVisible) {
149: super .privateRemove(o, isVisible);
150: if (isVisible) {
151: setChanged(true);
152: addToRemovedList(o);
153: }
154: }
155:
156: protected void privateChange(Object o, List changes,
157: boolean isVisible) {
158: if (isVisible) {
159: setChanged(true);
160: super .privateChange(o, changes, true);
161: }
162: }
163: }
|