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: package org.cougaar.glm.ldm.oplan;
027:
028: import java.io.Serializable;
029: import java.text.SimpleDateFormat;
030: import java.util.Calendar;
031: import java.util.Date;
032:
033: /**
034: * TimeSpan.
035: * <p>
036: * Interface change 10/25/99 to deprecate "delta" usage and replace
037: * with Date usage. <b>All deprecated methods and references to cDate
038: * will be removed! 01/12/2000 /b>
039: * <p>
040: */
041: public class TimeSpan implements Serializable, Cloneable,
042: org.cougaar.util.TimeSpan {
043:
044: private Date startDate;
045: private Date endDate;
046:
047: public TimeSpan() {
048: }
049:
050: public TimeSpan(Date startDate, Date endDate) {
051: this .startDate = internDate(startDate);
052: this .endDate = internDate(endDate);
053: }
054:
055: public TimeSpan(long startTime, long endTime) {
056: this (new Date(startTime), new Date(endTime));
057: }
058:
059: public boolean equals(Object o) {
060: if (o instanceof TimeSpan) {
061: TimeSpan ots = (TimeSpan) o;
062: return getStartDate().equals(ots.getStartDate())
063: && getEndDate().equals(ots.getEndDate());
064: }
065: return false;
066: }
067:
068: /**
069: * startDelta and endDelta are the DAY offsets from baseDate
070: * for the startDate and endDate, respectively.
071: */
072: public TimeSpan(Date baseDate, int startDelta, int endDelta) {
073: Calendar formatter = Calendar.getInstance();
074: formatter.setTime(baseDate);
075: formatter.add(formatter.DATE, startDelta);
076: this .startDate = internDate(formatter.getTime());
077:
078: formatter.setTime(baseDate);
079: formatter.add(formatter.DATE, endDelta);
080: this .endDate = internDate(formatter.getTime());
081:
082: this .cDate = internDate(baseDate); // to be removed
083: }
084:
085: public void setStartDate(Date startDate) {
086: this .startDate = internDate(startDate);
087: }
088:
089: public void setEndDate(Date endDate) {
090: this .endDate = endDate;
091: }
092:
093: /*
094: * @deprecated - use setEndDate
095: */
096: public void setThruDate(Date thruDate) {
097: printWarning("setThruDate");
098: setEndDate(thruDate);
099: }
100:
101: public Date getStartDate() {
102: return startDate;
103: }
104:
105: public Date getEndDate() {
106: return new Date(getEndTime());
107: }
108:
109: public long getStartTime() {
110: return startDate.getTime();
111: }
112:
113: public long getEndTime() {
114: return endDate.getTime();
115: }
116:
117: public Object clone() {
118: TimeSpan ts = new TimeSpan(startDate, endDate);
119: ts.cDate = cDate; // to be removed
120: return ts;
121: }
122:
123: /**
124: * THE FOLLOWING METHODS HAVE ALL BEEN DEPRECATED! THEY WILL BE REMOVED!
125: */
126:
127: /** cDate to be removed **/
128: private Date cDate;
129:
130: /**
131: * @see #TimeSpan(Date,Date)
132: * @deprecated cDate not needed
133: */
134: public TimeSpan(Date cDate, Date startDate, Date endDate) {
135: printWarning("TimeSpan");
136: this .cDate = internDate(cDate);
137: this .startDate = internDate(startDate);
138: this .endDate = internDate(endDate);
139: }
140:
141: /**
142: * @see #setThruDate
143: * @deprecated Using setStartDate removes the need for cDate
144: */
145: public void setStartDelta(int startDelta) {
146: printWarning("setStartDelta");
147: Calendar formatter = Calendar.getInstance();
148: formatter.setTime(cDate);
149: formatter.add(formatter.DATE, startDelta);
150: this .startDate = internDate(formatter.getTime());
151: }
152:
153: /**
154: * @see #setEndDate
155: * @deprecated Using setEndDate removes the need for cDate
156: */
157: public void setEndDelta(int endDelta) {
158: printWarning("setEndDelta");
159: Calendar formatter = Calendar.getInstance();
160: formatter.setTime(cDate);
161: formatter.add(formatter.DATE, endDelta);
162: this .endDate = internDate(formatter.getTime());
163: }
164:
165: /**
166: * @see #setThruDate
167: * @deprecated Using setEndDate removes the need for cDate
168: */
169: public void setThruDelta(int thruDelta) {
170: setEndDelta(thruDelta);
171: }
172:
173: /**
174: * TimeSpan "delta" to be removed. This utility not needed.
175: * @deprecated getStartDate removes the need for Oplan
176: */
177: public static Date getStartTime(int delta, Oplan oplan) {
178: printWarning("getStartTime");
179: Calendar formatter = Calendar.getInstance();
180: formatter.setTime(oplan.getCday());
181: formatter.add(formatter.DATE, delta);
182: return (formatter.getTime());
183: }
184:
185: /**
186: * TimeSpan "delta" to be removed. This utility not needed.
187: * @deprecated getStartDate removes the need for cDay
188: */
189: public static Date getStartTime(int delta, Date cday) {
190: printWarning("getStartTime");
191: Calendar formatter = Calendar.getInstance();
192: formatter.setTime(cday);
193: formatter.add(formatter.DATE, delta);
194: return (formatter.getTime());
195: }
196:
197: /**
198: * TimeSpan "delta" to be removed. This utility not needed.
199: * @deprecated getEndDate removes the need for Oplan
200: */
201: public static Date getEndTime(int delta, Oplan oplan) {
202: printWarning("getEndTime");
203: Calendar formatter = Calendar.getInstance();
204: formatter.setTime(oplan.getCday());
205: formatter.add(formatter.DATE, delta);
206: return (formatter.getTime());
207: }
208:
209: /**
210: * TimeSpan "delta" to be removed. This utility not needed.
211: * @deprecated getEndDate removes the need for cDay
212: */
213: public static Date getEndTime(int delta, Date cday) {
214: printWarning("getEndTime");
215: Calendar formatter = Calendar.getInstance();
216: formatter.setTime(cday);
217: formatter.add(formatter.DATE, delta);
218: return (formatter.getTime());
219: }
220:
221: private static void printWarning(String sMethodName) {
222: /* Silenced for now
223: Exception e = new java.lang.IllegalArgumentException("CALLING DEPRECATED METHOD org.cougaar.glm.ldm.oplan.TimeSpan."+sMethodName+"()!");
224: e.printStackTrace();
225: */
226: }
227:
228: private static final java.util.HashMap dateCache = new java.util.HashMap(
229: 89);
230:
231: private static final Date internDate(Date d) {
232: synchronized (dateCache) {
233: Date id = (Date) dateCache.get(d);
234: if (id != null)
235: return id;
236: dateCache.put(d, d);
237: return d;
238: }
239: }
240:
241: public String toString() {
242: return formatDate(startDate) + " - " + formatDate(endDate);
243: }
244:
245: private static SimpleDateFormat dateFormat = new SimpleDateFormat(
246: "MM/dd/yy HH:mm");
247:
248: private static String formatDate(long time) {
249: return formatDate(new Date(time));
250: }
251:
252: private static String formatDate(Date date) {
253: return dateFormat.format(date);
254: }
255: }
|