Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UpdateResult |
|
| 1.4285714285714286;1.429 |
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 | 1 | 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 | 26 | private UpdateResult() { |
49 | 26 | } |
50 | ||
51 | /** | |
52 | * @return number of updated files. | |
53 | */ | |
54 | public int getUpdated() { | |
55 | 6 | 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 | 6 | return merged; |
65 | } | |
66 | ||
67 | /** | |
68 | * @return number of removed files. | |
69 | */ | |
70 | public int getRemoved() { | |
71 | 6 | return removed; |
72 | } | |
73 | ||
74 | /** | |
75 | * @return number of unresolved files. | |
76 | */ | |
77 | public int getUnresolved() { | |
78 | 6 | 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 | 26 | UpdateResult result = new UpdateResult(); |
92 | 26 | Matcher matcher = UPDATE_LINE_PATTERN.matcher(line); |
93 | 26 | if (!matcher.matches()) { |
94 | 0 | throw new UnexpectedCommandOutputException("Unexpected format: " + line); |
95 | } | |
96 | 26 | int[] numbers = new int[4]; |
97 | 130 | for (int i = 0; i < 4; i++) { |
98 | 104 | numbers[i] = Integer.parseInt(line.substring(matcher.start(i + 1), matcher.end(i + 1))); |
99 | } | |
100 | 26 | result.updated = numbers[0]; |
101 | 26 | result.merged = numbers[1]; |
102 | 26 | result.removed = numbers[2]; |
103 | 26 | result.unresolved = numbers[3]; |
104 | 26 | return result; |
105 | } | |
106 | ||
107 | @Override | |
108 | public String toString() { | |
109 | 0 | return "updated " + getUpdated() + ", merged " + getMerged() + ", removed " + getRemoved() + ", unresolved " |
110 | + getUnresolved(); | |
111 | } | |
112 | } |