Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FileChange |
|
| 1.0;1 | ||||
FileChange$Smart |
|
| 1.0;1 | ||||
FileChange$Smart$AjcClosure1 |
|
| 1.0;1 | ||||
FileChange$Smart$AjcClosure11 |
|
| 1.0;1 | ||||
FileChange$Smart$AjcClosure13 |
|
| 1.0;1 | ||||
FileChange$Smart$AjcClosure15 |
|
| 1.0;1 | ||||
FileChange$Smart$AjcClosure17 |
|
| 1.0;1 | ||||
FileChange$Smart$AjcClosure19 |
|
| 1.0;1 | ||||
FileChange$Smart$AjcClosure21 |
|
| 1.0;1 | ||||
FileChange$Smart$AjcClosure3 |
|
| 1.0;1 | ||||
FileChange$Smart$AjcClosure5 |
|
| 1.0;1 | ||||
FileChange$Smart$AjcClosure7 |
|
| 1.0;1 | ||||
FileChange$Smart$AjcClosure9 |
|
| 1.0;1 | ||||
FileChange$Status |
|
| 1.0;1 |
1 | 4 | /** |
2 | * Copyright (c) 2013-2017, jcabi.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | package com.jcabi.github; | |
31 | ||
32 | import com.google.common.base.Optional; | |
33 | import com.jcabi.aspects.Immutable; | |
34 | import com.jcabi.aspects.Loggable; | |
35 | import java.io.IOException; | |
36 | import java.util.Locale; | |
37 | import javax.json.JsonObject; | |
38 | import lombok.EqualsAndHashCode; | |
39 | import lombok.ToString; | |
40 | ||
41 | /** | |
42 | * File change. | |
43 | * @author Chris Rebert (github@chrisrebert.com) | |
44 | * @version $Id: 126de50050dfa5477e74a8fb3a74feb4d162b1e3 $ | |
45 | * @since 0.24 | |
46 | * @see <a href="http://developer.github.com/v3/repos/commits/#compare-two-commits">Compare two commits</a> | |
47 | */ | |
48 | @Immutable | |
49 | @SuppressWarnings("PMD.TooManyMethods") | |
50 | public interface FileChange extends JsonReadable { | |
51 | 7 | enum Status implements StringEnum { |
52 | /** | |
53 | * File was added. | |
54 | */ | |
55 | 1 | ADDED("added"), |
56 | /** | |
57 | * File's content was modified. | |
58 | */ | |
59 | 1 | MODIFIED("modified"), |
60 | /** | |
61 | * File was removed. | |
62 | */ | |
63 | 1 | REMOVED("removed"), |
64 | /** | |
65 | * File was renamed. | |
66 | */ | |
67 | 1 | RENAMED("renamed"); |
68 | ||
69 | /** | |
70 | * File status string. | |
71 | */ | |
72 | private final transient String status; | |
73 | ||
74 | /** | |
75 | * Ctor. | |
76 | * @param stat File status string. | |
77 | */ | |
78 | 4 | Status(final String stat) { |
79 | 4 | this.status = stat; |
80 | 4 | } |
81 | ||
82 | @Override | |
83 | public String identifier() { | |
84 | 0 | return this.status; |
85 | } | |
86 | ||
87 | /** | |
88 | * Get file change status corresponding to the given status string. | |
89 | * @param name Status string | |
90 | * @return Status enum value | |
91 | */ | |
92 | public static FileChange.Status forValue(final String name) { | |
93 | 5 | return Status.valueOf(name.toUpperCase(Locale.ENGLISH)); |
94 | } | |
95 | } | |
96 | ||
97 | /** | |
98 | * Smart file change with extra features. | |
99 | */ | |
100 | @Immutable | |
101 | 0 | @ToString |
102 | @Loggable(Loggable.DEBUG) | |
103 | 0 | @EqualsAndHashCode(of = { "change", "jsn" }) |
104 | @SuppressWarnings("PMD.TooManyMethods") | |
105 | final class Smart implements FileChange { | |
106 | /** | |
107 | * Encapsulated file change. | |
108 | */ | |
109 | private final transient FileChange change; | |
110 | /** | |
111 | * SmartJson object for convenient JSON parsing. | |
112 | */ | |
113 | private final transient SmartJson jsn; | |
114 | ||
115 | /** | |
116 | * Public ctor. | |
117 | * @param chng File change | |
118 | */ | |
119 | 15 | public Smart(final FileChange chng) { |
120 | 16 | this.change = chng; |
121 | 15 | this.jsn = new SmartJson(chng); |
122 | 16 | } |
123 | ||
124 | /** | |
125 | * File's commit SHA. | |
126 | * @return SHA | |
127 | * @throws IOException If there is any I/O problem | |
128 | */ | |
129 | public String sha() throws IOException { | |
130 | 4 | return this.jsn.text("sha"); |
131 | } | |
132 | ||
133 | /** | |
134 | * File's name. Includes the path to the file from the | |
135 | * root directory of the repository. Does not start with a | |
136 | * forward slash. Example: "foo/bar/baz.txt" | |
137 | * @return Filename | |
138 | * @throws IOException If there is any I/O problem | |
139 | */ | |
140 | public String filename() throws IOException { | |
141 | 6 | return this.jsn.text("filename"); |
142 | } | |
143 | ||
144 | /** | |
145 | * Status of the file in this change. | |
146 | * @return File status | |
147 | * @throws IOException If there is any I/O problem | |
148 | */ | |
149 | public FileChange.Status status() throws IOException { | |
150 | 10 | return FileChange.Status.forValue(this.jsn.text("status")); |
151 | } | |
152 | ||
153 | /** | |
154 | * Number of lines added, or 0 if the file is binary. | |
155 | * @return Number of lines added | |
156 | * @throws IOException If there is any I/O problem | |
157 | */ | |
158 | public int additions() throws IOException { | |
159 | 4 | return this.jsn.number("additions"); |
160 | } | |
161 | ||
162 | /** | |
163 | * Number of lines deleted, or 0 if the file is binary. | |
164 | * @return Number of lines deleted | |
165 | * @throws IOException If there is any I/O problem | |
166 | */ | |
167 | public int deletions() throws IOException { | |
168 | 4 | return this.jsn.number("deletions"); |
169 | } | |
170 | ||
171 | /** | |
172 | * Number of lines modified, which is equal to the sum of | |
173 | * {@link Smart#additions()} and {@link Smart#deletions()}. | |
174 | * @return Number of lines modified | |
175 | * @throws IOException If there is any I/O problem | |
176 | */ | |
177 | public int changes() throws IOException { | |
178 | 4 | return this.jsn.number("changes"); |
179 | } | |
180 | ||
181 | /** | |
182 | * Diff string of the changes to the file. Only available if | |
183 | * the file is text (as opposed to binary). | |
184 | * @return Diff string | |
185 | * @throws IOException If there is any I/O problem | |
186 | */ | |
187 | public Optional<String> patch() throws IOException { | |
188 | 6 | return Optional.fromNullable(this.json().getString("patch", null)); |
189 | } | |
190 | ||
191 | /** | |
192 | * URL for the raw contents of the file. | |
193 | * @return URL | |
194 | * @throws IOException If there is any I/O problem | |
195 | */ | |
196 | public String rawUrl() throws IOException { | |
197 | 2 | return this.jsn.text("raw_url"); |
198 | } | |
199 | ||
200 | /** | |
201 | * URL for the file's git blob. | |
202 | * @return URL | |
203 | * @throws IOException If there is any I/O problem | |
204 | */ | |
205 | public String blobUrl() throws IOException { | |
206 | 2 | return this.jsn.text("blob_url"); |
207 | } | |
208 | ||
209 | /** | |
210 | * Repo contents URL for the file. | |
211 | * @return URL | |
212 | * @throws IOException If there is any I/O problem | |
213 | */ | |
214 | public String contentsUrl() throws IOException { | |
215 | 2 | return this.jsn.text("contents_url"); |
216 | } | |
217 | ||
218 | @Override | |
219 | public JsonObject json() throws IOException { | |
220 | 6 | return this.change.json(); |
221 | } | |
222 | } | |
223 | } |