package ru.szhernovoy.findtext;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by admin on 15.10.2016.
 */
public class FindText {

    private AtomicBoolean success = new AtomicBoolean(false);
    private boolean continueSearch = true;
    private Set<File> listFiles = new ConcurrentSkipListSet<>();
    private Set<Thread> threads = new ConcurrentSkipListSet<>();

    private final String REGEX;

    public  FindText(final boolean continueSearch,final String text){
        this.continueSearch = continueSearch;
        this.REGEX = text;
    }


    public AtomicBoolean getSuccess(){

        return this.success;
    }


    public synchronized void setSuccess(boolean success){
        this.success.set(success);
    }

    public void init(ListFiles find) throws InterruptedException {
           find = new ListFiles(this.listFiles);
           find.startThread();
    }



    public void startThread(ListFiles find) throws InterruptedException {




       // while()
        Thread.sleep(10000);
    }

    private class SnifferName extends Thread{

        private final List<File> chunk;

        public boolean find(String name){
            Matcher m = Pattern.compile(REGEX).matcher(name);
            return m.find();
        }

        public void stopThread(){

            //for (Thread thread : this.threads)



            Iterator<Thread> iter = threads.iterator();
            while(iter.hasNext()){
                iter.next().interrupt();
            }
        }


        public SnifferName(final List<File> part){
            this.chunk = part;
        }

        @Override
        public void run() {

            Iterator<File> iter = this.chunk.iterator();
            while(!Thread.currentThread().isInterrupted() && iter.hasNext()){
                  if(!getSuccess().get()){
                      File next = iter.next();

                      if(this.find(next.getName())){
                          setSuccess(true);
                          if(!continueSearch){
                              this.stopThread();
                          }
                          System.out.println(String.format("Success the search. File is  in %s",next.getAbsolutePath()));
                      }
                  }


            }
            threads.remove(Thread.currentThread());
        }
    }


}

@@ -68,91 +68,11 @@
 til.
-ArrayList;%0Aimport java.util.Iterator;%0A%0Aimport java.util.List;%0Aimport java.util.Set;
+*;%0A
 %0Aimp
@@ -493,39 +493,8 @@
 les 
-= new ConcurrentSkipListSet%3C%3E()
 ;%0A  
@@ -657,16 +657,39 @@
 ing text
+, final Set%3CFile%3E files
 )%7B%0A     
@@ -756,24 +756,55 @@
 = text;%0A
+        this.listFiles = files;%0A
     %7D%0A%0A
-%0A
     publ
@@ -830,25 +830,24 @@
 tSuccess()%7B%0A
-%0A
         retu
@@ -866,25 +866,24 @@
 ess;%0A    %7D%0A%0A
-%0A
     public s
@@ -990,27 +990,20 @@
 oid 
-init(ListFiles find
+startThread(
 ) th
@@ -1030,31 +1030,42 @@
 ption %7B%0A
-    
+%0A
        
-find
+ListFiles searcher
  = new L
@@ -1073,21 +1073,16 @@
 stFiles(
-this.
 listFile
@@ -1096,16 +1096,16 @@
     
-    find
+searcher
 .sta
@@ -1124,137 +1124,694 @@
     
-%7D%0A%0A%0A%0A    public void startThread(
+   Thread.sleep(1000);%0A       Iterator%3CFile%3E iter = this.listFiles.iterator();%0A%0A       int counter = 0;%0A       
 List
+%3C
 File
-s find) throws InterruptedException %7B%0A%0A%0A%0A%0A       // while()%0A        Thread.sleep(10000);
+%3E next = new LinkedList%3C%3E();%0A       while(iter.hasNext() )%7B%0A            if(this.success.get())%7B%0A                if(!this.continueSearch)%7B%0A                    break;%0A                %7D%0A            %7D%0A            next.add(iter.next());%0A            if(counter == 2000)%7B%0A                counter=0;%0A                Thread nextThread = new SnifferName(next,searcher);%0A                nextThread.start();%0A                this.threads.add(nextThread);%0A                next = new LinkedList%3C%3E();%0A            %7D%0A            else%7B%0A                counter++;%0A            %7D%0A       %7D
 %0A   
@@ -1900,16 +1900,55 @@
 %3E chunk;
+%0A        private final ListFiles files;
 %0A%0A      
@@ -2119,17 +2119,16 @@
 read()%7B%0A
-%0A
         
@@ -2135,47 +2135,34 @@
     
-//for (Thread thread : this.threads)%0A%0A%0A
+this.files.interruptAll();
 %0A   
@@ -2307,33 +2307,32 @@
    %7D%0A        %7D%0A%0A
-%0A
         public S
@@ -2363,16 +2363,38 @@
 le%3E part
+, final ListFiles file
 )%7B%0A     
@@ -2419,16 +2419,47 @@
 = part;%0A
+            this.files = file;%0A
         
@@ -2507,17 +2507,16 @@
 run() %7B%0A
-%0A
         
@@ -2728,25 +2728,24 @@
 ter.next();%0A
-%0A
             
@@ -3021,16 +3021,19 @@
 Success 
+in 
 the sear
@@ -3122,18 +3122,16 @@
       %7D%0A
-%0A%0A
         
@@ -3205,11 +3205,63 @@
 %0A    %7D%0A%0A
+    public static void main(String%5B%5D args) %7B%0A%0A    %7D%0A
 %0A%7D%0A

update lesson 3 (find )