comparison src/org/tmatesoft/hg/core/ChangesetTransformer.java @ 423:9c9c442b5f2e

Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 23 Mar 2012 22:51:18 +0100
parents 528b6780a8bd
children 31a89587eb04
comparison
equal deleted inserted replaced
422:5d1cc7366d04 423:9c9c442b5f2e
20 20
21 import org.tmatesoft.hg.repo.HgChangelog; 21 import org.tmatesoft.hg.repo.HgChangelog;
22 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; 22 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
23 import org.tmatesoft.hg.repo.HgRepository; 23 import org.tmatesoft.hg.repo.HgRepository;
24 import org.tmatesoft.hg.repo.HgStatusCollector; 24 import org.tmatesoft.hg.repo.HgStatusCollector;
25 import org.tmatesoft.hg.util.Adaptable;
25 import org.tmatesoft.hg.util.CancelSupport; 26 import org.tmatesoft.hg.util.CancelSupport;
26 import org.tmatesoft.hg.util.CancelledException; 27 import org.tmatesoft.hg.util.CancelledException;
27 import org.tmatesoft.hg.util.PathPool; 28 import org.tmatesoft.hg.util.PathPool;
28 import org.tmatesoft.hg.util.PathRewrite; 29 import org.tmatesoft.hg.util.PathRewrite;
29 import org.tmatesoft.hg.util.ProgressSupport; 30 import org.tmatesoft.hg.util.ProgressSupport;
33 * TODO post-1.0 Move to .internal once access to package-local HgChangeset cons is resolved. For 1.0, enough it's package-local 34 * TODO post-1.0 Move to .internal once access to package-local HgChangeset cons is resolved. For 1.0, enough it's package-local
34 * 35 *
35 * @author Artem Tikhomirov 36 * @author Artem Tikhomirov
36 * @author TMate Software Ltd. 37 * @author TMate Software Ltd.
37 */ 38 */
38 /*package-local*/ class ChangesetTransformer implements HgChangelog.Inspector { 39 /*package-local*/ class ChangesetTransformer implements HgChangelog.Inspector, Adaptable, CancelSupport {
39 private final HgChangesetHandler handler; 40 private final HgChangesetHandler handler;
40 private final ProgressSupport progressHelper; 41 private final ProgressSupport progressHelper;
41 private final CancelSupport cancelHelper; 42 private final CancelSupport cancelHelper;
42 private final Transformation t; 43 private final Transformation t;
43 private Set<String> branches; 44 private Set<String> branches;
54 throw new IllegalArgumentException(); 55 throw new IllegalArgumentException();
55 } 56 }
56 HgStatusCollector statusCollector = new HgStatusCollector(hgRepo); 57 HgStatusCollector statusCollector = new HgStatusCollector(hgRepo);
57 t = new Transformation(statusCollector, pw); 58 t = new Transformation(statusCollector, pw);
58 handler = delegate; 59 handler = delegate;
60 // we let HgChangelog#range deal with progress (pipe through getAdapter)
61 // but use own cancellation (which involves CallbackTargetException as well, and preserves original cancellation
62 // exception in case clients care)
59 cancelHelper = cs; 63 cancelHelper = cs;
60 progressHelper = ps; 64 progressHelper = ps;
61 } 65 }
62 66
63 public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) { 67 public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) {
64 if (failure != null || cancellation != null) {
65 return; // FIXME need a better way to stop iterating revlog
66 }
67 if (branches != null && !branches.contains(cset.branch())) { 68 if (branches != null && !branches.contains(cset.branch())) {
68 return; 69 return;
69 } 70 }
70 71
71 HgChangeset changeset = t.handle(revisionNumber, nodeid, cset); 72 HgChangeset changeset = t.handle(revisionNumber, nodeid, cset);
72 try { 73 try {
73 handler.next(changeset); 74 handler.next(changeset);
74 progressHelper.worked(1);
75 cancelHelper.checkCancelled(); 75 cancelHelper.checkCancelled();
76 } catch (RuntimeException ex) { 76 } catch (HgCallbackTargetException ex) {
77 failure = new HgCallbackTargetException(ex).setRevision(nodeid).setRevisionIndex(revisionNumber); 77 failure = ex.setRevision(nodeid).setRevisionIndex(revisionNumber);
78 } catch (CancelledException ex) { 78 } catch (CancelledException ex) {
79 cancellation = ex; 79 cancellation = ex;
80 } 80 }
81 } 81 }
82 82
116 HgChangeset handle(int revisionNumber, Nodeid nodeid, RawChangeset cset) { 116 HgChangeset handle(int revisionNumber, Nodeid nodeid, RawChangeset cset) {
117 changeset.init(revisionNumber, nodeid, cset); 117 changeset.init(revisionNumber, nodeid, cset);
118 return changeset; 118 return changeset;
119 } 119 }
120 } 120 }
121
122 public void checkCancelled() throws CancelledException {
123 if (failure != null || cancellation != null) {
124 // stop HgChangelog.Iterator. Our exception is for the purposes of cancellation only,
125 // the one we have stored (this.cancellation) is for user
126 throw new CancelledException();
127 }
128 }
129
130 public <T> T getAdapter(Class<T> adapterClass) {
131 if (adapterClass == ProgressSupport.class) {
132 return adapterClass.cast(progressHelper);
133 }
134 return null;
135 }
121 } 136 }