Created
February 24, 2016 13:55
-
-
Save Viacheslav77/1562288103a89b57c97a to your computer and use it in GitHub Desktop.
Модифицировать монитор так, чтобы он работал на базе связных списков
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Monitor2; | |
| public interface IFileEvents { | |
| void onFileAdded(String path); | |
| void onFileDeleted(String path); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Monitor2; | |
| import java.lang.InterruptedException; | |
| import java.util.ArrayList; | |
| import java.util.Iterator; | |
| import java.util.LinkedList; | |
| import java.util.List; | |
| import java.io.*; | |
| public class Monitor { | |
| private String path; | |
| private int timeout; | |
| private List<String> prev = new LinkedList<String>(); | |
| private List<String> curr = new LinkedList<String>(); | |
| private IFileEvents events; | |
| public Monitor(String path) { | |
| this.path = path; | |
| createArray(prev); | |
| } | |
| public void start() { | |
| while (true) { | |
| createArray(curr); | |
| compareArrays(prev, curr); | |
| prev.clear(); | |
| prev.addAll(curr); | |
| System.out.println("Waiting..."); | |
| try { | |
| Thread.sleep(timeout); | |
| } catch (InterruptedException ex) { | |
| return; | |
| } | |
| } | |
| } | |
| public int getTimeout() { | |
| return timeout; | |
| } | |
| public void setTimeout(int value) { | |
| timeout = value; | |
| } | |
| public IFileEvents getEvents() { | |
| return events; | |
| } | |
| public void setEvents(IFileEvents value) { | |
| events = value; | |
| } | |
| private void doFileAdded(String path) { | |
| if (events != null) | |
| events.onFileAdded(path); | |
| } | |
| private void doFileDeleted(String path) { | |
| if (events != null) | |
| events.onFileDeleted(path); | |
| } | |
| private void compareArrays(List<String> prev2, List<String> curr2) { | |
| Iterator<String> it = prev2.iterator(); | |
| String path; | |
| while (it.hasNext()) { | |
| path = it.next(); | |
| if ( ! curr2.contains(path)) | |
| doFileDeleted(path); | |
| } | |
| it = curr2.iterator(); | |
| while (it.hasNext()) { | |
| path = it.next(); | |
| if ( ! prev2.contains(path)) | |
| doFileAdded(path); | |
| } | |
| } | |
| private void createArray(List<String> curr2) { | |
| try { | |
| File file = new File(path); | |
| File[] list = file.listFiles(); | |
| curr2.clear(); | |
| for (File f : list) { | |
| curr2.add(f.getCanonicalPath()); | |
| } | |
| } catch (IOException ex) { | |
| ex.printStackTrace(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Monitor2; | |
| /*Модифицировать монитор так, чтобы он работал на базе связных списков.*/ | |
| public class MyClass { | |
| private static class MyEvents implements IFileEvents { | |
| public void onFileAdded(String path) { | |
| System.out.println("File added: " + path); | |
| } | |
| public void onFileDeleted(String path) { | |
| System.out.println("File deleted: " + path); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| Monitor m = new Monitor("d:\\1"); | |
| m.setTimeout(2000); | |
| m.setEvents(new MyEvents()); | |
| m.start(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment