001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2007 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: package org.cougaar.planning.ldm.measure;
027:
028: /**
029: * An operator that can be applied on two measures.
030: *
031: * @see Measure#apply(BinaryOperator,Measure)
032: */
033: public interface BinaryOperator<M extends Measure> {
034:
035: /**
036: * Apply this operator to the specified measures.
037: * @param a a measure, or null for zero
038: * @param b a measure, or null for zero
039: * @return the operator result, where null indicates zero. If both
040: * of the input measures are non-zero then this method will not
041: * return null.
042: */
043: M apply(M a, M b);
044:
045: Add ADD = new Add();
046:
047: class Add<M extends Measure> implements BinaryOperator<M> {
048: public M apply(M a, M b) {
049: return (a == null ? b : b == null ? a : (M) a.add(b));
050: }
051: }
052:
053: /** @see Measure#subtract */
054: Subtract SUBTRACT = new Subtract();
055:
056: class Subtract<M extends Measure> implements BinaryOperator<M> {
057: public M apply(M a, M b) {
058: return (a == null ? b : b == null ? a : (M) a.subtract(b));
059: }
060: }
061:
062: /** return the "a" graph */
063: First FIRST = new First();
064:
065: class First<M extends Measure> implements BinaryOperator<M> {
066: public M apply(M a, M b) {
067: return a;
068: }
069: }
070:
071: /** return the "b" graph */
072: Second SECOND = new Second();
073:
074: class Second<M extends Measure> implements BinaryOperator<M> {
075: public M apply(M a, M b) {
076: return b;
077: }
078: }
079:
080: // Multiply for GenericDerivative?
081:
082: /** Select the measure with the minimum {@link Measure#getNativeValue} */
083: Min MIN = new Min();
084:
085: class Min<M extends Measure> implements BinaryOperator<M> {
086: public M apply(M a, M b) {
087: return (a == null ? (b == null ? null : (M) b.min(null))
088: : (M) a.min(b));
089: }
090: }
091:
092: /** Select the measure with the maximum {@link Measure#getNativeValue} */
093: Max MAX = new Max();
094:
095: class Max<M extends Measure> implements BinaryOperator<M> {
096: public M apply(M a, M b) {
097: return (a == null ? (b == null ? null : (M) b.max(null))
098: : (M) a.max(b));
099: }
100: }
101: }
|