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.logistics.plugin.strattrans;
028:
029: import org.cougaar.util.UnaryPredicate;
030: import org.cougaar.util.DynamicUnaryPredicate;
031:
032: import org.cougaar.planning.ldm.asset.AggregateAsset;
033: import org.cougaar.planning.ldm.asset.Asset;
034:
035: import org.cougaar.lib.callback.UTILFilterCallbackAdapter;
036: import org.cougaar.lib.callback.UTILFilterCallbackListener;
037:
038: import java.util.Enumeration;
039: import org.cougaar.util.log.Logger;
040:
041: /**
042: * Used to filter for new or changed generic aggregate assets or normal assets.
043: * Parameterized so that a single plugin may instantiate multiple instances
044: * of this class to handle multiple types of assets based upon the given
045: * String key passed in the constructor. The "interestingAsset" method
046: * has an additional parameter, and the plugin must define this method to
047: * handle each asset type accordingly based upon the key value.
048: *
049: * Also may return a DynamicUnaryPredicate if so indicated by a boolean parameter
050: * passed into the constructor
051: *
052: */
053:
054: public class UTILParameterizedAggregateAssetCallback extends
055: UTILFilterCallbackAdapter {
056: protected String myKey;
057: protected boolean isDynamic;
058:
059: public UTILParameterizedAggregateAssetCallback(
060: UTILFilterCallbackListener listener, Logger logger,
061: String key, boolean dynamic) {
062: super (listener, logger);
063:
064: myKey = key;
065: isDynamic = dynamic;
066: mySub = myListener.subscribeFromCallback(getPredicate(),
067: getCollection());
068: }
069:
070: protected UnaryPredicate getPredicate() {
071: if (isDynamic) {
072: return new DynamicUnaryPredicate() {
073: public boolean execute(Object o) {
074: if (myKey == null)
075: return false; // needed to handle case where constructor super() calls this first before the key is set.
076:
077: while (o instanceof AggregateAsset)
078: o = ((AggregateAsset) o).getAsset(); // handle case with nested aggregate assets
079: return (o instanceof Asset && ((UTILParameterizedAssetListener) myListener)
080: .interestingParameterizedAsset((Asset) o,
081: myKey));
082: }
083: };
084: } else {
085: return new UnaryPredicate() {
086: public boolean execute(Object o) {
087: if (myKey == null)
088: return false; // needed to handle case where constructor super() calls this first before the key is set.
089:
090: while (o instanceof AggregateAsset)
091: o = ((AggregateAsset) o).getAsset(); // handle case with nested aggregate assets
092: return (o instanceof Asset && ((UTILParameterizedAssetListener) myListener)
093: .interestingParameterizedAsset((Asset) o,
094: myKey));
095: }
096: };
097: }
098: }
099:
100: public void reactToChangedFilter() {
101: Enumeration changedList = mySub.getChangedList();
102: Enumeration addedList = mySub.getAddedList();
103:
104: if (changedList.hasMoreElements())
105: ((UTILParameterizedAssetListener) myListener)
106: .handleChangedParameterizedAssets(changedList,
107: myKey);
108:
109: if (addedList.hasMoreElements())
110: ((UTILParameterizedAssetListener) myListener)
111: .handleNewParameterizedAssets(addedList, myKey);
112: }
113: }
|