Class Downloader<T>


  • public abstract class Downloader<T>
    extends java.lang.Object
    Manages downloading of data that only needs to be got once.
    Since:
    13 Jun 2014
    Author:
    Mark Taylor
    • Constructor Summary

      Constructors 
      Constructor Description
      Downloader​(java.lang.Class<T> clazz, java.lang.String dataDescription)
      Constructor.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void addActionListener​(java.awt.event.ActionListener listener)
      Adds a listener that will be notified if the data acquisition status changes.
      abstract T attemptReadData()
      Performs the actual download.
      void clearData()
      Resets the state of this downloader, as if the no download attempt had been made.
      javax.swing.JComponent createMonitorComponent()
      Returns a little component that monitors status of this downloader.
      T getData()
      Immediately returns the downloaded data, or null if it has not been downloaded, or if a download has failed.
      boolean isComplete()
      Indicates whether the data has been downloaded.
      void removeActionListener​(java.awt.event.ActionListener listener)
      Removes a previously added listener.
      T waitForData()
      Downloads the data if necessary, and returns its content.
      • Methods inherited from class java.lang.Object

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

      • Downloader

        public Downloader​(java.lang.Class<T> clazz,
                          java.lang.String dataDescription)
        Constructor.
        Parameters:
        clazz - type of data downloaded
        dataDescription - short description of downloaded data, may be used in logging messages
    • Method Detail

      • attemptReadData

        public abstract T attemptReadData()
                                   throws java.io.IOException
        Performs the actual download. Implementations are encouraged to log query and details of success if applicable at the INFO level, but an error will be logged by the Downloader.
        Returns:
        downloaded data
        Throws:
        java.io.IOException
      • isComplete

        public boolean isComplete()
        Indicates whether the data has been downloaded. If this method returns true, then getData() will return the result.
        Returns:
        true iff download has completed, successfully or otherwise
      • getData

        public T getData()
        Immediately returns the downloaded data, or null if it has not been downloaded, or if a download has failed.
        Returns:
        data
      • clearData

        public void clearData()
        Resets the state of this downloader, as if the no download attempt had been made.
      • waitForData

        public T waitForData()
        Downloads the data if necessary, and returns its content. If a download attempt has already been completed, this will return immediately, otherwise it will block. If the download failed, null will be returned.
        Returns:
        data or null on failure
      • createMonitorComponent

        public javax.swing.JComponent createMonitorComponent()
        Returns a little component that monitors status of this downloader. Currently, it is blank before the download has happened, then turns to green on success or red on failure.
      • addActionListener

        public void addActionListener​(java.awt.event.ActionListener listener)
        Adds a listener that will be notified if the data acquisition status changes.
        Parameters:
        listener - listener
      • removeActionListener

        public void removeActionListener​(java.awt.event.ActionListener listener)
        Removes a previously added listener.
        Parameters:
        listener - listener