1   package eu.fbk.knowledgestore.filestore;
2   
3   import java.io.IOException;
4   
5   import javax.annotation.Nullable;
6   
7   import com.google.common.base.Preconditions;
8   
9   /**
10   * Signals an attempt at creating a file that already exists.
11   * <p>
12   * This exception may denote either an error of the caller (in case it previously created the
13   * specified file) or an external modification to the {@link FileStore} resulting in the external
14   * creation of a file with the same filename. Mandatory property {@link #getFilename()} provides
15   * the name of the existing file.
16   * </p>
17   * <p>
18   * Note: a specific exception has been introduced as the existing
19   * {@code java.nio.file.FileAlreadyExistsException} is strictly related to the JDK FileSystem
20   * class and to the access to files on OS-managed filesystems.
21   * </p>
22   */
23  public class FileExistsException extends IOException {
24  
25      private static final long serialVersionUID = 4370301343958349711L;
26  
27      private final String filename;
28  
29      /**
30       * Creates a new instance with the filename and additional error message specified.
31       * 
32       * @param filename
33       *            the filename identifying the existing file
34       * @param message
35       *            an optional message providing additional information, which is concatenated to
36       *            an auto-generated message reporting the filename of the existing file
37       * 
38       * @see #FileExistsException(String, String, Throwable)
39       */
40      public FileExistsException(final String filename, @Nullable final String message) {
41          this(filename, message, null);
42      }
43  
44      /**
45       * Creates a new instance with the filename, additional error message and cuase specified.
46       * 
47       * @param filename
48       *            the filename identifying the existing file
49       * @param message
50       *            an optional message providing additional information, which is concatenated to
51       *            an auto-generated message reporting the filename of the existing file
52       * @param cause
53       *            an optional cause of this exception
54       */
55      public FileExistsException(final String filename, @Nullable final String message,
56              @Nullable final Throwable cause) {
57  
58          super("File " + filename + " already exists." + (message == null ? "" : " " + message),
59                  cause);
60  
61          Preconditions.checkNotNull(filename);
62          this.filename = filename;
63      }
64  
65      /**
66       * Returns the filename identifying the existing file.
67       * 
68       * @return the filename
69       */
70      public final String getFilename() {
71          return this.filename;
72      }
73  
74  }