Class ImagingTask

  • All Implemented Interfaces:
    java.io.Serializable

    public abstract class ImagingTask
    extends java.lang.Object
    implements java.io.Serializable
    Encapsulates an unit of image processing. Concrete implementations of this class MUST BE serializable.
    Author:
    Fabrizio Giudici
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      ImagingTask()
      For serialization.
      ImagingTask​(java.lang.String name, int loggingId)
      Creates a new instance of ImagingTask.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void addStatisticsSample​(java.lang.String name, long value)  
      void dispose()
      Subclasses should release all the allocated resources when this method is called.
      boolean equals​(java.lang.Object object)
      See the comment about uniqueId.
      void execute()
      Executes the task.
      protected <T extends Operation>
      T
      execute​(EditableImage image, T operation, java.lang.String operationName)
      Executes an operation adding the elapsed time to the statistics.
      protected EditableImage execute2​(EditableImage image, Operation operation, java.lang.String operationName)
      Executes an operation adding the elapsed time to the statistics.
      protected EditableImage execute2AndDispose​(EditableImage image, Operation operation, java.lang.String operationName)
      The same as execute2(), but the operand image is disposed before returning.
      protected <T extends Operation>
      T
      executeAndDispose​(EditableImage image, T operation, java.lang.String operationName)
      The same as execute(), but the operand image is disposed before returning.
      java.io.Serializable getId()
      Returns the unique id of this task.
      java.lang.String getName()
      Returns the name of this task.
      EditableImage getResult()
      Returns the result of this task.
      Statistics getStatistics()
      Returns the statistics collected by this task.
      java.lang.Throwable getThrowable()
      If the task ended with an error, a Throwable is returned by this method.
      int hashCode()
      See the comment about uniqueId.
      boolean isRemoteExecutionOk()
      If this method return true (the default), this task can be executed remotely.
      void prepare​(ImagingTaskProcessor processor)
      This method is performed in a local context before the task is scheduled to run - that is, this method is performed in a serialized fashion, possibly on a single computing node (depending on the processing engine).
      protected void registerTime​(java.lang.String name, EditableImage image)
      Updates statistics about execution time, about the latest performed operation on the given image.
      protected abstract void run()
      Concrete implementations should provide the task core in this method.
      protected void setResult​(EditableImage result)  
      void setThrowable​(java.lang.Throwable throwable)  
      java.lang.String toString()
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • latestExecutionTime

        public long latestExecutionTime
    • Constructor Detail

      • ImagingTask

        public ImagingTask()
        For serialization.
      • ImagingTask

        public ImagingTask​(java.lang.String name,
                           int loggingId)
        Creates a new instance of ImagingTask. Subclasses must *not* perform any initialization here: they should use prepare() instead. This constructor must return very quickly.
    • Method Detail

      • getName

        public java.lang.String getName()
        Returns the name of this task.
        Returns:
        the task name
      • getId

        public java.io.Serializable getId()
        Returns the unique id of this task.
        Returns:
        the task id
      • prepare

        public void prepare​(ImagingTaskProcessor processor)
                     throws java.lang.Exception
        This method is performed in a local context before the task is scheduled to run - that is, this method is performed in a serialized fashion, possibly on a single computing node (depending on the processing engine). Subclasses can override this method for customizing the behaviour (that must not be computing intensive). The processor parameter can be used to query some properties of the ImagingTaskProcessor that could be used for alternate preparing strategies. The super method must be always called.
        Parameters:
        processor - the processor
        Throws:
        java.lang.Exception
      • run

        protected abstract void run()
                             throws java.lang.Exception
        Concrete implementations should provide the task core in this method. This method is performed in a distributed context, if available.
        Throws:
        java.lang.Exception
      • getResult

        public final EditableImage getResult()
        Returns the result of this task. Must be implemented by subclasses.
        Returns:
        the result
      • setResult

        protected final void setResult​(EditableImage result)
      • getThrowable

        public java.lang.Throwable getThrowable()
        If the task ended with an error, a Throwable is returned by this method.
        Returns:
        the throwable
      • setThrowable

        public void setThrowable​(java.lang.Throwable throwable)
      • isRemoteExecutionOk

        public boolean isRemoteExecutionOk()
        If this method return true (the default), this task can be executed remotely. Otherwise it will be scheduled on the local node. Usually tasks which computation time is very quick and much shorter than the overhead to serialize data back and forth should return false.
        Returns:
        true if the task can be scheduled remotely
      • getStatistics

        public Statistics getStatistics()
        Returns the statistics collected by this task.
        Returns:
        the statistics
      • dispose

        public void dispose()
        Subclasses should release all the allocated resources when this method is called.
      • toString

        public final java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hashCode

        public final int hashCode()
        See the comment about uniqueId.
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public final boolean equals​(java.lang.Object object)
        See the comment about uniqueId.
        Overrides:
        equals in class java.lang.Object
      • execute

        public final void execute()
        Executes the task. This method is only called by the framework.
      • addStatisticsSample

        public void addStatisticsSample​(java.lang.String name,
                                        long value)
      • registerTime

        protected void registerTime​(java.lang.String name,
                                    EditableImage image)
        Updates statistics about execution time, about the latest performed operation on the given image.
        Parameters:
        name - the statistics name
        image - the image
      • execute

        protected <T extends Operation> T execute​(EditableImage image,
                                                  T operation,
                                                  java.lang.String operationName)
        Executes an operation adding the elapsed time to the statistics.
        Parameters:
        image - the image to process
        operation - the operation to execute
        operationName - the name used for the statistics
        Returns:
        the operation (as a convenience in case it carries results)
      • executeAndDispose

        protected <T extends Operation> T executeAndDispose​(EditableImage image,
                                                            T operation,
                                                            java.lang.String operationName)
        The same as execute(), but the operand image is disposed before returning.
        Parameters:
        image - the image to process
        operation - the operation to execute
        operationName - the name used for the statistics
        Returns:
        the operation (as a convenience in case it carries results)
      • execute2

        protected EditableImage execute2​(EditableImage image,
                                         Operation operation,
                                         java.lang.String operationName)
        Executes an operation adding the elapsed time to the statistics.
        Parameters:
        image - the image to process
        operation - the operation to execute
        operationName - the name used for the statistics
        Returns:
        the result
      • execute2AndDispose

        protected EditableImage execute2AndDispose​(EditableImage image,
                                                   Operation operation,
                                                   java.lang.String operationName)
        The same as execute2(), but the operand image is disposed before returning.
        Parameters:
        image - the image to process
        operation - the operation to execute
        operationName - the name used for the statistics
        Returns:
        the result