/*
** Module   : FailureNotification.java
** Abstract : helper for delegating failure reporting
**
** Copyright (c) 2009-2016, Golden Code Development Corporation.
**
** -#- -I- --Date-- -----------------------Description------------------------
** 001 GES 20090113 Helper for delegating failure reporting.
** 002 GES 20161017 Made all usage thread-safe.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness;

/**
 * Helper for delegating failure reporting.
 */
public class FailureNotification
{
   /** Reported problem or <code>null</code> if there was no problem. */
   private ExitCode rc = null;
   
   /** Flag to detect when the activity is complete. */
   private boolean complete = false;
   
   /** Specific details of the failure (if any). */
   private String details = null;
   
   /**
    * Detect if the final result is known.
    *
    * @return   <code>true</code> if the activity is complete.
    */
   public synchronized boolean isComplete()
   {
      return complete;
   }
   
   /**
    * Detect if a failure occurred.
    *
    * @return   <code>true</code> if the activity is complete and there a
    *           failure occurred.
    */
   public synchronized boolean isFailed()
   {
      return (complete && rc != null && rc != ExitCode.PASSED);
   }
   
   /**
    * Detect if the activity passed.
    *
    * @return   <code>true</code> if the activity is complete and everything
    *           completed successfully.
    */
   public synchronized boolean isPassed()
   {
      return (complete && (rc == null || rc == ExitCode.PASSED));
   }
   
   /**
    * Get the result of the activity.
    *
    * @return   The result or <code>null</code> if the activity finished
    *           without failure.
    */
   public synchronized ExitCode getResult()
   {
      return rc;
   }
   
   /**
    * Set the result of the activity.
    *
    * @param    rc
    *           The result or <code>null</code> if the activity finished
    *           without failure.
    */
   public synchronized void setResult(ExitCode rc)
   {
      this.rc       = rc;
      this.complete = true;
   }
   
   /**
    * Get the details of the failure.
    *
    * @return   Specific details of the failure or <code>null</code> if the
    *           activity finished without failure.
    */
   public synchronized String getDetails()
   {
      return details;
   }
   
   /**
    * Set the details of the failure.
    *
    * @param    details
    *           Specific details of the failure or <code>null</code> if the
    *           activity finished without failure.
    */
   public synchronized void setDetails(String details)
   {
      this.details = details;
   }
}
