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 accessing a non existing file.
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 {@code FileStore} resulting in the removal
14   * of a file previously created, which thus becomes missing. Mandatory property
15   * {@link #getFilename()} provides the name of the missing file.
16   * </p>
17   * <p>
18   * Note: a specific exception has been introduced as the existing
19   * {@code java.io.FileNotFoundException} and {@code java.nio.file.NoSuchFileException} are
20   * strictly related to JDK classes and the access to files on an OS-managed filesystem.
21   * </p>
22   */
23  public class FileMissingException extends IOException {
24  
25      private static final long serialVersionUID = -6196913035982664339L;
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 missing 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 missing file
37       *
38       * @see #FileMissingException(String, String, Throwable)
39       */
40      public FileMissingException(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 cause specified.
46       *
47       * @param filename
48       *            the filename identifying the missing 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 missing file
52       * @param cause
53       *            an optional cause of this exception
54       */
55      public FileMissingException(final String filename, @Nullable final String message,
56              @Nullable final Throwable cause) {
57          super("File " + filename + " does not exist." + (message == null ? "" : " " + message),
58                  cause);
59  
60          Preconditions.checkNotNull(filename);
61          this.filename = filename;
62      }
63  
64      /**
65       * Returns the filename identifying the missing file.
66       *
67       * @return the filename
68       */
69      public final String getFilename() {
70          return this.filename;
71      }
72  
73  }