package ru.agolovin;

/**
 * @author agolovin (agolovin@list.ru)
 * @version $Id$
 * @since 0.1
 */

public class StringParse {

    /**
     * Incoming string.
     */
    private String str;

    /**
     * Constructor.
     *
     * @param str String.
     */
    StringParse(String str) {
        this.str = str;
    }

    /**
     * Main method.
     *
     * @param args String array
     */
    public static void main(String[] args) {
        String str = "word word2      word3";
        System.out.println("Start");
        System.out.println(String.format("Incoming string: %s", str));
        StringParse stringParse = new StringParse(str);
        Thread threadOne = new Thread(stringParse.new CountWords());
        Thread threadTwo = new Thread(stringParse.new CountWhiteSpace());
        long startTime = System.currentTimeMillis();
        threadOne.start();
        try {
            threadOne.join(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        threadTwo.start();
        try {
            threadTwo.join(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        while (threadOne.isAlive() || threadTwo.isAlive()) {
            if ((threadOne.isAlive() || threadTwo.isAlive()) && (startTime - System.currentTimeMillis() > 1000)) {
                if (threadOne.isAlive()) {
                    stopThread(threadOne);
                }

                if (threadTwo.isAlive()) {
                    stopThread(threadTwo);
                }
            }
        }
        System.out.println("Finish");
    }

    /**
     * @param thread Thread
     */
    private static void stopThread(Thread thread) {
        thread.interrupt();
        if (thread.isInterrupted()) {
            try {
                throw new InterruptedException();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Show whiteSpaces count in the String.
     */
    public class CountWhiteSpace implements Runnable {
        @Override
        public void run() {
            int count = str.replaceAll("\\S+", "").toCharArray().length;
            System.out.println(String.format("Whitespace count: %s", count));
        }

    }

    /**
     * Show word count in the String.
     */
    public class CountWords implements Runnable {
        @Override
        public void run() {
            int count = str.split("\\s+").length;
            System.out.println(String.format("Words count: %s", count));
        }
    }

}

@@ -2153,56 +2153,192 @@
 t = 
-str.replaceAll(%22%5C%5CS+%22, %22%22).toCharArray().length;
+0;%0A            char%5B%5D arr = str.toCharArray();%0A            for (char element : arr) %7B%0A                if (element == ' ') %7B%0A                    count++;%0A                %7D%0A            %7D
 %0A   
@@ -2418,25 +2418,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A    /
@@ -2595,45 +2595,372 @@
     
-int count = str.split(%22%5C%5Cs+%22).length;
+boolean flag = false;%0A            int count = 0;%0A            char%5B%5D arr = str.toCharArray();%0A            for (char element : arr) %7B%0A                if ((element != ' ') && !flag) %7B%0A                    flag = true;%0A                    count++;%0A                %7D%0A                if (element == ' ') %7B%0A                    flag = false;%0A                %7D%0A            %7D
 %0A   

add task1 in part1 chapter7 package2