001: package org.cougaar.servicediscovery.description;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Iterator;
006:
007: import org.cougaar.util.TimeSpan;
008: import org.cougaar.util.TimeSpans;
009:
010: /**
011: * <p>Title: </p>
012: * <p>Description: </p>
013: * <p>Copyright: Copyright (c) 2003</p>
014: * <p>Company: </p>
015: */
016:
017: /** Collection of utility methods for manipulation and
018: * construction of TimeSpan objects.
019: **/
020: public abstract class TimeInterval {
021:
022: /** make uninstantiable **/
023: private TimeInterval() {
024: }
025:
026: /** Removes specified TimeSpan interval from a Collection of TimeSpans **/
027: public static Collection removeInterval(TimeSpan interval,
028: Collection desiredCoverageIntervals) {
029:
030: if (interval == null) {
031: return desiredCoverageIntervals;
032: }
033:
034: ArrayList ret = new ArrayList();
035: for (Iterator it = desiredCoverageIntervals.iterator(); it
036: .hasNext();) {
037: TimeSpan current = (TimeSpan) it.next();
038: ret.addAll(removeInterval(interval, current));
039: }
040: return ret;
041: }
042:
043: /** Removes specified TimeSpan interval from a TimeSpan **/
044: public static Collection removeInterval(TimeSpan interval,
045: TimeSpan current) {
046: ArrayList ret = new ArrayList();
047:
048: if (interval == null) {
049: ret.add(current);
050: return ret;
051: }
052:
053: //intervals are not overlapping
054: //this ******
055: //interval *****
056: if (current.getStartTime() < interval.getStartTime()
057: && current.getEndTime() < interval.getEndTime()
058: && current.getEndTime() <= interval.getStartTime()) {
059: ret.add(current);
060: return ret;
061: }
062:
063: //intervals are not overlapping
064: //this ******
065: //interval *****
066: if (interval.getStartTime() < current.getStartTime()
067: && interval.getEndTime() < current.getEndTime()
068: && interval.getEndTime() <= current.getStartTime()) {
069: ret.add(current);
070: return ret;
071: }
072:
073: //this is completely contained
074: //return empty
075: //this ****
076: //interval *******
077: if (interval.getStartTime() <= current.getStartTime()
078: && interval.getEndTime() >= current.getEndTime()) {
079: return ret;
080: }
081:
082: //interval is completely contained
083: //return 0 or 1 or 2 time intervals
084: //this *********
085: //interval ****
086: if (current.getStartTime() <= interval.getStartTime()
087: && current.getEndTime() >= interval.getEndTime()) {
088:
089: if (current.getStartTime() < interval.getStartTime()) {
090: ret.add(TimeSpans.getSpan(current.getStartTime(),
091: interval.getStartTime()));
092: }
093: if (current.getEndTime() > interval.getEndTime()) {
094: ret.add(TimeSpans.getSpan(interval.getEndTime(),
095: current.getEndTime()));
096: }
097: return ret;
098: }
099:
100: //overlap w/o containing
101: //return 1 time interval
102: //this ******
103: //inteval *****
104: if (current.getStartTime() <= interval.getStartTime()
105: && current.getEndTime() <= interval.getEndTime()
106: && interval.getStartTime() < current.getEndTime()) {
107: ret.add(TimeSpans.getSpan(current.getStartTime(), interval
108: .getStartTime()));
109: return ret;
110: }
111:
112: //overlap w/o containing
113: //return 1 time interval
114: //this ******
115: //inteval *****
116: if (interval.getStartTime() <= current.getStartTime()
117: && interval.getEndTime() <= current.getEndTime()
118: && current.getStartTime() < interval.getEndTime()) {
119: ret.add(TimeSpans.getSpan(interval.getEndTime(), current
120: .getEndTime()));
121: return ret;
122: }
123:
124: return null;
125: }
126: }
|