Package edu.cnu.mdi.util
Class Ping
java.lang.Object
edu.cnu.mdi.util.Ping
- All Implemented Interfaces:
AutoCloseable
Simple Swing-based timer that periodically notifies registered
IPing
listeners.
Internally this class uses a Timer, so all callbacks
occur on the Swing Event Dispatch Thread (EDT). This makes it suitable for
driving lightweight UI updates, animation steps, or regular housekeeping work
in an MDI application.
Example:
Ping ping = new Ping(500); // fire every 500 ms
ping.addPingListener(() -> {
// e.g. repaint a status component
});
// Later, when you no longer need it:
ping.stop(); // or ping.close();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddPingListener(IPing listener) Adds a ping listener that will be notified on each timer firing.voidclose()Stops the timer and releases any resources held by thisPing.intgetDelay()Returns the current delay between timer firings, in milliseconds.booleanReturnstrueif the underlying timer is currently running.voidremovePingListener(IPing listener) Removes a previously registered ping listener.voidsetDelay(int delayInMillis) Sets the delay between timer firings, in milliseconds.voidstart()Starts the underlying timer if it is not already running.voidstop()Stops the underlying timer.
-
Constructor Details
-
Ping
public Ping(int delayInMillis) Creates a newPingthat fires at the specified interval and starts immediately.- Parameters:
delayInMillis- the delay between pings in milliseconds; must be positive- Throws:
IllegalArgumentException- ifdelayInMillis <= 0
-
Ping
public Ping(int delayInMillis, boolean startImmediately) Creates a newPingthat fires at the specified interval.- Parameters:
delayInMillis- the delay between pings in milliseconds; must be positivestartImmediately- iftrue, the timer is started as soon as thePingis created; otherwise it remains stopped untilstart()is called- Throws:
IllegalArgumentException- ifdelayInMillis <= 0
-
-
Method Details
-
start
public void start()Starts the underlying timer if it is not already running.All subsequent ticks will result in calls to
IPing.ping()for each registered listener untilstop()orclose()is called. -
stop
public void stop()Stops the underlying timer. No furtherIPing.ping()callbacks will occur untilstart()is called again. -
isRunning
public boolean isRunning()Returnstrueif the underlying timer is currently running.- Returns:
trueif the timer is running;falseotherwise
-
getDelay
public int getDelay()Returns the current delay between timer firings, in milliseconds.- Returns:
- the delay between pings in milliseconds
-
setDelay
public void setDelay(int delayInMillis) Sets the delay between timer firings, in milliseconds.- Parameters:
delayInMillis- the new delay between pings; must be positive- Throws:
IllegalArgumentException- ifdelayInMillis <= 0
-
addPingListener
Adds a ping listener that will be notified on each timer firing.- Parameters:
listener- the ping listener to add; ignored ifnull
-
removePingListener
Removes a previously registered ping listener.- Parameters:
listener- the ping listener to remove; ignored ifnull
-
close
public void close()Stops the timer and releases any resources held by thisPing.This method simply calls
stop()and is provided so thatPingcan be used with try-with-resources:try (Ping ping = new Ping(250)) { ping.addPingListener(...); // use ping } // timer automatically stopped here- Specified by:
closein interfaceAutoCloseable
-