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.util;
028:
029: import java.io.Serializable;
030: import java.text.SimpleDateFormat;
031: import java.util.Date;
032:
033: /**
034: * Implementation of NewTimeSpan interface
035: * @see NewTimeSpan
036: */
037: public class MutableTimeSpan implements NewTimeSpan, Serializable {
038:
039: private long myStartTime = MIN_VALUE;
040: private long myEndTime = MAX_VALUE;
041:
042: /**
043: * Constructor - startTime initialized to TimeSpan.MIN_VALUE, endTime
044: * initialized to TimeSpan.MAX_VALUE
045: */
046: public MutableTimeSpan() {
047: }
048:
049: /** The first point in time to be considered part of the
050: * interval.
051: * @return MIN_VALUE IFF unbounded.
052: **/
053: public long getStartTime() {
054: return myStartTime;
055: }
056:
057: /** The first point in time after start to be considered
058: * <em> not </em> part of the interval.
059: * @return MAX_VALUE IFF unbounded.
060: **/
061: public long getEndTime() {
062: return myEndTime;
063: }
064:
065: /**
066: * setTimeSpan - sets the start and end time of the time span.
067: * Expected to enforce that startTime < endTime
068: *
069: * @throws IllegalArgumentException if startTime >= endTime
070: */
071: public void setTimeSpan(long startTime, long endTime) {
072: if ((startTime >= MIN_VALUE) && (endTime <= MAX_VALUE)
073: && (endTime >= startTime + EPSILON)) {
074: myStartTime = startTime;
075: myEndTime = endTime;
076: } else {
077: throw new IllegalArgumentException("startTime ("
078: + startTime + ") >= endTime (" + endTime
079: + " or outside bounds: MIN_VALUE=" + MIN_VALUE
080: + ", MAX_VALUE=" + MAX_VALUE);
081: }
082: }
083:
084: /**
085: * equals - performs field by field comparison
086: *
087: * @param object Object to compare
088: * @return boolean if 'same'
089: */
090: public boolean equals(Object object) {
091: if (object == this ) {
092: return true;
093: }
094:
095: if (!(object instanceof TimeSpan)) {
096: return false;
097: }
098:
099: TimeSpan other = (TimeSpan) object;
100: return ((getStartTime() == other.getStartTime()) && (getEndTime() == other
101: .getEndTime()));
102: }
103:
104: public int hashCode() {
105: return (int) (myStartTime + (myEndTime * 1000));
106: }
107:
108: public String toString() {
109: return "[" + dateString(new Date(myStartTime)) + " - "
110: + dateString(new Date(myEndTime)) + "]";
111: }
112:
113: public static String dateString(Date date) {
114: SimpleDateFormat dateTimeFormat_ = new SimpleDateFormat(
115: "MM/dd/yyyy HH:mm:ss.SSS z");
116: String sdate = dateTimeFormat_.format(date);
117: // map '9/8/00 12:00 AM' to ' 9/8/00 12:00 AM'
118: while (sdate.length() < 17) {
119: sdate = " " + sdate;
120: }
121: return sdate;
122: }
123: }
|