001package Torello.Java;
002
003import java.nio.file.*;
004import java.io.IOException;
005
006/**
007 * Thrown when a move or copy operation has specified identical <CODE>source</CODE> and
008 * <CODE>target</CODE> places on disk.
009 * 
010 * <EMBED CLASS='external-html' DATA-FILE-ID=SAME_SOURCE_EX>
011 */
012public class SameSourceAndTargetException extends RuntimeException
013{
014    /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUIDEX>  */
015    public static final long serialVersionUID = 1;
016
017    /** Constructs a {@code SameSourceAndTargetException} with no detail message. */
018    public SameSourceAndTargetException()
019    { super(); }
020
021    /**
022     * Constructs a {@code SameSourceAndTargetException} with the specified detail message.
023     * @param message the detail message.
024     */
025    public SameSourceAndTargetException(String message)
026    { super(message); }
027
028    /**
029     * Constructs a new exception with the specified detail message and cause.
030     * 
031     * <BR /><BR /><B CLASS=JDDescLabel>NOTE:</B>
032     * 
033     * <BR /><BR />The detail message associated with cause is not automatically incorporated into
034     * this exception's detail message.
035     * 
036     * @param message The detail message (which is saved for later retrieval by the
037     * {@code Throwable.getMessage()} method).
038     * 
039     * @param cause the cause (which is saved for later retrieval by the {@code Throwable.getCause()} 
040     * method).  (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
041     */
042    public SameSourceAndTargetException(String message, Throwable cause)
043    { super(message, cause); }
044
045    /**
046     * Constructs a new exception with the specified cause and a detail message of
047     * {@code (cause==null ? null : cause.toString())} (which typically contains the class and
048     * detail message of cause).  This constructor is useful for exceptions that are little more
049     * than wrappers for other {@code Throwable's}.
050     * 
051     * @param cause The cause (which is saved for later retrieval by the
052     * {@code Throwable.getCause()} method).  (A null value is permitted, and indicates that the
053     * cause is nonexistent or unknown.)
054     */
055    public SameSourceAndTargetException(Throwable cause)
056    { super(cause); }
057
058    /**
059     * This method can be used to check whether the source and target directories in a program are
060     * actually pointing to the same location on the file-system.  If they are, or if there are any
061     * problems in attempting to ascertain this information, then exceptions will be thrown.
062     * 
063     * @param directory This may be any instance of {@code FileNode} that represents a directory.
064     * 
065     * @param targetDirectory This is a {@code java.lang.String} that represents the target
066     * directory.
067     * 
068     * @throws SameSourceAndTargetException If after obtaining the <B>'Real Path'</B> of both the
069     * {@code source} and the {@code target} directories, both real-path's indicate the same 
070     * location on the file-system, then this exception will throw.
071     * 
072     * @throws InvalidPathException If the <I>Java Virtual Machine</I> is unable to instantiate an
073     * instance of {@code java.nio.files.Path} for either the {@code 'directory'} parameter or the
074     * {@code 'targetDirectoryParameter'}, then this exception will be thrown.
075     * 
076     * @throws NoSuchFileException If, after instantiating an instance of {@code Path} for either
077     * the {@code source} or the {@code target} directories, the <I>Java Virtual Machine</I> is
078     * unable to build an instance of {@code Path} using the method {@code Path.toRealPath()}, then
079     * this exception will throw.
080     * 
081     * @throws IOException If the method {@code 'Path.toRealPath()'} fails with any I/O problems
082     * then ths exception will throw
083     */
084    public static void check(FileNode directory, String targetDirectory)
085        throws IOException, SameSourceAndTargetException, InvalidPathException, NoSuchFileException
086    {
087        String sourceDir = Paths.get(directory.getFullPathName()).toRealPath().toString();
088
089        if (sourceDir.equals(Paths.get(targetDirectory).toRealPath().toString()))
090
091            throw new SameSourceAndTargetException(
092                "The source directory: \n\t" + directory.getFullPathName() + '\n' +
093                "And the target directory: \n\t" + targetDirectory + '\n' +
094                "Have the same real-path: \n\t" + sourceDir
095            );
096    }
097}