Search code examples
javadesign-patternsnio

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).


Solution

  • 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.