Coverage Report - com.aragost.javahg.commands.StatusCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
StatusCommand
88%
16/18
100%
4/4
1.929
StatusCommand$StatusLineIterator
69%
16/23
60%
6/10
1.929
 
 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  16
         super(repository);
 53  16
     }
 54  
 
 55  
     /**
 56  
      * @return a status result.
 57  
      */
 58  
     public StatusResult execute() {
 59  13
         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  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  
      * @param files
 78  
      *            the files to query.
 79  
      * @return a status result.
 80  
      */
 81  
     public StatusResult execute(File... files) {
 82  0
         return execute(Utils.fileArray2StringArray(files));
 83  
     }
 84  
 
 85  
     /**
 86  
      * 
 87  
      * @return a list of status lines.
 88  
      */
 89  
     public List<StatusLine> lines() {
 90  4
         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  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  
      * @param files
 108  
      *            the files to query.
 109  
      * @return a list of status lines.
 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  
      * @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  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  
 }