1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | package Torello.Java; import java.nio.file.*; import java.io.IOException; /** * Thrown when a move or copy operation has specified identical <CODE>source</CODE> and * <CODE>target</CODE> places on disk. * * <EMBED CLASS='external-html' DATA-FILE-ID=SAME_SOURCE_EX> */ public class SameSourceAndTargetException extends RuntimeException { /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUIDEX> */ public static final long serialVersionUID = 1; /** Constructs a {@code SameSourceAndTargetException} with no detail message. */ public SameSourceAndTargetException() { super(); } /** * Constructs a {@code SameSourceAndTargetException} with the specified detail message. * @param message the detail message. */ public SameSourceAndTargetException(String message) { super(message); } /** * Constructs a new exception with the specified detail message and cause. * * <BR /><BR /><B CLASS=JDDescLabel>NOTE:</B> * * <BR /><BR />The detail message associated with cause is not automatically incorporated into * this exception's detail message. * * @param message The detail message (which is saved for later retrieval by the * {@code Throwable.getMessage()} method). * * @param cause the cause (which is saved for later retrieval by the {@code Throwable.getCause()} * method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.) */ public SameSourceAndTargetException(String message, Throwable cause) { super(message, cause); } /** * Constructs a new exception with the specified cause and a detail message of * {@code (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 {@code Throwable's}. * * @param cause The cause (which is saved for later retrieval by the * {@code Throwable.getCause()} method). (A null value is permitted, and indicates that the * cause is nonexistent or unknown.) */ public SameSourceAndTargetException(Throwable cause) { super(cause); } /** * 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. * * @param directory This may be any instance of {@code FileNode} that represents a directory. * * @param targetDirectory This is a {@code java.lang.String} that represents the target * directory. * * @throws SameSourceAndTargetException If after obtaining the <B>'Real Path'</B> of both the * {@code source} and the {@code target} directories, both real-path's indicate the same * location on the file-system, then this exception will throw. * * @throws InvalidPathException If the <I>Java Virtual Machine</I> is unable to instantiate an * instance of {@code java.nio.files.Path} for either the {@code 'directory'} parameter or the * {@code 'targetDirectoryParameter'}, then this exception will be thrown. * * @throws NoSuchFileException If, after instantiating an instance of {@code Path} for either * the {@code source} or the {@code target} directories, the <I>Java Virtual Machine</I> is * unable to build an instance of {@code Path} using the method {@code Path.toRealPath()}, then * this exception will throw. * * @throws IOException If the method {@code 'Path.toRealPath()'} fails with any I/O problems * then ths exception will throw */ public static void check(FileNode directory, String targetDirectory) throws IOException, SameSourceAndTargetException, InvalidPathException, NoSuchFileException { 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 ); } } |