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: /**
030: * Contains metrics on transaction open and close timestamps.
031: * <p>
032: * @see EnvelopeMetricsSubscription
033: */
034: public final class EnvelopeMetrics implements java.io.Serializable {
035:
036: /**
037: * If "isBlackboard()" is true, then the "getName()" response
038: * is == to this interned "<blackboard>" string constant.
039: */
040: public static final String BLACKBOARD = "<blackboard>";
041:
042: /**
043: * If "isBlackboard()" is false, and the name is not known,
044: * the then "getName()" response is == to the interned
045: * "<unknown>" string constant.
046: */
047: public static final String UNKNOWN = "<unknown>";
048:
049: private final String name;
050: private final long openTime;
051: private final long closeTime;
052:
053: public EnvelopeMetrics(TimestampedEnvelope te) {
054: this .name = _getName(te);
055: this .openTime = te.getTransactionOpenTime();
056: this .closeTime = te.getTransactionCloseTime();
057:
058: // could also easily get the raw tuples and
059: // count the number of adds / changes / removes
060: }
061:
062: /**
063: * @return true if the envelope is from the blackboard (LPs)
064: */
065: public final boolean isBlackboard() {
066: return (name == null);
067: }
068:
069: /**
070: * @return the name of the subscriber that created this envelope.
071: * @see #BLACKBOARD
072: * @see #UNKNOWN
073: */
074: public final String getName() {
075: return ((name != null) ? name : BLACKBOARD);
076: }
077:
078: /**
079: * @return time in milliseconds when the transaction was opened
080: */
081: public final long getTransactionOpenTime() {
082: return openTime;
083: }
084:
085: /**
086: * @return time in milliseconds when the transaction was closed
087: */
088: public final long getTransactionCloseTime() {
089: return closeTime;
090: }
091:
092: //
093: // use the name as the hash-code and equality?
094: //
095:
096: public String toString() {
097: return "EnvelopeMetrics {" + "\n bb: " + isBlackboard()
098: + "\n name: " + getName() + "\n open: " + openTime
099: + "\n close: " + closeTime + " (+"
100: + (closeTime - openTime) + ")" + "}";
101: }
102:
103: // helper for constructor
104: private static final String _getName(TimestampedEnvelope te) {
105: if (te.isBlackboard()) {
106: return null;
107: }
108: String s = te.getName();
109: if (s == null) {
110: return UNKNOWN;
111: }
112: /*
113: // is this worth checking?
114: if (s.equals(BLACKBOARD)) {
115: return "dup"+BLACKBOARD;
116: } else if (s.equals(UNKNOWN)) {
117: return "dup"+UNKNOWN;
118: }
119: */
120: return s;
121: }
122:
123: private static final long serialVersionUID = -5208392019823789283L;
124: }
|