Class Task
- java.lang.Object
-
- com.tagtraum.beatunes.analysis.Task
-
- All Implemented Interfaces:
TaskFactory,TaskProcessListener,TaskRunListener,Matchable<PlayList.SubstringFilterProperty>,Cloneable
- Direct Known Subclasses:
AudioAnalysisTask,AudioPumpAnalysisTask,SongAnalysisTask
@Entity public class Task extends Object implements Matchable<PlayList.SubstringFilterProperty>, Cloneable, TaskFactory, TaskRunListener, TaskProcessListener
Provides the infrastructure for tasks that may contain other tasks.
To implement your own custom task, subclass
AudioAnalysisTaskorSongAnalysisTask, depending on whether you want to analyze audio data or just meta data. In any case, make sure you mark the class with theEntityannotation. This is essential - otherwise it does not get persisted and never shows up in the queue.A task needs a
TaskEditor- by default that's a class with the same name, but with "Editor" appended or an empty editor, in case that class is not found. If you want to instantiate a different editor, overridecreateEditor().In order to provide any behavior, you should override
runBefore(Task). This is where you must do the heavy lifting, i.e. analyze audio or similar things.If your task is capable of processing online reference data, also override
TaskProcessListener.processBefore(Task)and callsetUseOnlineResources(boolean)with the argumenttrue.getReferenceSong()will provide available reference data.In either case, you should always signal, whether what you did succeeded or not. You can do so with
setSucceeded(boolean)andsetSkipped(boolean).To register your task with beaTunes you need to add an entry to your
META-INF/plugin.xml.If you need to store any configurations parameters persistently, you may manipulate the persistent property map using
setProperty(String, String). Note that this should only occur at configuration time using aTaskEditorcreated withcreateEditor(). At runtime, you may read values usinggetProperty(String).- Author:
- Hendrik Schreiber
- See Also:
PluginManager,TaskEditor
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanuseOnlineResources
-
Constructor Summary
Constructors Constructor Description Task()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(Task task)voidaddTaskProcessListener(TaskProcessListener taskProcessListener)voidaddTaskRunListener(TaskRunListener taskRunListener)voidcancel()Asynchronously cancels this and all of its subtasks, if possible.voidclearProperties()Delete all properties.TaskcreateDeepCopy()This method creates a copy of the Task and its subtasks.TaskEditor<? extends Task>createEditor()By default instantiates a class with the namethis.getClass().getName() + "Editor".TaskcreateTask()booleandeepRemove(Task task)Deep remove.booleanequals(Object obj)protected voidfetchCollections()Fetches any lazily initialized collections, if necessary.AnalysisProgressgetAnalysisProgress()BeaTunesgetApplication()AudioFileFormatgetAudioFileFormat()OperationgetCurrentOperation()Task[]getDeepLeafs()intgetDeepTaskCount(boolean onlyProgressRelevant)Counts this task and all its sub tasks.Task[]getDeepTasks()All subtasks and their subtasks.StringgetDescription()HTML or plain text description of this task.TaskEditor<? extends Task>getEditor()Creates and initializes aTaskEditorwith this task.longgetId()protected MessagePanelgetMessagePanel()Convenience method for getting the analysis queue message panel.StringgetName()HTML or plaintext name of this task.TaskgetParent()intgetPriority()Map<String,String>getProperties()Used for XML based persistence by beaTunes.StringgetProperty(String propertyName)Returns property.Iterator<String>getPropertyNames()Names of persistent properties.ReferenceSonggetReferenceSong()AudioSonggetSong()LonggetSongId()TaskgetTask(int index)intgetTaskCount()Task[]getTasks()All subtasks without their subtasks.inthashCode()booleanhasSucceeded()Indicates whether processing or running this task succeeded.booleanisAvailable()Indicate, whether this task is actually available in the current configuration.booleanisCancelled()booleanisDeepUseOnlineResources()Indicates whether this task or any of its sub-tasks want to use online resources.booleanisFileExistedBeforeStart()Indicates whether the file (if any) associated with this song (getSong()) and task existed before processing started.booleanisLeaf()booleanisPaused()booleanisProgressRelevant()booleanisUseOnlineResources()Indicates whether this task should use online resources.booleanmatches(Pattern[] patterns, PlayList.SubstringFilterProperty substringFilterProperty)True if this object matches all provided patters (logical AND).protected voidmaybePause()Checks whether the task got paused or cancelled.voidprocess()voidprocessAfter(Task task)Is executed after sub tasks are run.voidprocessBefore(Task task)Process online results with this task before other tasks are invoked.voidprocessFinally(Task task)Is executed after sub tasks are run regardless of whether those sub tasks succeeded or not.booleanremove(Task task)Shallow remove.booleanremoveDeadLeaves()Removes any leaves that have succeeded (hasSucceeded()), were not skipped (!wasSkipped()) and used online resources (useOnlineResources).voidrun()voidrunAfter(Task task)Is executed after sub tasks are run.voidrunBefore(Task task)Is executed before sub tasks are run.voidrunFinally(Task task)Is executed after sub tasks are run regardless of whether those sub tasks succeeded or not.voidsetAnalysisProgress(AnalysisProgress analysisProgress)voidsetApplication(BeaTunes beaTunes)voidsetAudioFileFormat(AudioFileFormat audioFileFormat)protected voidsetAvailable(boolean available)voidsetCurrentOperation(Operation currentOperation)voidsetId(long id)voidsetParent(Task parent)voidsetPaused(boolean paused)Pauses ot resumes this and all of its sub tasks.voidsetPriority(int priority)Subtasks with lower priority are executed first.voidsetProgressRelevant(boolean progressRelevant)voidsetProperties(Map<String,String> properties)Used for XML based persistence by beaTunes.StringsetProperty(String name, String value)Sets persistent property.voidsetReferenceSong(ReferenceSong referenceSong)protected voidsetSkipped(boolean skipped)Indicates whether this task was skipped by eitherrunBefore(Task)orTaskProcessListener.processBefore(Task)(or similar methods).voidsetSong(AudioSong song)Sets the song associated with this task.voidsetSongId(Long id)protected voidsetSucceeded(boolean succeeded)Indicates whether processing or running a task withrunBefore(Task)orTaskProcessListener.processBefore(Task)(or similar methods) succeeded and led to the desired results, implying that it does not need to be processed any further.voidsetUseOnlineResources(boolean useOnlineResources)Determines whether this task should use online resources.booleanskip()Indicates, whether this task can be skipped.StringtoString()booleanwasSkipped()
-
-
-
Method Detail
-
addTaskProcessListener
public void addTaskProcessListener(TaskProcessListener taskProcessListener)
-
addTaskRunListener
public void addTaskRunListener(TaskRunListener taskRunListener)
-
hasSucceeded
public boolean hasSucceeded()
Indicates whether processing or running this task succeeded.- Returns:
- true or false
- See Also:
setSucceeded(boolean)
-
setSucceeded
protected void setSucceeded(boolean succeeded)
Indicates whether processing or running a task withrunBefore(Task)orTaskProcessListener.processBefore(Task)(or similar methods) succeeded and led to the desired results, implying that it does not need to be processed any further. This method must be called by your implementation ofrunBefore(Task)orTaskProcessListener.processBefore(Task). It is not called automatically!- Parameters:
succeeded- true or false- See Also:
setSkipped(boolean),hasSucceeded()
-
wasSkipped
public boolean wasSkipped()
- Returns:
- true or false
- See Also:
setSkipped(boolean)
-
setSkipped
protected void setSkipped(boolean skipped)
Indicates whether this task was skipped by eitherrunBefore(Task)orTaskProcessListener.processBefore(Task)(or similar methods). This method is not called automatically, but must be called by your implementation, whenever you decide to skip processing/running a task.- Parameters:
skipped- skipped- See Also:
skip(),wasSkipped(),setSucceeded(boolean)
-
isAvailable
public boolean isAvailable()
Indicate, whether this task is actually available in the current configuration. A task may not be available for non-iTunes libraries.- Returns:
- true or false
- See Also:
MediaLibrary.getLibraryDescriptor()
-
setAvailable
protected void setAvailable(boolean available)
- Parameters:
available- available- See Also:
isAvailable()
-
getEditor
public TaskEditor<? extends Task> getEditor()
Creates and initializes aTaskEditorwith this task.- Returns:
- initialized editor
- See Also:
TaskEditor.setTask(Task)
-
createEditor
public TaskEditor<? extends Task> createEditor()
By default instantiates a class with the namethis.getClass().getName() + "Editor". If that does not work (because the editor class does not exist), anEmptyTaskEditoris created. To create a meaningful editor for a configurable task, you must override this method.- Returns:
- task editor for this task.
-
isUseOnlineResources
public boolean isUseOnlineResources()
Indicates whether this task should use online resources. This persistent flag is typically set in the editorcreateEditor(). If it is set, itsTaskProcessListener.processBefore(Task)method is called once online results are available.- Returns:
- true or false (defaults to
false) - See Also:
process()
-
setUseOnlineResources
public void setUseOnlineResources(boolean useOnlineResources)
Determines whether this task should use online resources. This flag is persistent.- Parameters:
useOnlineResources- true or false- See Also:
isUseOnlineResources()
-
isDeepUseOnlineResources
public boolean isDeepUseOnlineResources()
Indicates whether this task or any of its sub-tasks want to use online resources.- Returns:
- true or false
-
cancel
public void cancel()
Asynchronously cancels this and all of its subtasks, if possible.
-
getCurrentOperation
public Operation getCurrentOperation()
-
setCurrentOperation
public void setCurrentOperation(Operation currentOperation)
-
isCancelled
public boolean isCancelled()
-
isPaused
public boolean isPaused()
-
setPaused
public void setPaused(boolean paused)
Pauses ot resumes this and all of its sub tasks.- Parameters:
paused- true or false
-
getApplication
public BeaTunes getApplication()
- Returns:
- this task's or its parent's beaTunes instance
-
setApplication
public void setApplication(BeaTunes beaTunes)
-
getAnalysisProgress
public AnalysisProgress getAnalysisProgress()
- Returns:
- this task's or its parent's AnalysisProgress instance
-
setAnalysisProgress
public void setAnalysisProgress(AnalysisProgress analysisProgress)
-
isProgressRelevant
public boolean isProgressRelevant()
- Returns:
- true if this task relevant for overall progress computations.
-
setProgressRelevant
public void setProgressRelevant(boolean progressRelevant)
- Parameters:
progressRelevant- set this to true, if you want this task and its subtasks to be included in overall progress computations.
-
getSong
public AudioSong getSong()
- Returns:
- this task's or its parent's Song instance
-
setSong
public void setSong(AudioSong song)
Sets the song associated with this task. At the same time the persistent id of this song will be registered usingsetSongId(Long).- Parameters:
song- song
-
setReferenceSong
public void setReferenceSong(ReferenceSong referenceSong)
-
getReferenceSong
public ReferenceSong getReferenceSong()
- Returns:
- this task's or its parent's ReferenceSong instance
-
isFileExistedBeforeStart
public boolean isFileExistedBeforeStart()
Indicates whether the file (if any) associated with this song (getSong()) and task existed before processing started.- Returns:
- true or false
-
fetchCollections
protected void fetchCollections()
Fetches any lazily initialized collections, if necessary.
-
getSongId
public Long getSongId()
-
setSongId
public void setSongId(Long id)
-
getAudioFileFormat
public AudioFileFormat getAudioFileFormat()
- Returns:
- this task's or its parent's
AudioFileFormatinstance (if set)
-
setAudioFileFormat
public void setAudioFileFormat(AudioFileFormat audioFileFormat)
-
getParent
public Task getParent()
-
setParent
public void setParent(Task parent)
-
getTask
public Task getTask(int index)
- Parameters:
index- index- Returns:
- sub task
-
getTaskCount
public int getTaskCount()
- Returns:
- number of (sub)tasks contained in this task
-
getDeepTaskCount
public int getDeepTaskCount(boolean onlyProgressRelevant)
Counts this task and all its sub tasks.- Parameters:
onlyProgressRelevant- count only tasks relevant for progress computation- Returns:
- deep count
-
add
public final void add(Task task)
- Parameters:
task- task to add
-
remove
public boolean remove(Task task)
Shallow remove.- Parameters:
task- task to remove- Returns:
- true, if successful
-
deepRemove
public boolean deepRemove(Task task)
Deep remove.- Parameters:
task- task to remove- Returns:
- true, if successful
-
removeDeadLeaves
public boolean removeDeadLeaves()
Removes any leaves that have succeeded (hasSucceeded()), were not skipped (!wasSkipped()) and used online resources (useOnlineResources).- Returns:
- true, if any leaves were removed
-
getTasks
public Task[] getTasks()
All subtasks without their subtasks.- Returns:
- shallow sub tasks
-
getDeepTasks
public Task[] getDeepTasks()
All subtasks and their subtasks.- Returns:
- deep sub tasks
-
getDeepLeafs
public Task[] getDeepLeafs()
- Returns:
- deep walk to get all leafs
-
isLeaf
public boolean isLeaf()
-
process
public void process()
-
processBefore
public void processBefore(Task task)
Process online results with this task before other tasks are invoked. Override this method, if you want to take online resources into account. Note that you will also need to setsetUseOnlineResources(boolean)totrue, if you want this method to be called at all.To avoid having this task processed again by
runBefore(Task), you must callsetSucceeded(boolean)with the argumenttrue. If, for some reason, you could not take care of this task with just the available online resources, you must callsetSucceeded(boolean)with the argumentfalseThis method is executed before sub tasks are run. When this method is called, the methods
getSong()andgetReferenceSong()will return meaningful results.- Specified by:
processBeforein interfaceTaskProcessListener- Parameters:
task- task
-
processAfter
public void processAfter(Task task)
Is executed after sub tasks are run.- Specified by:
processAfterin interfaceTaskProcessListener- Parameters:
task- task- See Also:
TaskProcessListener.processBefore(Task)
-
processFinally
public void processFinally(Task task)
Is executed after sub tasks are run regardless of whether those sub tasks succeeded or not.- Specified by:
processFinallyin interfaceTaskProcessListener- Parameters:
task- task- See Also:
TaskProcessListener.processBefore(Task)
-
run
public void run() throws AnalysisExceptionCallsrunBefore(Task), all the sub task'srun()methods, andrunAfter(Task). ThenrunFinally(Task)is called.- Throws:
AnalysisException- if any of the analysis steps throw an AnalysisException
-
maybePause
protected void maybePause()
Checks whether the task got paused or cancelled.
-
runBefore
public void runBefore(Task task) throws AnalysisException
Is executed before sub tasks are run.- Specified by:
runBeforein interfaceTaskRunListener- Parameters:
task- task- Throws:
AnalysisException- if this analysis step fails
-
runAfter
public void runAfter(Task task) throws AnalysisException
Is executed after sub tasks are run.- Specified by:
runAfterin interfaceTaskRunListener- Parameters:
task- task- Throws:
AnalysisException- if this analysis step fails
-
runFinally
public void runFinally(Task task)
Is executed after sub tasks are run regardless of whether those sub tasks succeeded or not.- Specified by:
runFinallyin interfaceTaskRunListener- Parameters:
task- task
-
matches
public boolean matches(Pattern[] patterns, PlayList.SubstringFilterProperty substringFilterProperty)
Description copied from interface:MatchableTrue if this object matches all provided patters (logical AND).- Specified by:
matchesin interfaceMatchable<PlayList.SubstringFilterProperty>- Parameters:
patterns- patternssubstringFilterProperty- specifies a particular way in which the match should occur- Returns:
- true if this object matches.
-
getId
public long getId()
-
setId
public void setId(long id)
-
skip
public boolean skip()
Indicates, whether this task can be skipped. Defaults to false.Since this task is not necessarily called by beaTunes, but by your own implementation of
runBefore(Task), you should callsetSkipped(boolean)with whatever value you are returning.- Returns:
- true or false
-
getDescription
public String getDescription()
HTML or plain text description of this task.- Returns:
- description
-
getName
public String getName()
HTML or plaintext name of this task. Defaults to the task's classname.- Returns:
- name
-
getPropertyNames
public final Iterator<String> getPropertyNames()
Names of persistent properties.- Returns:
- iterator over the property names
-
getProperty
public String getProperty(String propertyName)
Returns property.- Parameters:
propertyName- name of the property in question- Returns:
- value or null if not found
-
setProperty
public final String setProperty(String name, String value)
Sets persistent property. Properties are not persisted, if this method is called while the task is executed.- Parameters:
name- property namevalue- property value- Returns:
- old property value or null, if none was set
-
clearProperties
public final void clearProperties()
Delete all properties.
-
getProperties
public final Map<String,String> getProperties()
Used for XML based persistence by beaTunes. For programmatic getting of properties, usegetProperty(String)andgetPropertyNames().- Returns:
- unmodifiable properties map or null, if none was set
-
setProperties
public final void setProperties(Map<String,String> properties)
Used for XML based persistence by beaTunes. For programmatic setting of properties, usesetProperty(String, String)andclearProperties().- Parameters:
properties- properties
-
createTask
public final Task createTask()
- Specified by:
createTaskin interfaceTaskFactory- Returns:
- copy
- See Also:
createDeepCopy()
-
createDeepCopy
public Task createDeepCopy()
This method creates a copy of the Task and its subtasks. If you added any members to your subclass you MUST override this method and provide code that copies your members.- Returns:
- copy
-
getPriority
public int getPriority()
- Returns:
- priority
-
setPriority
public void setPriority(int priority)
Subtasks with lower priority are executed first.- Parameters:
priority- priority
-
getMessagePanel
protected MessagePanel getMessagePanel()
Convenience method for getting the analysis queue message panel.- Returns:
- message panel
- See Also:
Message
-
-