1 | 2 | /** |
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.jcabi.aspects.Immutable; | |
33 | import com.jcabi.aspects.Loggable; | |
34 | import java.io.IOException; | |
35 | import java.io.InputStream; | |
36 | import java.net.URL; | |
37 | import java.text.ParseException; | |
38 | import java.util.Date; | |
39 | import javax.json.Json; | |
40 | import javax.json.JsonObject; | |
41 | import lombok.EqualsAndHashCode; | |
42 | import lombok.ToString; | |
43 | ||
44 | /** | |
45 | * Github release asset. | |
46 | * @author Carlos Miranda (miranda.cma@gmail.com) | |
47 | * @version $Id: 7a93c497a57ca31bc134aba043d51e9a47c8b149 $ | |
48 | * @since 0.8 | |
49 | */ | |
50 | @Immutable | |
51 | @SuppressWarnings("PMD.TooManyMethods") | |
52 | public interface ReleaseAsset extends JsonReadable, JsonPatchable { | |
53 | ||
54 | /** | |
55 | * The release we're in. | |
56 | * @return Release | |
57 | */ | |
58 | Release release(); | |
59 | ||
60 | /** | |
61 | * Number. | |
62 | * @return Release asset number | |
63 | */ | |
64 | int number(); | |
65 | ||
66 | /** | |
67 | * Delete the release asset. | |
68 | * @throws IOException If there is any I/O problem | |
69 | * @see <a href="http://developer.github.com/v3/repos/releases/#delete-a-release-asset">Delete a Release Asset</a> | |
70 | */ | |
71 | void remove() throws IOException; | |
72 | ||
73 | /** | |
74 | * Gets release asset raw content. | |
75 | * @return Release asset number | |
76 | * @throws IOException If there is any I/O problem | |
77 | * @see <a href="http://developer.github.com/v3/repos/releases/#get-a-single-release-asset">Get a single release asset</a> | |
78 | */ | |
79 | InputStream raw() throws IOException; | |
80 | ||
81 | /** | |
82 | * Smart ReleaseAsset with extra features. | |
83 | * @checkstyle MultipleStringLiterals (500 lines) | |
84 | */ | |
85 | @Immutable | |
86 | 0 | @ToString |
87 | @Loggable(Loggable.DEBUG) | |
88 | 0 | @EqualsAndHashCode(of = {"asset", "jsn" }) |
89 | final class Smart implements ReleaseAsset { | |
90 | /** | |
91 | * Encapsulated Release Asset. | |
92 | */ | |
93 | private final transient ReleaseAsset asset; | |
94 | /** | |
95 | * SmartJson object for convenient JSON parsing. | |
96 | */ | |
97 | private final transient SmartJson jsn; | |
98 | ||
99 | /** | |
100 | * Public ctor. | |
101 | * @param ast Release asset | |
102 | */ | |
103 | public Smart( | |
104 | final ReleaseAsset ast | |
105 | 11 | ) { |
106 | 11 | this.asset = ast; |
107 | 11 | this.jsn = new SmartJson(ast); |
108 | 11 | } |
109 | ||
110 | /** | |
111 | * Get its URL. | |
112 | * @return URL of release asset | |
113 | * @throws IOException If there is any I/O problem | |
114 | */ | |
115 | public URL url() throws IOException { | |
116 | 2 | return new URL(this.jsn.text("url")); |
117 | } | |
118 | ||
119 | /** | |
120 | * Get its name. | |
121 | * @return Name of release asset | |
122 | * @throws IOException If there is any I/O problem | |
123 | */ | |
124 | public String name() throws IOException { | |
125 | 2 | return this.jsn.text("name"); |
126 | } | |
127 | ||
128 | /** | |
129 | * Get its label. | |
130 | * @return Label of release asset | |
131 | * @throws IOException If there is any I/O problem | |
132 | */ | |
133 | public String label() throws IOException { | |
134 | 2 | return this.jsn.text("label"); |
135 | } | |
136 | ||
137 | /** | |
138 | * Get its state. | |
139 | * @return State of release asset | |
140 | * @throws IOException If there is any I/O problem | |
141 | */ | |
142 | public String state() throws IOException { | |
143 | 2 | return this.jsn.text("state"); |
144 | } | |
145 | ||
146 | /** | |
147 | * Get its content type. | |
148 | * @return Content type of release asset | |
149 | * @throws IOException If there is any I/O problem | |
150 | */ | |
151 | public String contentType() throws IOException { | |
152 | 2 | return this.jsn.text("content_type"); |
153 | } | |
154 | ||
155 | /** | |
156 | * Get its size. | |
157 | * @return Size of release asset | |
158 | * @throws IOException If there is any I/O problem | |
159 | */ | |
160 | public int size() throws IOException { | |
161 | 2 | return this.jsn.number("size"); |
162 | } | |
163 | ||
164 | /** | |
165 | * Get its downloadCount. | |
166 | * @return Download count of release asset | |
167 | * @throws IOException If there is any I/O problem | |
168 | */ | |
169 | public int downloadCount() throws IOException { | |
170 | 2 | return this.jsn.number("download_count"); |
171 | } | |
172 | ||
173 | /** | |
174 | * When it was created. | |
175 | * @return Date of creation | |
176 | * @throws IOException If there is any I/O problem | |
177 | */ | |
178 | public Date createdAt() throws IOException { | |
179 | try { | |
180 | 3 | return new Github.Time( |
181 | 1 | this.jsn.text("created_at") |
182 | 1 | ).date(); |
183 | 0 | } catch (final ParseException ex) { |
184 | 0 | throw new IllegalStateException(ex); |
185 | } | |
186 | } | |
187 | ||
188 | /** | |
189 | * When it was updated. | |
190 | * @return Date of update | |
191 | * @throws IOException If there is any I/O problem | |
192 | */ | |
193 | public Date updatedAt() throws IOException { | |
194 | try { | |
195 | 3 | return new Github.Time( |
196 | 1 | this.jsn.text("updated_at") |
197 | 1 | ).date(); |
198 | 0 | } catch (final ParseException ex) { |
199 | 0 | throw new IllegalStateException(ex); |
200 | } | |
201 | } | |
202 | ||
203 | /** | |
204 | * Change its name. | |
205 | * @param text Name of release asset | |
206 | * @throws IOException If there is any I/O problem | |
207 | */ | |
208 | public void name( | |
209 | final String text | |
210 | ) throws IOException { | |
211 | 3 | this.asset.patch( |
212 | 1 | Json.createObjectBuilder().add("name", text).build() |
213 | ); | |
214 | 1 | } |
215 | ||
216 | /** | |
217 | * Change its label. | |
218 | * @param text Label of release asset | |
219 | * @throws IOException If there is any I/O problem | |
220 | */ | |
221 | public void label( | |
222 | final String text | |
223 | ) throws IOException { | |
224 | 3 | this.asset.patch( |
225 | 1 | Json.createObjectBuilder().add("label", text).build() |
226 | ); | |
227 | 1 | } |
228 | ||
229 | @Override | |
230 | public Release release() { | |
231 | 0 | return this.asset.release(); |
232 | } | |
233 | ||
234 | @Override | |
235 | public int number() { | |
236 | 0 | return this.asset.number(); |
237 | } | |
238 | ||
239 | @Override | |
240 | public void remove() throws IOException { | |
241 | 0 | this.asset.remove(); |
242 | 0 | } |
243 | ||
244 | @Override | |
245 | public InputStream raw() throws IOException { | |
246 | 0 | return this.asset.raw(); |
247 | } | |
248 | ||
249 | @Override | |
250 | public void patch( | |
251 | final JsonObject json | |
252 | ) throws IOException { | |
253 | 0 | this.asset.patch(json); |
254 | 0 | } |
255 | ||
256 | @Override | |
257 | public JsonObject json() throws IOException { | |
258 | 0 | return this.asset.json(); |
259 | } | |
260 | } | |
261 | } |