View Javadoc

1   package com.aragost.javahg.internals;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.List;
6   
7   import com.google.common.collect.Lists;
8   
9   /**
10   * Helper class to implement Add and Removed Command.
11   */
12  public class AddRemoveCommandHelper {
13  
14      private AbstractCommand command;
15  
16      private String prefix;
17  
18      public AddRemoveCommandHelper(AbstractCommand command, String prefix) {
19          super();
20          this.command = command;
21          this.prefix = prefix;
22      }
23  
24      public List<String> execute(String... files) {
25          List<String> result = Lists.newArrayList();
26          for (String line : command.launchIterator(files)) {
27              if (line.startsWith(prefix)) {
28                  String file = new String(line.substring(prefix.length()));
29                  result.add(file);
30              } else {
31                  command.cleanUp();
32                  throw new UnexpectedCommandOutputException(this.command, line);
33              }
34          }
35          return result;
36      }
37  
38      public List<File> execute(File... files) {
39          return stringList2fileList(execute(fileArray2StringArray(files)));
40      }
41  
42      public List<File> execute() {
43          return execute(new File[0]);
44      }
45  
46      /**
47       * Helper method to convert Files to Strings
48       * 
49       * @param files
50       * @return
51       */
52      private final String[] fileArray2StringArray(File[] files) {
53          return Utils.fileArray2StringArray(files);
54      }
55  
56      /**
57       * Helper method to convert Strings to Files
58       * 
59       * @param strings
60       * @return
61       * @throws IOException
62       */
63      private final List<File> stringList2fileList(List<String> strings) {
64          String[] strArray = strings.toArray(new String[strings.size()]);
65          File[] fileArray = Utils.stringArray2FileArray(this.command.getRepository().getDirectory(), strArray);
66          return Lists.newArrayList(fileArray);
67      }
68  
69  }