# Class Downloader<T>

java.lang.Object
uk.ac.starlink.util.gui.Downloader<T>

public abstract class Downloader<T> extends 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(Class<T> clazz, String dataDescription)`
Constructor.

## Method Summary

Modifier and Type
Method
Description
`void`
`addActionListener(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 no download attempt had been made.
`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(ActionListener listener)`
Removes a previously added listener.
`void`
`setData(T value)`
Sets the state of this downloader as if it had just downloaded the given data item.
`T`
`waitForData()`
Downloads the data if necessary, and returns its content.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### Downloader

public Downloader(Class<T> clazz, String dataDescription)
Constructor.

Parameters:
  `clazz` - type of data downloaded
  `dataDescription` - short description of downloaded data, may be used in logging messages

## Method Details

### attemptReadData

public abstract T attemptReadData() throws 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:
  `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 no download attempt had been made.

### setData

public void setData(T value)
Sets the state of this downloader as if it had just downloaded the given data item. This can be necessary to restore its state, since it affects the monitor component.

Parameters:
  `value` - value that would have been downloaded

### 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 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(ActionListener listener)
Adds a listener that will be notified if the data acquisition status changes.

Parameters:
  `listener` - listener

### removeActionListener

public void removeActionListener(ActionListener listener)
Removes a previously added listener.

Parameters:
  `listener` - listener