001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.perseus.model;
027:
028: import java.util.Vector;
029:
030: /**
031: * A <code>TimeInterval</code> models a specific 'run' of a
032: * <code>TimedElement</code>. It has a begin and end time and
033: * a list of <code>IntervalTimeInstance</code>s depending on its begin
034: * or end time.
035: *
036: * @version $Id: TimeInterval.java,v 1.3 2006/04/21 06:39:30 st125089 Exp $
037: */
038: public final class TimeInterval {
039: /**
040: * The interval's begin time.
041: */
042: Time begin;
043:
044: /**
045: * The interval's end time
046: */
047: Time end;
048:
049: /**
050: * The end of the last simple duration
051: */
052: Time lastDur;
053:
054: /**
055: * The list of dependent begin time instances.
056: * Contains <code>IntervalTimeInstance</code> objects.
057: */
058: Vector beginDependents;
059:
060: /**
061: * The list of end dependents. Contains
062: * <code>IntervalTimeInstance</code> objects.
063: */
064: Vector endDependents;
065:
066: /**
067: * Creates a new interval with a specific begin and end times.
068: *
069: * @param begin the initial begin time. This time should be resolved.
070: * Otherwise, an <code>IllegalStateException</code> is thrown.
071: * Should not be null.
072: * @param end the initial end time. Should not be null.
073: */
074: TimeInterval(final Time begin, final Time end) {
075: if (begin == null || end == null) {
076: throw new NullPointerException();
077: }
078:
079: setBegin(begin);
080: setEnd(end);
081: }
082:
083: /**
084: * Adds a new <code>IntervalTimeInstance</code> dependent.
085: * If <code>timeInstance</code> synchronizes on begin, it is
086: * added to the <code>beginDependent</code> list. Otherwise,
087: * it is added to the <code>endDependent</code> list.
088: *
089: * @param timeInstance the new <code>IntervalTimeInstance</code>.
090: * If null, throws a <code>NullPointerException</code>.
091: */
092: void addDependent(final IntervalTimeInstance timeInstance) {
093: Vector dependents = beginDependents;
094: if (!timeInstance.isBeginSync) {
095: dependents = endDependents;
096: }
097:
098: if (dependents == null) {
099: dependents = new Vector(1);
100: if (timeInstance.isBeginSync) {
101: beginDependents = dependents;
102: } else {
103: endDependents = dependents;
104: }
105: }
106:
107: dependents.addElement(timeInstance);
108: }
109:
110: /**
111: * Removes the input <code>IntervalTimeInstance</code> dependent.
112: *
113: * @param timeInstance the <code>IntervalTimeInstance</code> to
114: * remove from this interval. Throws a
115: * <code>NullPointerException</code> if null.
116: */
117: void removeDependent(final IntervalTimeInstance timeInstance) {
118: Vector dependents = beginDependents;
119: if (!timeInstance.isBeginSync) {
120: dependents = endDependents;
121: }
122:
123: if (dependents == null) {
124: return;
125: }
126:
127: dependents.removeElement(timeInstance);
128: }
129:
130: /**
131: * Updates the begin time. Note that an unresolved begin time is
132: * illegal. Trying to set one will cause an exception to be thrown
133: * (an IllegalArgumentException).
134: * Dependent end conditions are notified of begin time change.
135: *
136: * @param newBegin the new begin time.
137: */
138: void setBegin(final Time newBegin) {
139: if (!newBegin.isResolved()) {
140: throw new IllegalArgumentException("" + newBegin);
141: }
142:
143: this .begin = newBegin;
144: if (beginDependents != null) {
145: int n = beginDependents.size();
146: for (int i = 0; i < n; i++) {
147: ((IntervalTimeInstance) beginDependents.elementAt(i))
148: .onIntervalUpdate();
149: }
150: }
151: }
152:
153: /**
154: * Updates the end time. Dependent end conditions are notified
155: * of the end time change. A
156: *
157: * @param newEnd the new end time.
158: */
159: void setEnd(final Time newEnd) {
160: if (newEnd == null) {
161: throw new NullPointerException();
162: }
163: this .end = newEnd;
164: if (endDependents != null) {
165: int n = endDependents.size();
166: for (int i = 0; i < n; i++) {
167: ((IntervalTimeInstance) endDependents.elementAt(i))
168: .onIntervalUpdate();
169: }
170: }
171: }
172:
173: /**
174: * Called when the interval is pruned from a timed element. The result is
175: * that all dependent time instances should be removed from their respective
176: * instance lists.
177: */
178: void prune() {
179: int n = beginDependents != null ? beginDependents.size() : 0;
180: for (int i = n - 1; i >= 0; i--) {
181: IntervalTimeInstance iti = (IntervalTimeInstance) beginDependents
182: .elementAt(i);
183: iti.timedElement.removeTimeInstance(iti);
184: iti.dispose();
185: }
186:
187: n = endDependents != null ? endDependents.size() : 0;
188: for (int i = n - 1; i >= 0; i--) {
189: IntervalTimeInstance iti = (IntervalTimeInstance) endDependents
190: .elementAt(i);
191: iti.timedElement.removeTimeInstance(iti);
192: iti.dispose();
193: }
194: }
195: }
|