package com.amlinv.javasched.engine;

import com.amlinv.javasched.SchedulerProcessExecutionSlip;

/**
 * Created by art on 12/14/14.
 */
public class StandardProcessExecutionSlip implements SchedulerProcessExecutionSlip, ProcessStateListener {
  /**
   * The process is runnable when it has been submitted to a scheduler, until the scheduler determines the process is
   * complete.  More states may be added in the future, such as suspended.
   */
  private boolean runnable;

  /**
   * A started process is one that the scheduler has accepted and became ready to execute.  The process may no longer
   * be runnable, so this only marks a one-time event.
   */
  private boolean started;

  /**
   * A stopped process is one that the scheduler executed to completion (i.e. until it produced no more steps).
   */
  private boolean stopped;

  private Object stateSync = new Object();

  public StandardProcessExecutionSlip () {
    this.runnable = true;
    this.started  = false;
    this.stopped  = false;
  }

  @Override
  public void waitUntilComplete () throws InterruptedException {
    this.waitUntilComplete(0, 0);
  }

  @Override
  public void waitUntilComplete (long timeoutMilli, long timeoutNano) throws InterruptedException {
    long    endMark;
    boolean expired = false;

    if ( ( timeoutMilli != 0 ) || ( timeoutNano != 0 ) ) {
      endMark = System.nanoTime() + ( timeoutNano + ( timeoutMilli * 1000000L ) );
    } else {
      endMark = 0;
    }

    synchronized ( this.stateSync ) {
      while ( ( this.runnable ) && ( ! expired ) ) {
        if ( endMark != 0 ) {
          long now = System.nanoTime();
          if ( now < endMark ) {
            long remaining = endMark - now;
            this.stateSync.wait(remaining / 1000000L, (int) (remaining % 1000000L));
          } else {
            expired = true;
          }
        } else {
          this.stateSync.wait();
        }
      }
    }
  }

  @Override
  public boolean isRunnable () {
    return this.runnable;
  }

  @Override
  public void processStarted () {
    synchronized ( this.stateSync ) {
      this.started  = true;
      this.runnable = true;
      this.stateSync.notifyAll();
    }
  }

  /**
   * Notify when a process has stopped running.
   */
  @Override
  public void processStopped () {
    synchronized ( this.stateSync ) {
      this.stopped  = true;
      this.runnable = false;
      this.stateSync.notifyAll();
    }
  }
}

@@ -171,16 +171,20 @@
 tionSlip
+%0A   
  impleme
@@ -241,16 +241,17 @@
 tener %7B%0A
+%0A
   /**%0A  
@@ -339,16 +339,21 @@
 cheduler
+%0A   *
  determi
@@ -370,21 +370,16 @@
 ocess is
-%0A   *
  complet
@@ -384,68 +384,8 @@
 ete.
-  More states may be added in the future, such as suspended.
 %0A   
@@ -513,24 +513,29 @@
 xecute.  The
+%0A   *
  process may
@@ -544,21 +544,16 @@
 o longer
-%0A   *
  be runn
@@ -733,16 +733,21 @@
 duced no
+%0A   *
  more st
@@ -796,16 +796,22 @@
 private 
+final 
 Object s
@@ -873,17 +873,16 @@
 tionSlip
- 
 () %7B%0A   
@@ -921,17 +921,16 @@
 started 
- 
 = false;
@@ -947,17 +947,16 @@
 stopped 
- 
 = false;
@@ -1004,17 +1004,16 @@
 Complete
- 
 () throw
@@ -1119,17 +1119,16 @@
 Complete
- 
 (long ti
@@ -1196,19 +1196,16 @@
    long 
-   
 endMark;
@@ -1243,19 +1243,17 @@
     if (
- ( 
+(
 timeoutM
@@ -1265,16 +1265,14 @@
 != 0
- 
 ) %7C%7C (
- 
 time
@@ -1283,19 +1283,17 @@
 ano != 0
- ) 
+)
 ) %7B%0A    
@@ -1321,25 +1321,24 @@
 noTime() + (
- 
 timeoutNano 
@@ -1340,17 +1340,16 @@
 Nano + (
- 
 timeoutM
@@ -1363,19 +1363,17 @@
 1000000L
- ) 
+)
 );%0A    %7D
@@ -1416,33 +1416,32 @@
   synchronized (
- 
 this.stateSync )
@@ -1430,33 +1430,32 @@
  (this.stateSync
- 
 ) %7B%0A      while 
@@ -1455,19 +1455,17 @@
  while (
- ( 
+(
 this.run
@@ -1473,28 +1473,23 @@
 able
- 
 ) && (
- ! 
+!
 expired
- ) 
+)
 ) %7B%0A
@@ -1500,17 +1500,16 @@
     if (
- 
 endMark 
@@ -1512,17 +1512,16 @@
 ark != 0
- 
 ) %7B%0A    
@@ -1570,17 +1570,16 @@
     if (
- 
 now %3C en
@@ -1583,17 +1583,16 @@
  endMark
- 
 ) %7B%0A    
@@ -1893,17 +1893,16 @@
 Runnable
- 
 () %7B%0A   
@@ -1969,17 +1969,16 @@
 sStarted
- 
 () %7B%0A   
@@ -1984,33 +1984,32 @@
   synchronized (
- 
 this.stateSync )
@@ -1998,33 +1998,32 @@
  (this.stateSync
- 
 ) %7B%0A      this.s
@@ -2029,17 +2029,16 @@
 started 
- 
 = true;%0A
@@ -2210,17 +2210,16 @@
 sStopped
- 
 () %7B%0A   
@@ -2233,17 +2233,16 @@
 onized (
- 
 this.sta
@@ -2247,17 +2247,16 @@
 tateSync
- 
 ) %7B%0A    
@@ -2270,17 +2270,16 @@
 stopped 
- 
 = true;%0A

Reformat the code and other minot cleanup.