Coverage Report - com.aragost.javahg.commands.StatusResult
 
Classes in this File Line Coverage Branch Coverage Complexity
StatusResult
60%
37/61
53%
8/15
2.182
StatusResult$1
100%
1/1
N/A
2.182
 
 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  13
 public class StatusResult {
 41  13
     private List<String> modified = Lists.newArrayList();
 42  13
     private List<String> added = Lists.newArrayList();
 43  13
     private List<String> removed = Lists.newArrayList();
 44  13
     private List<String> clean = Lists.newArrayList();
 45  13
     private List<String> missing = Lists.newArrayList();
 46  13
     private List<String> unknown = Lists.newArrayList();
 47  13
     private List<String> ignored = Lists.newArrayList();
 48  
     // Map from new name to old name
 49  13
     private Map<String, String> copied = Maps.newHashMap();
 50  
 
 51  
     /**
 52  
      * @return files with status M
 53  
      */
 54  
     public List<String> getModified() {
 55  8
         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  7
         return added;
 71  
     }
 72  
 
 73  
     /**
 74  
      * @return Files with status R
 75  
      */
 76  
     public List<String> getRemoved() {
 77  11
         return removed;
 78  
     }
 79  
 
 80  
     /**
 81  
      * @return files with status C
 82  
      */
 83  
     public List<String> getClean() {
 84  3
         return clean;
 85  
     }
 86  
 
 87  
     /**
 88  
      * @return files with status !
 89  
      */
 90  
     public List<String> getMissing() {
 91  3
         return missing;
 92  
     }
 93  
 
 94  
     /**
 95  
      * @return files with status ?
 96  
      */
 97  
     public List<String> getUnknown() {
 98  3
         return unknown;
 99  
     }
 100  
 
 101  
     /**
 102  
      * @return files with status I
 103  
      */
 104  
     public List<String> getIgnored() {
 105  3
         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  4
         return copied;
 116  
     }
 117  
 
 118  
     void addLine(StatusLine current) {
 119  21
         String fileName = current.getFileName();
 120  21
         switch (current.getType()) {
 121  
         case ADDED:
 122  7
             this.added.add(fileName);
 123  7
             break;
 124  
         case CLEAN:
 125  2
             this.clean.add(fileName);
 126  2
             break;
 127  
         case IGNORED:
 128  1
             this.ignored.add(fileName);
 129  1
             break;
 130  
         case MISSING:
 131  1
             this.missing.add(fileName);
 132  1
             break;
 133  
         case MODIFIED:
 134  4
             this.modified.add(fileName);
 135  4
             break;
 136  
         case ORIGIN:
 137  
             // The previous line is ADDED. It should be removed,
 138  
             // and replaced with a mapping in copied
 139  2
             String newName = this.added.remove(this.added.size() - 1);
 140  2
             this.copied.put(newName, fileName);
 141  2
             break;
 142  
         case REMOVED:
 143  3
             this.removed.add(fileName);
 144  3
             break;
 145  
         case UNKNOWN:
 146  1
             this.unknown.add(fileName);
 147  1
             break;
 148  
         default:
 149  0
             throw new IllegalStateException("Unhandled StatusLine type: " + current.getType());
 150  
         }
 151  21
     }
 152  
 
 153  
     @Override
 154  
     public String toString() {
 155  0
         StringBuilder s = new StringBuilder("status(");
 156  0
         toStringHelper(s, this.modified, "modified");
 157  0
         toStringHelper(s, this.added, "added");
 158  0
         toStringHelper(s, this.removed, "removed");
 159  0
         toStringHelper(s, this.unknown, "unknown");
 160  0
         toStringHelper(s, this.missing, "missing");
 161  0
         toStringHelper(s, this.ignored, "ignored");
 162  0
         toStringHelper(s, this.clean, "clean");
 163  0
         s.append(")");
 164  0
         return s.toString();
 165  
     }
 166  
 
 167  
     private void toStringHelper(StringBuilder s, List<String> coll, String name) {
 168  0
         int size = coll.size();
 169  0
         if (size == 0) {
 170  0
             return;
 171  
         }
 172  0
         s.append('[');
 173  0
         if (size > 5) {
 174  0
             s.append(size).append(" files");
 175  
         } else {
 176  0
             char sep = '=';
 177  0
             s.append(name);
 178  0
             for (String e : coll) {
 179  0
                 s.append(sep).append(e);
 180  0
                 sep = ',';
 181  
             }
 182  
         }
 183  0
         s.append(']');
 184  0
     }
 185  
 }