Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the goal of the java.nio.file.CopyOption interface?

I'm confused about the design of JDK7 nio package (which I use often). Take for example Files.copy, which takes instances of CopyOption's which is an empty interface, e.g. implemented by StandardCopyOption:

public enum StandardCopyOption implements CopyOption {
    REPLACE_EXISTING
    COPY_ATTRIBUTES,
    ATOMIC_MOVE;
}

public interface CopyOption {
}

What is the idea behind such a design? I mean, even though the CopyOption interfaces is passed to Files.copy, Files.copy has still a compile-time dependency on StandardCopyOption (see source-code of Files.copy).

like image 295
Raphael Roth Avatar asked Dec 01 '25 09:12

Raphael Roth


1 Answers

Notice there are two different enums that implement CopyOption: StandardCopyOption and LinkOption. Since the semantics of the two overlap (LinkOption applies to links, but also during copy operations), having a superinterface allows the API for copy() to be cleaner since it takes a varargs list of CopyOption, which can contain instances of either enum.

like image 193
Jim Garrison Avatar answered Dec 03 '25 22:12

Jim Garrison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!