package ru.job4j.thread;

/**
 * Created on 06.09.17
 * Calculation class.
 * @author Wamdue
 * @version 1.0
 */
public class CalcSpacesWords {
    /**
     * Text to analise.
     */
    private  String text;

    /**
     * Main constructor.
     * @param text - text to analise.
     */
    public CalcSpacesWords(String text) {
        this.text = text;
    }

    /**
     * Thread to calculate amount of spaces in text.
     */
    private class CalcSpaces implements Runnable {
        private final String text;

        public CalcSpaces(String text) {
            this.text = text;
        }

        @Override
        public void run() {
            int count = text.length() - text.replace(" ", "").length();
            System.out.printf("Spaces: %d;\n", count);
        }
    }

    /**
     * Thread to calculate amount of words in text.
     */
    private class CalcWords implements Runnable {
        private final String text;

        public CalcWords(String text) {
            this.text = text;
        }

        @Override
        public void run() {
            String[] words = text.split(" ");
            System.out.printf("Words: %d;\n", words.length);
        }
    }

    /**
     * Demon class to watch for calculation time.
     */
    private class Demon extends Thread {
        private Thread spaces;
        private Thread words;

        public Demon(Thread spaces, Thread words) {
            this.spaces = spaces;
            this.words = words;
        }
        @Override
        public void run() {
            try {
                sleep(1000);
                if (spaces.isAlive() || words.isAlive()) {
                    spaces.interrupt();
                    words.interrupt();
                }
            } catch (InterruptedException e) {
                System.out.println("Calulation takes to long, interrupting!");
            }
        }
    }

    /**
     * Async calculation spaces and words.
     */
    public void calc() {
        System.out.println("Calculations starts.");

        Thread calcEnd = new Thread(() -> System.out.println("Calculation finished."));

        Thread spaces = new Thread(new CalcSpaces(text));
        Thread words = new Thread(new CalcWords(text));
        Demon demon = new Demon(spaces, words);

        demon.setDaemon(true);
        demon.start();

        spaces.start();
        words.start();
        try {
            spaces.join();
            words.join();
            if (spaces.isAlive() || words.isAlive())
            {
                spaces.interrupt();
                words.interrupt();
                System.out.println("Calculation takes to long time, interrupting.");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        calcEnd.start();
    }

    public static void main(String[] args) {
        new CalcSpacesWords("Hello my dear friend.").calc();
    }
}
@@ -68,16 +68,19 @@
  class.%0A
+ *%0A
  * @auth
@@ -192,17 +192,16 @@
 private 
- 
 String t
@@ -239,16 +239,23 @@
 ructor.%0A
+     *%0A
      * @
@@ -1092,39 +1092,648 @@
     
-String%5B%5D words = text.split(%22 %22
+long beginTime = System.currentTimeMillis();%0A            String%5B%5D words = text.split(%22 %22);%0A            long endTime = System.currentTimeMillis();%0A            System.out.printf(%22Regex time: %25d;%5Cn%22, endTime - beginTime);%0A            int count = 0;%0A            beginTime = System.currentTimeMillis();%0A            for (int i = 0; i %3C text.length(); i++) %7B%0A                char c = text.charAt(i);%0A                if (c == ' ' %7C%7C c == '.' %7C%7C c == '!' %7C%7C c == '?') %7B%0A                    count++;%0A                %7D%0A            %7D%0A            endTime = System.currentTimeMillis();%0A            System.out.printf(%22For time: %25d;%5Cn%22, endTime - beginTime
 );%0A 
@@ -1792,16 +1792,75 @@
 ength);%0A
+            System.out.printf(%22Count word: %25d;%5Cn%22, count);%0A
         
@@ -2157,32 +2157,33 @@
 ords;%0A        %7D%0A
+%0A
         @Overrid
@@ -3182,28 +3182,16 @@
 Alive())
-%0A           
  %7B%0A     

refactor ClacSpacesWords class, task #1016