View Javadoc

1   /*
2   
3    * #%L
4    * JavaHg
5    * %%
6    * Copyright (C) 2011 aragost Trifork ag
7    * %%
8    * Permission is hereby granted, free of charge, to any person obtaining a copy
9    * of this software and associated documentation files (the "Software"), to deal
10   * in the Software without restriction, including without limitation the rights
11   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12   * copies of the Software, and to permit persons to whom the Software is
13   * furnished to do so, subject to the following conditions:
14   * 
15   * The above copyright notice and this permission notice shall be included in
16   * all copies or substantial portions of the Software.
17   * 
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24   * THE SOFTWARE.
25   * #L%
26   */
27  package com.aragost.javahg.commands;
28  
29  import java.util.List;
30  import java.util.Map;
31  
32  import com.google.common.collect.Lists;
33  import com.google.common.collect.Maps;
34  
35  /**
36   * An aggregated status result. This class gives easy access to the
37   * files of different status types and will in particular parse added
38   * and copied files correctly.
39   */
40  public class StatusResult {
41      private List<String> modified = Lists.newArrayList();
42      private List<String> added = Lists.newArrayList();
43      private List<String> removed = Lists.newArrayList();
44      private List<String> clean = Lists.newArrayList();
45      private List<String> missing = Lists.newArrayList();
46      private List<String> unknown = Lists.newArrayList();
47      private List<String> ignored = Lists.newArrayList();
48      // Map from new name to old name
49      private Map<String, String> copied = Maps.newHashMap();
50  
51      /**
52       * @return files with status M
53       */
54      public List<String> getModified() {
55          return modified;
56      }
57  
58      /**
59       * List of added files. Files that has been copied is <em>not</em>
60       * in this List, even though 'hg status' reports them with status
61       * A. Instead they are in {@code copied}.
62       * <p>
63       * If the <tt>--copies</tt> flag isn't set the status command does
64       * not report origin, and this List will also include copied
65       * files.
66       * 
67       * @return files with status A
68       */
69      public List<String> getAdded() {
70          return added;
71      }
72  
73      /**
74       * @return Files with status R
75       */
76      public List<String> getRemoved() {
77          return removed;
78      }
79  
80      /**
81       * @return files with status C
82       */
83      public List<String> getClean() {
84          return clean;
85      }
86  
87      /**
88       * @return files with status !
89       */
90      public List<String> getMissing() {
91          return missing;
92      }
93  
94      /**
95       * @return files with status ?
96       */
97      public List<String> getUnknown() {
98          return unknown;
99      }
100 
101     /**
102      * @return files with status I
103      */
104     public List<String> getIgnored() {
105         return ignored;
106     }
107 
108     /**
109      * Mapping copied files. The key is the new name and value is
110      * origin.
111      * 
112      * @return map from new name to origin for copied files.
113      */
114     public Map<String, String> getCopied() {
115         return copied;
116     }
117 
118     void addLine(StatusLine current) {
119         String fileName = current.getFileName();
120         switch (current.getType()) {
121         case ADDED:
122             this.added.add(fileName);
123             break;
124         case CLEAN:
125             this.clean.add(fileName);
126             break;
127         case IGNORED:
128             this.ignored.add(fileName);
129             break;
130         case MISSING:
131             this.missing.add(fileName);
132             break;
133         case MODIFIED:
134             this.modified.add(fileName);
135             break;
136         case ORIGIN:
137             // The previous line is ADDED. It should be removed,
138             // and replaced with a mapping in copied
139             String newName = this.added.remove(this.added.size() - 1);
140             this.copied.put(newName, fileName);
141             break;
142         case REMOVED:
143             this.removed.add(fileName);
144             break;
145         case UNKNOWN:
146             this.unknown.add(fileName);
147             break;
148         default:
149             throw new IllegalStateException("Unhandled StatusLine type: " + current.getType());
150         }
151     }
152 
153     @Override
154     public String toString() {
155         StringBuilder s = new StringBuilder("status(");
156         toStringHelper(s, this.modified, "modified");
157         toStringHelper(s, this.added, "added");
158         toStringHelper(s, this.removed, "removed");
159         toStringHelper(s, this.unknown, "unknown");
160         toStringHelper(s, this.missing, "missing");
161         toStringHelper(s, this.ignored, "ignored");
162         toStringHelper(s, this.clean, "clean");
163         s.append(")");
164         return s.toString();
165     }
166 
167     private void toStringHelper(StringBuilder s, List<String> coll, String name) {
168         int size = coll.size();
169         if (size == 0) {
170             return;
171         }
172         s.append('[');
173         if (size > 5) {
174             s.append(size).append(" files");
175         } else {
176             char sep = '=';
177             s.append(name);
178             for (String e : coll) {
179                 s.append(sep).append(e);
180                 sep = ',';
181             }
182         }
183         s.append(']');
184     }
185 }