comparison src/org/tmatesoft/hg/core/HgCatCommand.java @ 148:1a7a9a20e1f9

Exceptions, javadoc. Initial cancel and progress support
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 23 Feb 2011 22:36:28 +0100
parents 4a948ec83980
children d5268ca7715b
comparison
equal deleted inserted replaced
147:a05145db4d0c 148:1a7a9a20e1f9
14 * the terms of a license other than GNU General Public License 14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.core; 17 package org.tmatesoft.hg.core;
18 18
19 import static org.tmatesoft.hg.repo.HgInternals.wrongLocalRevision;
19 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; 20 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION;
20 import static org.tmatesoft.hg.repo.HgRepository.TIP; 21 import static org.tmatesoft.hg.repo.HgRepository.TIP;
21 22
22 import java.io.FileNotFoundException; 23 import java.io.FileNotFoundException;
24 import java.io.IOException;
23 25
24 import org.tmatesoft.hg.repo.HgDataFile; 26 import org.tmatesoft.hg.repo.HgDataFile;
25 import org.tmatesoft.hg.repo.HgRepository; 27 import org.tmatesoft.hg.repo.HgRepository;
26 import org.tmatesoft.hg.util.ByteChannel; 28 import org.tmatesoft.hg.util.ByteChannel;
29 import org.tmatesoft.hg.util.CancelledException;
27 import org.tmatesoft.hg.util.Path; 30 import org.tmatesoft.hg.util.Path;
28 31
29 /** 32 /**
30 * Command to obtain content of a file, 'hg cat' counterpart. 33 * Command to obtain content of a file, 'hg cat' counterpart.
31 * 34 *
41 44
42 public HgCatCommand(HgRepository hgRepo) { 45 public HgCatCommand(HgRepository hgRepo) {
43 repo = hgRepo; 46 repo = hgRepo;
44 } 47 }
45 48
49 /**
50 * File to read, required parameter
51 * @param fname path to a repository file, can't be <code>null</code>
52 * @return <code>this</code> for convenience
53 * @throws IllegalArgumentException if supplied fname is null or points to directory
54 */
46 public HgCatCommand file(Path fname) { 55 public HgCatCommand file(Path fname) {
56 if (fname == null || fname.isDirectory()) {
57 throw new IllegalArgumentException(String.valueOf(fname));
58 }
47 file = fname; 59 file = fname;
48 return this; 60 return this;
49 } 61 }
50 62
51 // rev can't be WORKING_COPY (if allowed, need to implement in #execute()) 63 /**
64 * Invocation of this method clears revision set with {@link #revision(Nodeid)} or {@link #revision(int)} earlier.
65 * XXX rev can't be WORKING_COPY (if allowed, need to implement in #execute())
66 * @param rev local revision number, non-negative, or one of predefined constants. Note, use of {@link HgRepository#BAD_REVISION},
67 * although possible, makes little sense (command would fail if executed).
68 * @return <code>this</code> for convenience
69 */
52 public HgCatCommand revision(int rev) { 70 public HgCatCommand revision(int rev) {
71 if (wrongLocalRevision(rev)) {
72 throw new IllegalArgumentException(String.valueOf(rev));
73 }
53 localRevision = rev; 74 localRevision = rev;
54 revision = null; 75 revision = null;
55 return this; 76 return this;
56 } 77 }
57 78
79 /**
80 * Select revision to read. Invocation of this method clears revision set with {@link #revision(int)} or {@link #revision(Nodeid)} earlier.
81 *
82 * @param nodeid - unique revision identifier, Note, use of <code>null</code> or {@link Nodeid#NULL} is senseless
83 * @return <code>this</code> for convenience
84 */
58 public HgCatCommand revision(Nodeid nodeid) { 85 public HgCatCommand revision(Nodeid nodeid) {
86 if (nodeid != null && nodeid.isNull()) {
87 nodeid = null;
88 }
59 revision = nodeid; 89 revision = nodeid;
60 localRevision = BAD_REVISION; 90 localRevision = BAD_REVISION;
61 return this; 91 return this;
62 } 92 }
63 93
64 public void execute(ByteChannel sink) throws Exception /*TODO own exception type*/ { 94 /**
95 * Runs the command with current set of parameters and pipes data to provided sink.
96 *
97 * @param sink output channel to write data to.
98 * @throws HgDataStreamException
99 * @throws IllegalArgumentException when command arguments are incomplete or wrong
100 */
101 public void execute(ByteChannel sink) throws HgDataStreamException, IOException, CancelledException {
65 if (localRevision == BAD_REVISION && revision == null) { 102 if (localRevision == BAD_REVISION && revision == null) {
66 throw new IllegalArgumentException("Either local file revision number or nodeid shall be specified"); 103 throw new IllegalArgumentException("Either local file revision number or nodeid shall be specified");
67 } 104 }
68 if (file == null) { 105 if (file == null) {
69 throw new IllegalArgumentException("Name of the file is missing"); 106 throw new IllegalArgumentException("Name of the file is missing");
70 } 107 }
71 if (sink == null) { 108 if (sink == null) {
72 throw new IllegalArgumentException(); 109 throw new IllegalArgumentException("Need an output channel");
73 } 110 }
74 HgDataFile dataFile = repo.getFileNode(file); 111 HgDataFile dataFile = repo.getFileNode(file);
75 if (!dataFile.exists()) { 112 if (!dataFile.exists()) {
76 throw new FileNotFoundException(); 113 throw new HgDataStreamException(file.toString(), new FileNotFoundException(file.toString()));
77 } 114 }
78 int revToExtract; 115 int revToExtract;
79 if (revision != null) { 116 if (revision != null) {
80 revToExtract = dataFile.getLocalRevision(revision); 117 revToExtract = dataFile.getLocalRevision(revision);
81 } else { 118 } else {