View Javadoc

1   /*
2    * #%L
3    * JavaHg
4    * %%
5    * Copyright (C) 2011 aragost Trifork ag
6    * %%
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   * #L%
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   * Command class for executing <tt>hg status</tt>. Set flags from
43   * {@link StatusCommandFlags} and call the {@link #execute} method.
44   */
45  public class StatusCommand extends StatusCommandFlags implements Iterable<StatusLine> {
46  
47      /**
48       * @param repository
49       *            the repository associated with this command.
50       */
51      public StatusCommand(Repository repository) {
52          super(repository);
53      }
54  
55      /**
56       * @return a status result.
57       */
58      public StatusResult execute() {
59          return execute(new String[0]);
60      }
61  
62      /**
63       * @param files
64       *            the files to query.
65       * @return a status result.
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       * @param files
78       *            the files to query.
79       * @return a status result.
80       */
81      public StatusResult execute(File... files) {
82          return execute(Utils.fileArray2StringArray(files));
83      }
84  
85      /**
86       * 
87       * @return a list of status lines.
88       */
89      public List<StatusLine> lines() {
90          return lines(new String[0]);
91      }
92  
93      /**
94       * @param files
95       *            the files to query.
96       * @return a list of status lines.
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      * @param files
108      *            the files to query.
109      * @return a list of status lines.
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      * @param files
121      *            the files to query
122      * @return an iterator over status lines. The iterator must be
123      *         exhausted before another command is sent to the server.
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 }