package sudoku.time;

import common.time.AbstractTime;
import common.time.Time;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * <p>Denotes the termination of a Solver thread because a change was made to the puzzle being
 * solved. The SolutionEvent resulting from this change to the puzzle is
 * {@link #techniqueEvent() wrapped} by this Time.</p>
 * @author fiveham
 * @author fiveham
 *
 */
public class ThreadEvent extends AbstractTime {
	
	private final TechniqueEvent techniqueEvent;
	private final String threadName;
	
    /**
     * <p>Constructs a ThreadEvent having the specified {@code parent} and wrapping the specified
     * SolutionEvent.</p>
     * @param parent the ThreadEvent marking the change-making termination of the thread that
     * spawned the thread whose change-making termination is represented by this ThreadEvent
     * @param techniqueEvent the SolutionEvent that terminated the thread to which this ThreadEvent
     * pertains
     */
	public ThreadEvent(ThreadEvent parent, TechniqueEvent techniqueEvent, String threadName) {
		super(parent);
		this.techniqueEvent = techniqueEvent;
		this.threadName = threadName;
		
		if(parent != null){
			parent.addChild(this);
		}
	}
	
    /**
     * <p>Adds the specified Time to this ThreadEvent as a child. This method is synchronized
     * because each child added shall be added from a different thread, in order to minimize the
     * ThreadEvent time-tree. Only once a thread has changed the puzzle and is thus about to
     * terminate does it add a child ThreadEvent to the ThreadEvent that ended that thread's parent
     * thread.</p>
     * @param time the new child Time
     * @return true if this Time's collection of children was changed by this call to this method,
     * false otherwise
     */
	@Override
	public synchronized boolean addChild(Time time){
		return super.addChild(time);
	}
	
    /**
     * <p>Returns the SolutionEvent wrapped by this ThreadEvent.</p>
     * @return the SolutionEvent wrapped by this ThreadEvent
     */
	public TechniqueEvent techniqueEvent(){
		return techniqueEvent;
	}
	
	@Override
	public boolean equals(Object o){
		if(o instanceof ThreadEvent){
			ThreadEvent te = (ThreadEvent) o;
			return techniqueEvent.equals(te.techniqueEvent);
		}
		return false;
	}
	
	@Override
	public String toString(){
		StringBuilder result = new StringBuilder("ThreadEvent terminating ").append(threadName).append(System.lineSeparator());
		
		List<Object> subordinateTimes = new ArrayList<>(children().size()+1);
		subordinateTimes.add(techniqueEvent);
		subordinateTimes.addAll(children());
		for(Object o : subordinateTimes){
			String oString = o.toString();
			for(Scanner s = new Scanner(oString); s.hasNextLine();){
				result.append("  ").append(s.nextLine()).append(System.lineSeparator());
			}
		}
		
		return result.toString();
	}
}

@@ -216,94 +216,25 @@
 use 
-a change was made to the puzzle being%0A * solved. The SolutionEvent resulting from this
+the Solver made a
  cha
@@ -240,27 +240,27 @@
 ange to 
-the
+its
  puzzle 
 is%0A * %7B@
@@ -251,18 +251,20 @@
  puzzle 
-is
+and 
 %0A * %7B@li
@@ -292,49 +292,69 @@
 wrap
-ped%7D by this Time.%3C/p%3E%0A * @author fiveham
+s%7D a TechniqueEvent describing that change to the puzzle.%3C/p%3E
 %0A * 
@@ -373,11 +373,8 @@
 ham%0A
- *%0A
  */%0A
@@ -505,26 +505,22 @@
 Name;%0A%09%0A
-  
   /**%0A
-  
    * %3Cp%3E
@@ -614,20 +614,19 @@
 %0A   
-  * Solution
+* Technique
 Even
@@ -628,26 +628,24 @@
 eEvent.%3C/p%3E%0A
-  
    * @param 
@@ -659,64 +659,25 @@
 the 
-ThreadEvent marking the change-making termination of the
+end of the Solver
  thr
@@ -684,23 +684,16 @@
 ead that
-%0A     *
  spawned
@@ -693,24 +693,31 @@
 spawned the 
+Solver 
 thread whose
@@ -721,71 +721,46 @@
 ose 
-change-making termination is represented by this ThreadEvent%0A  
+end this %0A   * ThreadEvent represents%0A
    *
@@ -782,33 +782,25 @@
 ueEvent the 
-SolutionE
+e
 vent that te
@@ -801,22 +801,24 @@
 hat 
-terminated the
+ended the Solver
  thr
@@ -846,23 +846,16 @@
 eadEvent
-%0A     *
  pertain
@@ -856,18 +856,16 @@
 ertains%0A
-  
    */%0A%09p
@@ -1099,34 +1099,30 @@
 );%0A%09%09%7D%0A%09%7D%0A%09%0A
-  
   /**%0A
-  
    * %3Cp%3EAdds
@@ -1172,17 +1172,29 @@
 a child.
- 
+%3C/p%3E%0A   * %3Cp%3E
 This met
@@ -1212,23 +1212,16 @@
 hronized
-%0A     *
  because
@@ -1274,255 +1274,165 @@
 ent 
-thread, in order to minimize the%0A     * ThreadEvent time-tree. Only once a thread has
+Solver %0A   * thread. A Solver thread will only call this method once it has made a
  change
-d
  t
-he
+o its
  puzzle
- and is thus about to%0A 
+. %0A
    
- 
 * 
+Af
 ter
-minate does it add a child ThreadEvent to the ThreadEvent that ended that thread's parent%0A     * thread
+ that call, that Solver thread dies
 .%3C/p%3E%0A
-  
    *
@@ -1462,26 +1462,24 @@
 ild Time%0A   
- 
- 
 * @return tr
@@ -1563,18 +1563,16 @@
 hod,%0A   
-  
 * false 
@@ -1580,26 +1580,24 @@
 therwise%0A   
- 
- 
 */%0A%09@Overrid
@@ -1684,18 +1684,16 @@
 );%0A%09%7D%0A%09%0A
-  
   /**%0A  
@@ -1693,18 +1693,16 @@
  /**%0A   
-  
 * %3Cp%3ERet
@@ -1706,32 +1706,33 @@
 Returns the 
-Solution
+Technique
 Event wrappe
@@ -1761,18 +1761,16 @@
 %3C/p%3E%0A   
-  
 * @retur
@@ -1779,16 +1779,17 @@
 the 
-Solution
+Technique
 Even
@@ -1818,18 +1818,16 @@
 adEvent%0A
-  
    */%0A%09p

update javadoc