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.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31 import com.aragost.javahg.internals.UnexpectedCommandOutputException;
32
33 /**
34 * The statistic returned by running <tt>hg update</tt>.
35 */
36 public class UpdateResult {
37
38 private static final Pattern UPDATE_LINE_PATTERN = Pattern.compile("^(.*) files updated, (.*) files merged, (.*) files removed, (.*) files unresolved$");
39
40 private int updated;
41
42 private int merged;
43
44 private int removed;
45
46 private int unresolved;
47
48 private UpdateResult() {
49 }
50
51 /**
52 * @return number of updated files.
53 */
54 public int getUpdated() {
55 return updated;
56 }
57
58 /**
59 * @return number of merged files. These are the files that were
60 * merged without conflict, see {@link #getUnresolved()}
61 * for the number of files with merge conflicts.
62 */
63 public int getMerged() {
64 return merged;
65 }
66
67 /**
68 * @return number of removed files.
69 */
70 public int getRemoved() {
71 return removed;
72 }
73
74 /**
75 * @return number of unresolved files.
76 */
77 public int getUnresolved() {
78 return unresolved;
79 }
80
81 /**
82 * Factory method to create an {@link UpdateResult}.
83 *
84 * @param line
85 * the final line of <tt>hg update</tt>
86 * @return the parsed update result.
87 */
88 public static UpdateResult fromLine(String line) {
89 // format of line: 0 files updated, 0 files merged, 0 files
90 // removed, 0 files unresolved
91 UpdateResult result = new UpdateResult();
92 Matcher matcher = UPDATE_LINE_PATTERN.matcher(line);
93 if (!matcher.matches()) {
94 throw new UnexpectedCommandOutputException("Unexpected format: " + line);
95 }
96 int[] numbers = new int[4];
97 for (int i = 0; i < 4; i++) {
98 numbers[i] = Integer.parseInt(line.substring(matcher.start(i + 1), matcher.end(i + 1)));
99 }
100 result.updated = numbers[0];
101 result.merged = numbers[1];
102 result.removed = numbers[2];
103 result.unresolved = numbers[3];
104 return result;
105 }
106
107 @Override
108 public String toString() {
109 return "updated " + getUpdated() + ", merged " + getMerged() + ", removed " + getRemoved() + ", unresolved "
110 + getUnresolved();
111 }
112 }