• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.commons.io.monitor;
18 
19 import java.io.File;
20 import java.io.Serializable;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 
24 /**
25  * {@link FileAlterationListener} implementation that adds created, changed and deleted
26  * files/directories to a set of {@link Collection}s.
27  */
28 public class CollectionFileListener implements FileAlterationListener, Serializable {
29 
30     private static final long serialVersionUID = 939724715678693963L;
31     private final boolean clearOnStart;
32     private final Collection<File> createdFiles = new ArrayList<>();
33     private final Collection<File> changedFiles = new ArrayList<>();
34     private final Collection<File> deletedFiles = new ArrayList<>();
35     private final Collection<File> createdDirectories = new ArrayList<>();
36     private final Collection<File> changedDirectories = new ArrayList<>();
37     private final Collection<File> deletedDirectories = new ArrayList<>();
38 
39     /**
40      * Create a new observer.
41      *
42      * @param clearOnStart true if clear() should be called by onStart().
43      */
CollectionFileListener(final boolean clearOnStart)44     public CollectionFileListener(final boolean clearOnStart) {
45         this.clearOnStart = clearOnStart;
46     }
47 
48     /**
49      * Clear file collections.
50      */
clear()51     public void clear() {
52         createdFiles.clear();
53         changedFiles.clear();
54         deletedFiles.clear();
55         createdDirectories.clear();
56         changedDirectories.clear();
57         deletedDirectories.clear();
58     }
59 
60     /**
61      * Return the set of changed directories.
62      *
63      * @return Directories which have changed
64      */
getChangedDirectories()65     public Collection<File> getChangedDirectories() {
66         return changedDirectories;
67     }
68 
69     /**
70      * Return the set of changed files.
71      *
72      * @return Files which have changed
73      */
getChangedFiles()74     public Collection<File> getChangedFiles() {
75         return changedFiles;
76     }
77 
78     /**
79      * Return the set of created directories.
80      *
81      * @return Directories which have been created
82      */
getCreatedDirectories()83     public Collection<File> getCreatedDirectories() {
84         return createdDirectories;
85     }
86 
87     /**
88      * Return the set of created files.
89      *
90      * @return Files which have been created
91      */
getCreatedFiles()92     public Collection<File> getCreatedFiles() {
93         return createdFiles;
94     }
95 
96     /**
97      * Return the set of deleted directories.
98      *
99      * @return Directories which been deleted
100      */
getDeletedDirectories()101     public Collection<File> getDeletedDirectories() {
102         return deletedDirectories;
103     }
104 
105     /**
106      * Return the set of deleted files.
107      *
108      * @return Files which been deleted
109      */
getDeletedFiles()110     public Collection<File> getDeletedFiles() {
111         return deletedFiles;
112     }
113 
114     /**
115      * Directory changed Event.
116      *
117      * @param directory The directory changed
118      */
119     @Override
onDirectoryChange(final File directory)120     public void onDirectoryChange(final File directory) {
121         changedDirectories.add(directory);
122     }
123 
124     /**
125      * Directory created Event.
126      *
127      * @param directory The directory created
128      */
129     @Override
onDirectoryCreate(final File directory)130     public void onDirectoryCreate(final File directory) {
131         createdDirectories.add(directory);
132     }
133 
134     /**
135      * Directory deleted Event.
136      *
137      * @param directory The directory deleted
138      */
139     @Override
onDirectoryDelete(final File directory)140     public void onDirectoryDelete(final File directory) {
141         deletedDirectories.add(directory);
142     }
143 
144     /**
145      * File changed Event.
146      *
147      * @param file The file changed
148      */
149     @Override
onFileChange(final File file)150     public void onFileChange(final File file) {
151         changedFiles.add(file);
152     }
153 
154     /**
155      * File created Event.
156      *
157      * @param file The file created
158      */
159     @Override
onFileCreate(final File file)160     public void onFileCreate(final File file) {
161         createdFiles.add(file);
162     }
163 
164     /**
165      * File deleted Event.
166      *
167      * @param file The file deleted
168      */
169     @Override
onFileDelete(final File file)170     public void onFileDelete(final File file) {
171         deletedFiles.add(file);
172     }
173 
174     /**
175      * File system observer started checking event.
176      *
177      * @param observer The file system observer
178      */
179     @Override
onStart(final FileAlterationObserver observer)180     public void onStart(final FileAlterationObserver observer) {
181         if (clearOnStart) {
182             clear();
183         }
184     }
185 
186     /**
187      * File system observer finished checking event.
188      *
189      * @param observer The file system observer
190      */
191     @Override
onStop(final FileAlterationObserver observer)192     public void onStop(final FileAlterationObserver observer) {
193         // noop
194     }
195 
196 }
197