/*
 *                  Aozan development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU General Public License version 3 or later 
 * and CeCILL. This should be distributed with the code. If you 
 * do not have a copy, see:
 *
 *      http://www.gnu.org/licenses/gpl-3.0-standalone.html
 *      http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
 *
 * Copyright for this code is held jointly by the Genomic platform
 * of the Institut de Biologie de l'École Normale Supérieure and
 * the individual authors. These should be listed in @author doc
 * comments.
 *
 * For more information on the Aozan project and its aims,
 * or to join the Aozan Google group, visit the home page at:
 *
 *      http://outils.genomique.biologie.ens.fr/aozan
 *
 */

package fr.ens.biologie.genomique.aozan.collectors;

import static fr.ens.biologie.genomique.kenetre.util.StringUtils.toTimeHumanReadable;

import java.util.concurrent.TimeUnit;

import com.google.common.base.Stopwatch;

import fr.ens.biologie.genomique.aozan.AozanException;
import fr.ens.biologie.genomique.aozan.RunData;
import fr.ens.biologie.genomique.aozan.io.FastqSample;

/**
 * The abstract class define a thread, it calls by AbstractFastqCollector.
 * @since 1.0
 * @author Laurent Jourdren
 * @author Sandrine Perrin
 */
abstract class AbstractFastqProcessThread implements Runnable {

  private final FastqSample fastqSample;
  private final RunData results;

  private Throwable exception;
  private boolean success;

  @Override
  public void run() {

    // Timer
    final Stopwatch timer = Stopwatch.createStarted();

    logThreadStart();

    try {
      // Launch process treatment related to each collector
      process();

      setSuccess(true);

    } catch (final Throwable e) {
      setException(e);
    } finally {

      final String duration =
          toTimeHumanReadable(timer.elapsed(TimeUnit.MILLISECONDS));
      timer.stop();

      logThreadEnd(duration);
    }

  }

  //
  // Abstract methods
  //

  protected abstract void logThreadEnd(final String duration);

  protected abstract void logThreadStart();

  protected abstract void process() throws AozanException;

  //
  // Getters
  //

  /**
   * Get the results of the analysis.
   * @return a RunData object with only the result of the thread
   */
  public RunData getResults() {

    return this.results;
  }

  /**
   * Get the exception generated by the call to processSequences in the run()
   * method.
   * @return a exception object or null if no Exception has been thrown
   */
  public Throwable getException() {

    return this.exception;
  }

  /**
   * Test if the call to run method was a success.
   * @return true if the call to run method was a success
   */
  public boolean isSuccess() {

    return this.success;
  }

  /**
   * Return the fastqSample which represent a sample to treat.
   * @return fastqSample, object which represent a sample to treat
   */
  public FastqSample getFastqSample() {
    return this.fastqSample;
  }

  //
  // Setters
  //

  /**
   * Set the exception if occurs.
   * @param exception the exception
   */
  protected void setException(final Throwable exception) {
    this.exception = exception;
  }

  /**
   * Set if the process has been successful.
   * @param success the success result
   */
  protected void setSuccess(final boolean success) {
    this.success = success;
  }

  //
  // Constructor
  //

  /**
   * Public constructor.
   * @param fastqSample, object which represent a sample to treat
   * @throws AozanException if the fastqSample return none fastq file.
   */
  public AbstractFastqProcessThread(final FastqSample fastqSample)
      throws AozanException {

    // Check if fastqSample is null
    if (fastqSample == null) {
      throw new AozanException("No fastqSample defined");
    }

    this.fastqSample = fastqSample;

    // Check if fastq files exists for this fastqSample
    if (this.fastqSample.getFastqFiles() == null
        || this.fastqSample.getFastqFiles().isEmpty()) {
      throw new AozanException("No fastq file defined");
    }

    this.results = new RunData();
  }
}
