Mercurial > jhg
comparison src/org/tmatesoft/hg/core/HgCallbackTargetException.java @ 215:41a778e3fd31
Issue 5: Facilities for progress and cancellation. More specific exceptions. Exceptions from callbacks as RuntimeException
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Tue, 17 May 2011 00:56:54 +0200 |
| parents | |
| children | c251bbc979cf |
comparison
equal
deleted
inserted
replaced
| 214:4252faa556cd | 215:41a778e3fd31 |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2011 TMate Software Ltd | |
| 3 * | |
| 4 * This program is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 6 * the Free Software Foundation; version 2 of the License. | |
| 7 * | |
| 8 * This program is distributed in the hope that it will be useful, | |
| 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11 * GNU General Public License for more details. | |
| 12 * | |
| 13 * For information on how to redistribute this software under | |
| 14 * the terms of a license other than GNU General Public License | |
| 15 * contact TMate Software at support@hg4j.com | |
| 16 */ | |
| 17 package org.tmatesoft.hg.core; | |
| 18 | |
| 19 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; | |
| 20 | |
| 21 import org.tmatesoft.hg.repo.HgRepository; | |
| 22 import org.tmatesoft.hg.util.Path; | |
| 23 | |
| 24 /** | |
| 25 * Checked exception that indicates errors in client code and tries to supply extra information about the context it occured in. | |
| 26 * | |
| 27 * @author Artem Tikhomirov | |
| 28 * @author TMate Software Ltd. | |
| 29 */ | |
| 30 @SuppressWarnings("serial") | |
| 31 public class HgCallbackTargetException extends HgException { | |
| 32 private int revNumber = BAD_REVISION; | |
| 33 private Nodeid revision; | |
| 34 private Path filename; | |
| 35 | |
| 36 /** | |
| 37 * @param cause can't be <code>null</code> | |
| 38 */ | |
| 39 public HgCallbackTargetException(Throwable cause) { | |
| 40 super((Throwable) null); | |
| 41 if (cause == null) { | |
| 42 throw new IllegalArgumentException(); | |
| 43 } | |
| 44 if (cause.getClass() == Wrap.class) { | |
| 45 // eliminate wrapper | |
| 46 initCause(cause.getCause()); | |
| 47 } else { | |
| 48 initCause(cause); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 /** | |
| 53 * @return not {@link HgRepository#BAD_REVISION} only when local revision number was supplied at the construction time | |
| 54 */ | |
| 55 public int getRevisionNumber() { | |
| 56 return revNumber; | |
| 57 } | |
| 58 | |
| 59 public HgCallbackTargetException setRevisionNumber(int rev) { | |
| 60 revNumber = rev; | |
| 61 return this; | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * @return non-null only when revision was supplied at construction time | |
| 66 */ | |
| 67 public Nodeid getRevision() { | |
| 68 return revision; | |
| 69 } | |
| 70 | |
| 71 public HgCallbackTargetException setRevision(Nodeid r) { | |
| 72 revision = r; | |
| 73 return this; | |
| 74 } | |
| 75 | |
| 76 /** | |
| 77 * @return non-null only if file name was set at construction time | |
| 78 */ | |
| 79 public Path getFileName() { | |
| 80 return filename; | |
| 81 } | |
| 82 | |
| 83 public HgCallbackTargetException setFileName(Path name) { | |
| 84 filename = name; | |
| 85 return this; | |
| 86 } | |
| 87 | |
| 88 @SuppressWarnings("unchecked") | |
| 89 public <T extends Exception> T getTargetException() { | |
| 90 return (T) getCause(); | |
| 91 } | |
| 92 | |
| 93 /** | |
| 94 * Despite this exception is merely a way to give users access to their own exceptions, it may still supply | |
| 95 * valuable debugging information about what led to the error. | |
| 96 */ | |
| 97 @Override | |
| 98 public String getMessage() { | |
| 99 StringBuilder sb = new StringBuilder(); | |
| 100 if (filename != null) { | |
| 101 sb.append(filename); | |
| 102 sb.append(':'); | |
| 103 sb.append(' '); | |
| 104 } | |
| 105 if (revNumber != BAD_REVISION) { | |
| 106 sb.append(revNumber); | |
| 107 if (revision != null) { | |
| 108 sb.append(':'); | |
| 109 } | |
| 110 } | |
| 111 if (revision != null) { | |
| 112 sb.append(revision.shortNotation()); | |
| 113 } | |
| 114 return sb.toString(); | |
| 115 } | |
| 116 | |
| 117 /** | |
| 118 * Given the approach high-level handlers throw RuntimeExceptions to indicate errors, and | |
| 119 * a need to throw reasonable checked exception from client code, clients may utilize this class | |
| 120 * to get their checked exceptions unwrapped by {@link HgCallbackTargetException} and serve as that | |
| 121 * exception cause, eliminating {@link RuntimeException} mediator. | |
| 122 */ | |
| 123 public static final class Wrap extends RuntimeException { | |
| 124 | |
| 125 public Wrap(Throwable cause) { | |
| 126 super(cause); | |
| 127 if (cause == null) { | |
| 128 throw new IllegalArgumentException(); | |
| 129 } | |
| 130 } | |
| 131 } | |
| 132 } |
