package mypackage;
import java.beans.*;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class RuleTimerTestClass implements Serializable
{
private static final long serialVersionUID = 1205628167835531094L;
private double expireInterval;
private double expireTime;
private double timeBegin;
private boolean timedOut = false;
private double timeLeft;
private final String uniqueName;
private final SimpleDateFormat df = new SimpleDateFormat("MMM d yyyy HH:mm:ss.SSS 'GMT'");
/**
* Support object for JavaBean properties.
*/
public final PropertyChangeSupport changes = new PropertyChangeSupport(this);
public RuleTimerTestClass()
{
this("default",0);
}
/**
* Constructor with time out set to 0
.
*
*
*/
public RuleTimerTestClass(String uniqueName)
{
this(uniqueName, 0);
}
/**
* Class constructor with a timeout setting in total seconds.
*
*/
public RuleTimerTestClass(String uniqueName, double seconds)
{
this(uniqueName, 0, 0, seconds);
}
/**
* Class constructor with a timeout setting specified in hours, minutes, and seconds.
*
*/
public RuleTimerTestClass(String uniqueName, int hours, int minutes, double seconds)
{
df.setTimeZone(TimeZone.getTimeZone("GMT"));
if (hours < 0)
{
hours = 0;
}
if (minutes < 0)
{
minutes = 0;
}
if (seconds < 0)
{
seconds = 0;
}
double totalSeconds = seconds + (minutes * 60) + (hours * 3600);
this.uniqueName = uniqueName;
{
if (uniqueName == null)
{
uniqueName = "default";
}
}
if (totalSeconds < 0)
{
this.expireInterval = 0;
}
else
this.expireInterval = totalSeconds;
setTimedOut(false);
timeLeft = this.expireInterval;
timeBegin = System.currentTimeMillis() / 1000.0;
expireTime = timeBegin + this.expireInterval;
}
/**
* Starts this RuleTimer anew, marking count-down from the current system wall time.
*
*/
public final void start()
{
setTimedOut(false);
timeLeft = expireInterval;
timeBegin = System.currentTimeMillis() / 1000.0;
expireTime = timeBegin + this.expireInterval;
}
/**
* Sets the timeLeft property by calculating the offset from when this RuleTimer was
* last started. If the timer has timed out, the Drools fact that corresponds to this
* RuleTimer will be updated.
*
*/
public void calculateTimeLeft()
{
timeLeft = expireInterval - ((System.currentTimeMillis() / 1000.0) - timeBegin);
if (timeLeft <= 0)
setTimedOut(true);
}
/**
* Reports the amount of time left in the count-down for this timer.
*/
public final double getTimeLeft()
{
return timeLeft;
}
/**
* Reports if this RuleTimer has expired.
*
*/
public final boolean isTimedOut()
{
return timedOut;
}
public final void setTimedOut(final boolean b)
{
final boolean oldTimedOut = timedOut;
timedOut = b;
changes.firePropertyChange("timedOut", oldTimedOut, timedOut);
}
/**
* Returns the time stamp (number of seconds since the UNIX epoch) this RuleTimer was
* initialized with the last time it was started.
*/
public final double getTimeBegin()
{
return timeBegin;
}
/**
* Returns the total number of seconds this RuleTimer is active for during a count-down.
*
*/
public final double getExpireInterval()
{
return expireInterval;
}
/**
* Sets the total number of hours, minutes, and seconds this RuleTimer is active
* for.
*
*/
public final void setFullExpireInterval(int hours, int minutes, double seconds)
{
double oldExpireInterval = this.expireInterval;
if (hours < 0)
{
hours = 0;
}
if (minutes < 0)
{
minutes = 0;
}
if (seconds < 0)
{
seconds = 0;
}
this.expireInterval = seconds + (minutes * 60) + (hours * 3600);
this.changes.firePropertyChange("expireInterval", oldExpireInterval, this.expireInterval);
}
/**
* Sets the total number of seconds this RuleTimer is active for.
*/
public final void setExpireInterval(double seconds)
{
setFullExpireInterval(0, 0, seconds);
}
/**
* Returns the unique name of this RuleTimer.
*
*
* This enables the field uniqueName to be used in the LHS of a rule. */ public final String getUniqueName() { return uniqueName; } /** * Returns the time stamp (number of seconds since the UNIX epoch) at which this * RuleTimer is scheduled to time out. */ public final double getExpireTime() { return expireTime; } /** * Adds a JavaBean property listener. * * @param l */ public void addPropertyChangeListener(final PropertyChangeListener l) { this.changes.addPropertyChangeListener(l); } /** * Removes a JavaBean property listener. * * @param l */ public void removePropertyChangeListener(final PropertyChangeListener l) { this.changes.removePropertyChangeListener(l); } /** * Output this RuleTimer in human-readable form. */ @Override public String toString() { StringBuilder sb = new StringBuilder(); String newline = "\n"; sb.append(newline + "************************************" + newline); sb.append("* RuleTimer '"); sb.append(this.uniqueName); sb.append("' " + newline); sb.append("************************************" + newline); sb.append("Expiration interval: " + intervalFormat(this.expireInterval) + newline); sb.append("Started at: " + df.format(new Date((long)(this.timeBegin*1000))) + newline); sb.append("Expires at: " + df.format(new Date((long)(this.expireTime*1000))) + newline); if (!this.timedOut) sb.append("Time left before expire: " + intervalFormat(this.timeLeft) + newline); else sb.append(" ---EXPIRED--- " + newline); return sb.toString(); } private String intervalFormat(double time) { double leftOver = 0.0; int hours = (int) Math.floor(time / 3600); leftOver = time % 3600; int minutes = (int) Math.floor(leftOver / 60); double seconds = leftOver % 60; StringBuilder sb = new StringBuilder(); if (hours > 1) sb.append(hours + " hours"); else if (hours == 1) sb.append(hours + " hour"); if (hours > 0 && minutes > 0) sb.append(", "); if (minutes > 1) sb.append(minutes + " minutes"); else if (minutes == 1) sb.append(minutes + " minute"); if ((minutes > 0 || hours > 0) && seconds > 0) sb.append(", "); if (seconds > 0) sb.append(String.format("%.1f", seconds) + " seconds"); sb.append(" (total secs: " + String.format("%.1f", time) + ")"); return sb.toString(); } }