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 super(repository);
53 }
54
55
56
57
58 public StatusResult execute() {
59 return execute(new String[0]);
60 }
61
62
63
64
65
66
67 public StatusResult execute(String... files) {
68 StatusResult result = new StatusResult();
69 for (Iterator<StatusLine> i = iterator(files); i.hasNext();) {
70 StatusLine line = i.next();
71 result.addLine(line);
72 }
73 return result;
74 }
75
76
77
78
79
80
81 public StatusResult execute(File... files) {
82 return execute(Utils.fileArray2StringArray(files));
83 }
84
85
86
87
88
89 public List<StatusLine> lines() {
90 return lines(new String[0]);
91 }
92
93
94
95
96
97
98 public List<StatusLine> lines(String... files) {
99 List<StatusLine> lines = Lists.newArrayList();
100 for (Iterator<StatusLine> i = iterator(files); i.hasNext();) {
101 lines.add(i.next());
102 }
103 return lines;
104 }
105
106
107
108
109
110
111 public List<StatusLine> lines(File... files) {
112 return lines(Utils.fileArray2StringArray(files));
113 }
114
115 public Iterator<StatusLine> iterator() {
116 return iterator(new String[0]);
117 }
118
119
120
121
122
123
124
125 public Iterator<StatusLine> iterator(String... files) {
126 return new StatusLineIterator(launchStream(files));
127 }
128
129 private static class StatusLineIterator implements Iterator<StatusLine> {
130
131 private boolean nextLineCurrent = false;
132 private StatusLine nextLine = null;
133 private HgInputStream in;
134
135 public StatusLineIterator(HgInputStream in) {
136 super();
137 this.in = in;
138 }
139
140 public boolean hasNext() {
141 if (!this.nextLineCurrent) {
142 this.nextLine = readLine();
143 this.nextLineCurrent = true;
144 }
145 return this.nextLine != null;
146 }
147
148 public StatusLine next() {
149 if (this.nextLineCurrent) {
150 this.nextLineCurrent = false;
151 return this.nextLine;
152 } else {
153 StatusLine line = readLine();
154 if (line == null) {
155 throw new NoSuchElementException();
156 }
157 return line;
158 }
159
160 }
161
162 private StatusLine readLine() {
163 try {
164 if (this.in.isEof()) {
165 return null;
166 }
167 return StatusLine.fromStream(this.in);
168 } catch (IOException e) {
169 throw new RuntimeIOException(e);
170 }
171 }
172
173 public void remove() {
174 throw new UnsupportedOperationException();
175 }
176 }
177 }