From: <dr...@us...> - 2003-06-12 00:48:16
|
Update of /cvsroot/webmacro/webmacro/src/org/opendoors/util In directory sc8-pr-cvs1:/tmp/cvs-serv6675/src/org/opendoors/util Modified Files: PostponeObservable.java Timer.java Log Message: If this doesn't drive our SF "activity" stats through the roof, I don't know what will. Mass re-formatting of all code, following (to the best of my interpertation) the Sun (w/ jakarta tweaks) coding style guidelines defined here: http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html and here: http://jakarta.apache.org/turbine/common/code-standards.html I did this reformatting with IDEA 3.0.5, and I am going to commit the style configuration for IDEA (if I can find it). Index: PostponeObservable.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/opendoors/util/PostponeObservable.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** PostponeObservable.java 11 Jun 2002 17:43:20 -0000 1.4 --- PostponeObservable.java 12 Jun 2003 00:47:43 -0000 1.5 *************** *** 15,19 **** import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; ! import java.util.*; /** --- 15,20 ---- import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; ! import java.util.Observable; ! import java.util.Observer; /** *************** *** 48,165 **** * @see java.util.Observer */ ! public class PostponeObservable extends Observable implements PropertyChangeListener { ! //-------public members----- ! /** ! * On property change, postpone event notification this number of millis. ! */ ! protected int postponeInterval = 120000; //2 minutes ! /** ! * While waiting to fire a notification, ! * reset the interval if another change comes in. ! */ ! protected boolean resetClockOnUpdate = true; ! //-------private and protected members----- ! private long timeToNotify = System.currentTimeMillis(); ! private Timer tick = null; ! //-------constructor(s)----- ! /** ! * Constructs a default observable. ! */ ! public PostponeObservable() { ! init(); ! } ! /** ! * Constructs a default observable with the following settings. ! * @param The interval to wait in millis between notifications. ! * @param Resets the clock so that changes can be aggregated ! * over a period of time ! */ ! public PostponeObservable(int postponeInterval, boolean resetClockOnUpdate) { ! this.postponeInterval = postponeInterval; ! this.resetClockOnUpdate = resetClockOnUpdate; ! init(); ! } ! /** Initializes the instance. */ ! protected void init() { ! tick = new Timer("PropertyObservable", postponeInterval, false); ! tick.addObserver(new TimerObserver()); ! } ! //-------public initializers/destroyers----- ! /** Sets the observable period. */ ! public void setPostponePeriod(int postponeInterval) { ! this.postponeInterval = postponeInterval; ! tick.setPeriod(postponeInterval); ! } ! /** Enables postponeability if true. */ ! public void enablePostponeability(boolean enable) { ! this.resetClockOnUpdate = enable; ! } ! //-------public event handlers----- ! /** ! * Using the property event model ! * propagate a change event to the observable. ! * @param evt The property change event which can be null. ! */ ! public void propertyChange(PropertyChangeEvent evt) { ! setChanged(); ! } ! /** ! * Call back from the timer when ! * the observation period has expired. ! */ ! public void timerAction() { ! if (hasChanged() && (timeToNotify < System.currentTimeMillis())) { ! notifyObservers(); ! } ! } ! /** ! * Signals that the observable has changed. ! * <p> ! * Observers will be ! * notified when ! * <pre> ! * current time > (time of last change + postponeInterval) ! * </pre> ! * provided postponeability is enabled. ! */ ! public void setChanged() { ! super.setChanged(); ! if (resetClockOnUpdate) { ! timeToNotify = (System.currentTimeMillis() + postponeInterval); ! } ! } ! /** Destoys this instance and the associated timer. */ ! public void destroy() { ! tick.stop(); ! tick = null; ! } ! /** ! * Class which listens to updates in the observable tick and calls ! * the timer notifiction method. ! */ ! class TimerObserver implements Observer { ! public void update(Observable o, Object arg) { ! timerAction(); ! } ! } --- 49,180 ---- * @see java.util.Observer */ ! public class PostponeObservable extends Observable implements PropertyChangeListener ! { ! //-------public members----- ! /** ! * On property change, postpone event notification this number of millis. ! */ ! protected int postponeInterval = 120000; //2 minutes ! /** ! * While waiting to fire a notification, ! * reset the interval if another change comes in. ! */ ! protected boolean resetClockOnUpdate = true; ! //-------private and protected members----- ! private long timeToNotify = System.currentTimeMillis(); ! private Timer tick = null; ! //-------constructor(s)----- ! /** ! * Constructs a default observable. ! */ ! public PostponeObservable () ! { ! init(); ! } ! /** ! * Constructs a default observable with the following settings. ! * @param The interval to wait in millis between notifications. ! * @param Resets the clock so that changes can be aggregated ! * over a period of time ! */ ! public PostponeObservable (int postponeInterval, boolean resetClockOnUpdate) ! { ! this.postponeInterval = postponeInterval; ! this.resetClockOnUpdate = resetClockOnUpdate; ! init(); ! } ! /** Initializes the instance. */ ! protected void init () ! { ! tick = new Timer("PropertyObservable", postponeInterval, false); ! tick.addObserver(new TimerObserver()); ! } ! //-------public initializers/destroyers----- ! /** Sets the observable period. */ ! public void setPostponePeriod (int postponeInterval) ! { ! this.postponeInterval = postponeInterval; ! tick.setPeriod(postponeInterval); ! } ! /** Enables postponeability if true. */ ! public void enablePostponeability (boolean enable) ! { ! this.resetClockOnUpdate = enable; ! } ! //-------public event handlers----- ! /** ! * Using the property event model ! * propagate a change event to the observable. ! * @param evt The property change event which can be null. ! */ ! public void propertyChange (PropertyChangeEvent evt) ! { ! setChanged(); ! } ! /** ! * Call back from the timer when ! * the observation period has expired. ! */ ! public void timerAction () ! { ! if (hasChanged() && (timeToNotify < System.currentTimeMillis())) ! { ! notifyObservers(); ! } ! } ! /** ! * Signals that the observable has changed. ! * <p> ! * Observers will be ! * notified when ! * <pre> ! * current time > (time of last change + postponeInterval) ! * </pre> ! * provided postponeability is enabled. ! */ ! public void setChanged () ! { ! super.setChanged(); ! if (resetClockOnUpdate) ! { ! timeToNotify = (System.currentTimeMillis() + postponeInterval); ! } ! } ! /** Destoys this instance and the associated timer. */ ! public void destroy () ! { ! tick.stop(); ! tick = null; ! } ! /** ! * Class which listens to updates in the observable tick and calls ! * the timer notifiction method. ! */ ! class TimerObserver implements Observer ! { ! public void update (Observable o, Object arg) ! { ! timerAction(); ! } ! } Index: Timer.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/opendoors/util/Timer.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Timer.java 11 Jun 2002 17:43:20 -0000 1.4 --- Timer.java 12 Jun 2003 00:47:43 -0000 1.5 *************** *** 13,18 **** package org.opendoors.util; ! import java.io.*; ! import java.util.*; /** --- 13,18 ---- package org.opendoors.util; ! import java.io.Serializable; ! import java.util.Observable; /** *************** *** 30,161 **** * observable such as an observable which fires every 24 hours. */ ! public class Timer extends Observable implements Runnable, Serializable { ! /** The timer thread. */ ! private transient Thread timerThread; ! /** Signal to stop the thread. */ ! private transient boolean stopThread = false; ! /** The name. */ ! private String name; ! /** The interval for firing. */ ! private int period; // in milliseconds ! /** Is a one shot timer. */ ! private boolean oneShot; ! /** ! * Public constructor to create a timer event source using factory defaults ! * which are 1 second timer, recurring. ! */ ! public Timer() { ! this("PerSecondTimer", 1000, false); ! } ! /** ! * Construct a timer with the certain properties set. ! * @param The name of the timer. ! * @param The period in milliseconds. ! * @param True if this is a oneShot instance ! */ ! public Timer(String name, int period, boolean oneShot) { ! super(); ! // Set properties ! this.name = name; ! this.period = period; ! this.oneShot = oneShot; ! // Create the clock thread ! timerThread = new Thread(this, name); ! timerThread.setDaemon(true); ! timerThread.start(); ! } ! /** ! * Stop the timer ! */ ! public void stop() { ! stopThread = true; ! } ! // Accessor methods ! /** ! * Gets the name of the timer. ! * @return The timer name. ! */ ! public String getName() { ! return name; ! } ! /** ! * Sets the name of the timer. ! * @param The textual name. ! */ ! public void setName(String n) { ! name = n; ! } ! /** ! * Gets the period between timer events. ! * @return The interval in milliseconds ! */ ! public int getPeriod() { ! return period; ! } ! /** ! * Sets the period in milliseconds. ! * @param The interval. ! */ ! public void setPeriod(int p) { ! period = p; ! if (timerThread != null) ! timerThread.interrupt(); ! } ! /** ! * Interrogates if this is a one-shot operation. ! * @return True, if a one-shot operation. ! */ ! public boolean isOneShot() { ! return oneShot; ! } ! /** ! * Set this object to be one-shot or not. ! * @param True, if one-shot operation desired ! */ ! public void setOneShot(boolean os) { ! oneShot = os; ! } ! /** ! * Run the timer. ! */ ! public void run() { ! while (timerThread != null) { ! // Sleep for the period ! try { ! timerThread.sleep(period); ! } ! catch (InterruptedException e) { ! // Restart the loop ! continue; ! } ! // Fire an action event ! setChanged(); ! notifyObservers(); ! if (oneShot || stopThread) ! break; ! } ! // clean up: ! timerThread = null; ! } } --- 30,175 ---- * observable such as an observable which fires every 24 hours. */ ! public class Timer extends Observable implements Runnable, Serializable ! { ! /** The timer thread. */ ! private transient Thread timerThread; ! /** Signal to stop the thread. */ ! private transient boolean stopThread = false; ! /** The name. */ ! private String name; ! /** The interval for firing. */ ! private int period; // in milliseconds ! /** Is a one shot timer. */ ! private boolean oneShot; ! /** ! * Public constructor to create a timer event source using factory defaults ! * which are 1 second timer, recurring. ! */ ! public Timer () ! { ! this("PerSecondTimer", 1000, false); ! } ! /** ! * Construct a timer with the certain properties set. ! * @param The name of the timer. ! * @param The period in milliseconds. ! * @param True if this is a oneShot instance ! */ ! public Timer (String name, int period, boolean oneShot) ! { ! super(); ! // Set properties ! this.name = name; ! this.period = period; ! this.oneShot = oneShot; ! // Create the clock thread ! timerThread = new Thread(this, name); ! timerThread.setDaemon(true); ! timerThread.start(); ! } ! /** ! * Stop the timer ! */ ! public void stop () ! { ! stopThread = true; ! } ! // Accessor methods ! /** ! * Gets the name of the timer. ! * @return The timer name. ! */ ! public String getName () ! { ! return name; ! } ! /** ! * Sets the name of the timer. ! * @param The textual name. ! */ ! public void setName (String n) ! { ! name = n; ! } ! /** ! * Gets the period between timer events. ! * @return The interval in milliseconds ! */ ! public int getPeriod () ! { ! return period; ! } ! /** ! * Sets the period in milliseconds. ! * @param The interval. ! */ ! public void setPeriod (int p) ! { ! period = p; ! if (timerThread != null) ! timerThread.interrupt(); ! } ! /** ! * Interrogates if this is a one-shot operation. ! * @return True, if a one-shot operation. ! */ ! public boolean isOneShot () ! { ! return oneShot; ! } ! /** ! * Set this object to be one-shot or not. ! * @param True, if one-shot operation desired ! */ ! public void setOneShot (boolean os) ! { ! oneShot = os; ! } ! /** ! * Run the timer. ! */ ! public void run () ! { ! while (timerThread != null) ! { ! // Sleep for the period ! try ! { ! timerThread.sleep(period); ! } ! catch (InterruptedException e) ! { ! // Restart the loop ! continue; ! } ! // Fire an action event ! setChanged(); ! notifyObservers(); ! if (oneShot || stopThread) ! break; ! } ! // clean up: ! timerThread = null; ! } } |