Package Torello.Java

Class SameSourceAndTargetException

  • All Implemented Interfaces:
    java.io.Serializable

    public class SameSourceAndTargetException
    extends java.lang.RuntimeException
    Thrown when a move or copy operation has specified identical source and target places on disk.

    This Exception is used to identify scenarios where a user has requested a move operation be performed on either a file, a directory, or a directory-tree where the source and target files (or directories) are identical.

    This can have disastrous implications whereby after an initial Copy Phase, the Source-Code Logic then attempts to perform a Delete Phase whereby the original files are then removed.

    The potential disaster to avoid is where the copy writes out the data to the same location (because of a user error where the provided source and target locations were identical). If the exception is not thrown immediately, when the delete begins, both versions are erased from disk, and the original data is simply wiped / erased completely.

    This exception was initially written for: FileRW.copyFile
    See Also:
    Serialized Form


    • Field Summary

       
      Serializable ID
      Modifier and Type Field
      static long serialVersionUID
    • Constructor Summary

      Constructors 
      Constructor Description
      SameSourceAndTargetException()
      Constructs a SameSourceAndTargetException with no detail message.
      SameSourceAndTargetException​(String message)
      Constructs a SameSourceAndTargetException with the specified detail message.
      SameSourceAndTargetException​(String message, Throwable cause)
      Constructs a new exception with the specified detail message and cause.
      SameSourceAndTargetException​(Throwable cause)
      Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).
    • Method Summary

       
      'static' Exception Check Methods
      Modifier and Type Method
      static void check​(FileNode directory, String targetDirectory)
      • Methods inherited from class java.lang.Throwable

        addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • serialVersionUID

        🡇     🗕  🗗  🗖
        public static final long serialVersionUID
        This fulfils the SerialVersion UID requirement for all classes that implement Java's interface java.io.Serializable. Using the Serializable Implementation offered by java is very easy, and can make saving program state when debugging a lot easier. It can also be used in place of more complicated systems like "hibernate" to store data as well.

        Note that Java's java.lang.Exception and java.lang.Error classes implement the Serializable interface, and a warning-free build expects this field be defined here.
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
         public static final long serialVersionUID = 1;
        
    • Constructor Detail

      • SameSourceAndTargetException

        🡅  🡇     🗕  🗗  🗖
        public SameSourceAndTargetException​(java.lang.String message,
                                            java.lang.Throwable cause)
        Constructs a new exception with the specified detail message and cause.

        NOTE:

        The detail message associated with cause is not automatically incorporated into this exception's detail message.
        Parameters:
        message - The detail message (which is saved for later retrieval by the Throwable.getMessage() method).
        cause - the cause (which is saved for later retrieval by the Throwable.getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
      • SameSourceAndTargetException

        🡅  🡇     🗕  🗗  🗖
        public SameSourceAndTargetException​(java.lang.Throwable cause)
        Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause). This constructor is useful for exceptions that are little more than wrappers for other Throwable's.
        Parameters:
        cause - The cause (which is saved for later retrieval by the Throwable.getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
    • Method Detail

      • check

        🡅     🗕  🗗  🗖
        public static void check​(FileNode directory,
                                 java.lang.String targetDirectory)
                          throws java.io.IOException,
                                 SameSourceAndTargetException,
                                 java.nio.file.InvalidPathException,
                                 java.nio.file.NoSuchFileException
        This method can be used to check whether the source and target directories in a program are actually pointing to the same location on the file-system. If they are, or if there are any problems in attempting to ascertain this information, then exceptions will be thrown.
        Parameters:
        directory - This may be any instance of FileNode that represents a directory.
        targetDirectory - This is a java.lang.String that represents the target directory.
        Throws:
        SameSourceAndTargetException - If after obtaining the 'Real Path' of both the source and the target directories, both real-path's indicate the same location on the file-system, then this exception will throw.
        java.nio.file.InvalidPathException - If the Java Virtual Machine is unable to instantiate an instance of java.nio.files.Path for either the 'directory' parameter or the 'targetDirectoryParameter', then this exception will be thrown.
        java.nio.file.NoSuchFileException - If, after instantiating an instance of Path for either the source or the target directories, the Java Virtual Machine is unable to build an instance of Path using the method Path.toRealPath(), then this exception will throw.
        java.io.IOException - If the method 'Path.toRealPath()' fails with any I/O problems then ths exception will throw
        Code:
        Exact Method Body:
         String sourceDir = Paths.get(directory.getFullPathName()).toRealPath().toString();
        
         if (sourceDir.equals(Paths.get(targetDirectory).toRealPath().toString()))
        
             throw new SameSourceAndTargetException(
                 "The source directory: \n\t" + directory.getFullPathName() + '\n' +
                 "And the target directory: \n\t" + targetDirectory + '\n' +
                 "Have the same real-path: \n\t" + sourceDir
             );