@@ -195,6 +195,25 @@ public List<Path> moveFiles(final Collection<File> files,
195195 return paths ;
196196 }
197197
198+ public List <Path > copyFiles (final Collection <String > fileNames ,
199+ final NameToPath converter , final File destination ,
200+ Precondition <File > precondition ) throws IOException {
201+ if (fileNames == null || destination == null ) {
202+ throw new NullPointerException (
203+ "files and destination cannot be null." );
204+ }
205+ if (!destination .isDirectory ()) {
206+ throw new IllegalArgumentException (
207+ "Destination must be a directory." );
208+ }
209+ List <Path > paths = new LinkedList <>();
210+ for (String file : fileNames ) {
211+ paths .add (_copyFile (destination , precondition ,
212+ converter .resolve (file ).toFile ()));
213+ }
214+ return paths ;
215+ }
216+
198217 /**
199218 *
200219 * @param files
@@ -210,20 +229,38 @@ public List<Path> copyFiles(final Collection<File> files,
210229 throw new NullPointerException (
211230 "files and destination cannot be null." );
212231 }
232+ if (!destination .isDirectory ()) {
233+ throw new IOException ("Destination " + destination .getName ()
234+ + " is not a directory." );
235+ }
213236 if (precondition == null ) {
214237 precondition = new Precondition .EmptyCondition <>();
215238 }
239+ return _copyFiles (files , destination , precondition );
240+ }
241+
242+ private List <Path > _copyFiles (final Collection <File > files ,
243+ final File destination , Precondition <File > precondition )
244+ throws IOException {
216245 List <Path > paths = new LinkedList <>();
217246 for (File file : files ) {
218- if (precondition .isMet (file )) {
219- Path p = destination .toPath ().resolve (file .getName ());
220- Files .copy (file .toPath (), p ,
221- StandardCopyOption .REPLACE_EXISTING );
222- }
247+ Path result = _copyFile (destination , precondition , file );
248+ if (result != null )
249+ paths .add (result );
223250 }
224251 return paths ;
225252 }
226253
254+ private Path _copyFile (final File destination ,
255+ Precondition <File > precondition , File file ) throws IOException {
256+ if (precondition .isMet (file )) {
257+ Path p = destination .toPath ().resolve (file .getName ());
258+ return Files .copy (file .toPath (), p ,
259+ StandardCopyOption .REPLACE_EXISTING );
260+ }
261+ return null ;
262+ }
263+
227264 /**
228265 *
229266 * @param sourceDir
@@ -246,13 +283,13 @@ public Path copyDirectory(File sourceDir, File destination)
246283 public Path copyDirectory (Path source , Path destination ) throws IOException {
247284 if (!Files .exists (source )) {
248285 throw new IOException ("Cannot copy directory tree from source: "
249- + source .toString ()
250- + ". Source directory does not exist." );
286+ + source .toString () + ". Source directory does not exist." );
251287 }
252- if (destination .startsWith (source )) {
253- throw new IllegalArgumentException ("Cannot copy directory structure into subdirectory of itself." );
288+ if (destination .startsWith (source )) {
289+ throw new IllegalArgumentException (
290+ "Cannot copy directory structure into subdirectory of itself." );
254291 }
255- if (!Files .exists (destination )) {
292+ if (!Files .exists (destination )) {
256293 Files .createDirectories (destination );
257294 }
258295 CopyDirVisitor visitor = new CopyDirVisitor (source , destination );
@@ -277,11 +314,10 @@ public boolean isFileType(final File file, final String extension) {
277314 public void emptyDir (final File root ) throws IOException {
278315 emptyDir (root , false );
279316 }
280-
317+
281318 public void emptyDir (final Path root ) throws IOException {
282319 emptyDir (root , false );
283320 }
284-
285321
286322 /**
287323 *
@@ -293,11 +329,10 @@ public void emptyDir(final File root, boolean deleteRoot)
293329 throws IOException {
294330 emptyDir (root .toPath (), deleteRoot );
295331 }
296-
332+
297333 public void emptyDir (final Path root , boolean deleteRoot )
298334 throws IOException {
299- DeleteDirVisitor visitor = new DeleteDirVisitor (root ,
300- deleteRoot );
335+ DeleteDirVisitor visitor = new DeleteDirVisitor (root , deleteRoot );
301336 Files .walkFileTree (root , visitor );
302337 }
303338
0 commit comments