/* XBN Java: Generically useful, non-GUI Java code. http://sourceforge.net/projects/xbnjava Copyright (C) 1997-2003, Jeff Epstein All rights reserved. Modifications: No Redistribution in binary form, with or without modifications, are permitted provided that the following conditions are met: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * If modifications are made to source code then this license should indicate that fact in the "Modifications" section above. * Neither the author, nor the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [NOTE: This license contains NO advertising clause.] */ package xbn.util; import xbn.XBNObject; import java.util.Date; /**
For keeping track of things that have a delay, or expire within a given number of units.
Source code: TimerUnits.java. Example code See the example code for TimerSeconds.
Create a TimerUnits that does not expire.
Equal to TimerUnits(0, -1)
Create a TimerUnits.
Equal to TimerUnits(l_millsMultiplier, i_unitsToExpire, false)
Create a TimerUnits.
Equal to TimerUnits(l_millsMultiplier, i_unitsToExpire, b_crashIfMidStartOrStop, b_crashIfMidStartOrStop)
Create a TimerUnits.
@param l_millsMultiplier This number times i_unitsToExpire is the number of milleseconds until expiration. See getMillsMultiplier. @param i_unitsToExpire The number of units for the delay/until expiration. If -1, then there is no delay (expiration never occurs). Otherwise, must be greater than or equal to one. See getSeconds. @param b_crashIfStartStart Is it legal to start the timer when already started? See doCrashIfStartStart. @param b_crashIfStopStop Is it legal to stop the timer when already stopped? See doCrashIfStopStop. **/ public TimerUnits(long l_millsMultiplier, int i_unitsToExpire, boolean b_crashIfStartStart, boolean b_crashIfStopStop) { if(i_unitsToExpire != -1 && i_unitsToExpire < 1) { throwAX("constructor: i_unitsToExpire (" + i_unitsToExpire + ") may equal -1--meaning no delay, or must be greater than zero."); } lMillsMultiplier = l_millsMultiplier; iUnitsToExpire = i_unitsToExpire; bCrashIfStartStart = b_crashIfStartStart; bCrashIfStopStop = b_crashIfStopStop; dStarted = null; } /**Start the timer. getSeconds, from the moment when this function is called, is when isReady will return true.
See stop.
@exception AssertException If already started and doCrashIfStartStart equals true. **/ public void start() { if(doCrashIfStartStart() && isStarted()) { throwAX("start: Both doCrashIfStartStart() and isStarted() equal true. Already started."); } if(!doesExpire()) { return; } dStarted = new Date(); } /**Stop the timer.
See start.
@exception AssertException If already stopped and doCrashIfStopStop equals true. **/ public void stop() { if(doCrashIfStopStop() && isStopped()) { throwAX("end: Both doCrashIfStopStop() and isStopped() equal true. Already stopped."); } if(!doesExpire()) { return; } dStarted = null; } /**When the timer is already started, is it legal to start it again?
@return true If it is only legal to start the timer when it is stopped.When the timer is already stopped, is it legal to stop it again?
@return true If it is only legal to stop the timer when it is started.Get the total number of units until expiration. This number, multiplied by getMillsMultiplier, is the number of milliseconds until expiration.
This number represents the units from the moment this TimerUnits is started, to the moment it expires. It does not change during the life of this object.
**/ public int getUnitsToExpire() { return iUnitsToExpire; } /**Get the millesecond multiplier for this TimerUnits. This number, multiplied by getUnitsToExpire, is the number of milliseconds until expiration.
**/ public long getMillsMultiplier() { return lMillsMultiplier; } /**Get the number of milleseconds until expiration.
@return(getUnitsToExpire() * getMillsMultiplier())
**/
public long getMillsToExpire() {
return (getUnitsToExpire() * getMillsMultiplier());
}
/**
Does this timer ever expire?
@return(getUnitsToExpire() != -1)
**/
public boolean doesExpire() {
return (getUnitsToExpire() != -1);
}
/**
Has the timer been started?
@return(getDateStarted() != null)
**/
public boolean isStarted() {
return (getDateStarted() != null);
}
/**
Has the timer been stopped?
@return!isStarted()
**/
public boolean isStopped() {
return !isStarted();
}
/**
Get the date representing the moment the timer was started.
@return A Date If the timer was started.Have the number of units to expiration gone a went?
When this function returns true, the timer is stopped.
@return true If the timer has not yet been started.[UtilDate].getNowMinusYUnits(getDateStarted(), getMillsMultiplier()) >= getUnitsToExpire())
**/
public synchronized boolean hasExpired() {
if(isStopped()) {
return true;
}
boolean bReady = (uDate.getNowMinusYUnits(getDateStarted(), getMillsMultiplier()) >= getUnitsToExpire());
if(bReady) {
stop();
}
return bReady;
}
/**
Get some information about this TimerUnits.
**/ public String toString() { return this.getClass().getName() + ": doCrashIfStartStart()=" + doCrashIfStartStart() + ", getMillsMultiplier()=" + getMillsMultiplier() + ", getUnitsToExpire()=" + getUnitsToExpire() + ", isStarted()=" + isStarted() + ", getDateStarted()=" + getDateStarted() + sES; } }