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.Enumeration;
032: import java.util.List;
033:
034: import org.cougaar.util.Empty;
035: import org.cougaar.util.Enumerator;
036:
037: /**
038: * A {@link Subscription} that collects {@link EnvelopeMetrics}.
039: *
040: * @see Subscriber the "timestamp" system property must be enabled
041: */
042: public class EnvelopeMetricsSubscription extends Subscription {
043:
044: private final boolean includeBlackboard;
045: private final List myList = new ArrayList(5);
046:
047: public EnvelopeMetricsSubscription() {
048: this (true);
049: }
050:
051: public EnvelopeMetricsSubscription(boolean includeBlackboard) {
052: super (null);
053: this .includeBlackboard = includeBlackboard;
054: }
055:
056: protected void resetChanges() {
057: super .resetChanges();
058: myList.clear();
059: }
060:
061: /**
062: * @return an enumeration of EnvelopeMetrics that have been added
063: * since the last transaction.
064: */
065: public Enumeration getAddedList() {
066: checkTransactionOK("getAddedList()");
067: if (myList.isEmpty())
068: return Empty.enumeration;
069: return new Enumerator(myList);
070: }
071:
072: /**
073: * @return a possibly empty collection of EnvelopeMetrics that have
074: * been added since the last transaction. Will not return null.
075: */
076: public Collection getAddedCollection() {
077: return myList;
078: }
079:
080: public boolean apply(Envelope e) {
081: if (!(e instanceof TimestampedEnvelope)) {
082: return false;
083: }
084: TimestampedEnvelope te = (TimestampedEnvelope) e;
085: if ((!includeBlackboard) && te.isBlackboard()) {
086: return false;
087: }
088: EnvelopeMetrics em = new EnvelopeMetrics(te);
089: myList.add(em);
090: setChanged(true);
091: return true;
092: }
093:
094: // never called, due to "apply(..)" override:
095: protected void privateAdd(Object o, boolean isVisible) {
096: }
097:
098: protected void privateRemove(Object o, boolean isVisible) {
099: }
100:
101: protected void privateChange(Object o, List changes,
102: boolean isVisible) {
103: }
104:
105: }
|