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 | |
package com.aragost.javahg.commands; |
27 | |
|
28 | |
import java.io.File; |
29 | |
import java.io.IOException; |
30 | |
import java.util.Iterator; |
31 | |
import java.util.List; |
32 | |
import java.util.NoSuchElementException; |
33 | |
|
34 | |
import com.aragost.javahg.Repository; |
35 | |
import com.aragost.javahg.commands.flags.StatusCommandFlags; |
36 | |
import com.aragost.javahg.internals.HgInputStream; |
37 | |
import com.aragost.javahg.internals.RuntimeIOException; |
38 | |
import com.aragost.javahg.internals.Utils; |
39 | |
import com.google.common.collect.Lists; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class StatusCommand extends StatusCommandFlags implements Iterable<StatusLine> { |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public StatusCommand(Repository repository) { |
52 | 16 | super(repository); |
53 | 16 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public StatusResult execute() { |
59 | 13 | return execute(new String[0]); |
60 | |
} |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public StatusResult execute(String... files) { |
68 | 13 | StatusResult result = new StatusResult(); |
69 | 13 | for (Iterator<StatusLine> i = iterator(files); i.hasNext();) { |
70 | 21 | StatusLine line = i.next(); |
71 | 21 | result.addLine(line); |
72 | 21 | } |
73 | 13 | return result; |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public StatusResult execute(File... files) { |
82 | 0 | return execute(Utils.fileArray2StringArray(files)); |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public List<StatusLine> lines() { |
90 | 4 | return lines(new String[0]); |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public List<StatusLine> lines(String... files) { |
99 | 4 | List<StatusLine> lines = Lists.newArrayList(); |
100 | 4 | for (Iterator<StatusLine> i = iterator(files); i.hasNext();) { |
101 | 4 | lines.add(i.next()); |
102 | |
} |
103 | 4 | return lines; |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public List<StatusLine> lines(File... files) { |
112 | 0 | return lines(Utils.fileArray2StringArray(files)); |
113 | |
} |
114 | |
|
115 | |
public Iterator<StatusLine> iterator() { |
116 | 1 | return iterator(new String[0]); |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public Iterator<StatusLine> iterator(String... files) { |
126 | 18 | return new StatusLineIterator(launchStream(files)); |
127 | |
} |
128 | |
|
129 | 27 | private static class StatusLineIterator implements Iterator<StatusLine> { |
130 | |
|
131 | 18 | private boolean nextLineCurrent = false; |
132 | 18 | private StatusLine nextLine = null; |
133 | |
private HgInputStream in; |
134 | |
|
135 | |
public StatusLineIterator(HgInputStream in) { |
136 | 18 | super(); |
137 | 18 | this.in = in; |
138 | 18 | } |
139 | |
|
140 | |
public boolean hasNext() { |
141 | 45 | if (!this.nextLineCurrent) { |
142 | 45 | this.nextLine = readLine(); |
143 | 45 | this.nextLineCurrent = true; |
144 | |
} |
145 | 45 | return this.nextLine != null; |
146 | |
} |
147 | |
|
148 | |
public StatusLine next() { |
149 | 27 | if (this.nextLineCurrent) { |
150 | 27 | this.nextLineCurrent = false; |
151 | 27 | return this.nextLine; |
152 | |
} else { |
153 | 0 | StatusLine line = readLine(); |
154 | 0 | if (line == null) { |
155 | 0 | throw new NoSuchElementException(); |
156 | |
} |
157 | 0 | return line; |
158 | |
} |
159 | |
|
160 | |
} |
161 | |
|
162 | |
private StatusLine readLine() { |
163 | |
try { |
164 | 45 | if (this.in.isEof()) { |
165 | 18 | return null; |
166 | |
} |
167 | 27 | return StatusLine.fromStream(this.in); |
168 | 0 | } catch (IOException e) { |
169 | 0 | throw new RuntimeIOException(e); |
170 | |
} |
171 | |
} |
172 | |
|
173 | |
public void remove() { |
174 | 0 | throw new UnsupportedOperationException(); |
175 | |
} |
176 | |
} |
177 | |
} |