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.util.Map;
30
31 import com.aragost.javahg.Repository;
32 import com.aragost.javahg.commands.flags.RenameCommandFlags;
33 import com.aragost.javahg.internals.CopyRenameHelper;
34 import com.aragost.javahg.internals.Utils;
35 import com.google.common.annotations.VisibleForTesting;
36 import com.google.common.collect.Maps;
37
38 /**
39 * @see CopyCommand
40 */
41 public class RenameCommand extends RenameCommandFlags {
42
43 @VisibleForTesting
44 static final String PREFIX = "moving ";
45
46 public RenameCommand(Repository repository) {
47 super(repository);
48 withDebugFlag();
49 }
50
51 /**
52 * @param files
53 * the files to copy. The last file in this list is the
54 * destination. If two files are given, the target must be a
55 * directory or a non-existing file. If more files are given, the
56 * destination must be an directory.
57 * @return mapping from new names to old names.
58 */
59 // TODO: this would be clearer if we overloaded execute to take
60 // either (String src, String dst) or (String[] srcs, String dst).
61 public Map<String, String> execute(String... files) {
62 Map<File, File> map = execute(Utils.stringArray2FileArray(getRepository().getDirectory(), files));
63 Map<String, String> result = Maps.newHashMap();
64 for (Map.Entry<File, File> entry : map.entrySet()) {
65 result.put(entry.getKey().getPath(), entry.getValue().getPath());
66 }
67 return result;
68 }
69
70 public Map<File, File> execute(File... files) {
71 if (files.length < 2) {
72 throw new IllegalArgumentException("must provide at least two files");
73 }
74
75 return CopyRenameHelper.parse(getRepository(), files, launchIterator(Utils.fileArray2StringArray(files)), PREFIX);
76 }
77 }