Class Ping

java.lang.Object
edu.cnu.mdi.util.Ping
All Implemented Interfaces:
AutoCloseable

public class Ping extends Object implements 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
    Constructor
    Description
    Ping(int delayInMillis)
    Creates a new Ping that fires at the specified interval and starts immediately.
    Ping(int delayInMillis, boolean startImmediately)
    Creates a new Ping that fires at the specified interval.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a ping listener that will be notified on each timer firing.
    void
    Stops the timer and releases any resources held by this Ping.
    int
    Returns the current delay between timer firings, in milliseconds.
    boolean
    Returns true if the underlying timer is currently running.
    void
    Removes a previously registered ping listener.
    void
    setDelay(int delayInMillis)
    Sets the delay between timer firings, in milliseconds.
    void
    Starts the underlying timer if it is not already running.
    void
    Stops the underlying timer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Ping

      public Ping(int delayInMillis)
      Creates a new Ping that fires at the specified interval and starts immediately.
      Parameters:
      delayInMillis - the delay between pings in milliseconds; must be positive
      Throws:
      IllegalArgumentException - if delayInMillis <= 0
    • Ping

      public Ping(int delayInMillis, boolean startImmediately)
      Creates a new Ping that fires at the specified interval.
      Parameters:
      delayInMillis - the delay between pings in milliseconds; must be positive
      startImmediately - if true, the timer is started as soon as the Ping is created; otherwise it remains stopped until start() is called
      Throws:
      IllegalArgumentException - if delayInMillis <= 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 until stop() or close() is called.

    • stop

      public void stop()
      Stops the underlying timer. No further IPing.ping() callbacks will occur until start() is called again.
    • isRunning

      public boolean isRunning()
      Returns true if the underlying timer is currently running.
      Returns:
      true if the timer is running; false otherwise
    • 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 - if delayInMillis <= 0
    • addPingListener

      public void addPingListener(IPing listener)
      Adds a ping listener that will be notified on each timer firing.
      Parameters:
      listener - the ping listener to add; ignored if null
    • removePingListener

      public void removePingListener(IPing listener)
      Removes a previously registered ping listener.
      Parameters:
      listener - the ping listener to remove; ignored if null
    • close

      public void close()
      Stops the timer and releases any resources held by this Ping.

      This method simply calls stop() and is provided so that Ping can be used with try-with-resources:

      
       try (Ping ping = new Ping(250)) {
           ping.addPingListener(...);
           // use ping
       }
       // timer automatically stopped here
       
      Specified by:
      close in interface AutoCloseable