View Javadoc
1   /**
2    * Copyright (c) 2013-2023, 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.StringReader;
36  import javax.json.Json;
37  import javax.json.JsonObject;
38  import lombok.EqualsAndHashCode;
39  import lombok.ToString;
40  import org.apache.commons.lang3.builder.CompareToBuilder;
41  
42  /**
43   * Github label.
44   * @author Yegor Bugayenko (yegor256@gmail.com)
45   * @version $Id: 642c80ad86152807e62c49bef370cc2410573817 $
46   * @checkstyle MultipleStringLiterals (500 lines)
47   * @see <a href="https://developer.github.com/v3/issues/labels/">Labels API</a>
48   * @since 0.1
49   */
50  @Immutable
51  @SuppressWarnings("PMD.TooManyMethods")
52  public interface Label extends Comparable<Label>, JsonReadable, JsonPatchable {
53  
54      /**
55       * The repo we're in.
56       * @return Issue
57       * @since 0.6
58       */
59      Repo repo();
60  
61      /**
62       * Name of it.
63       * @return Name
64       */
65      String name();
66  
67      /**
68       * Smart Label with extra features.
69       */
70      @Immutable
71      @ToString
72      @Loggable(Loggable.DEBUG)
73      @EqualsAndHashCode(of = { "label", "jsn" })
74      final class Smart implements Label {
75          /**
76           * Encapsulated label.
77           */
78          private final transient Label label;
79          /**
80           * SmartJson object for convenient JSON parsing.
81           */
82          private final transient SmartJson jsn;
83  
84          /**
85           * Public ctor.
86           * @param lbl Label
87           */
88          public Smart(final Label lbl) {
89              this.label = lbl;
90              this.jsn = new SmartJson(lbl);
91          }
92  
93          /**
94           * Get its color.
95           * @return Color of it
96           * @throws IOException If there is any I/O problem
97           */
98          public String color() throws IOException {
99              return this.jsn.text("color");
100         }
101 
102         /**
103          * Set its color.
104          * @param color Color to set
105          * @throws IOException If there is any I/O problem
106          */
107         public void color(final String color) throws IOException {
108             this.label.patch(
109                 Json.createObjectBuilder().add("color", color).build()
110             );
111         }
112 
113         @Override
114         public Repo repo() {
115             return this.label.repo();
116         }
117 
118         @Override
119         public String name() {
120             return this.label.name();
121         }
122 
123         @Override
124         public int compareTo(final Label lbl) {
125             return this.label.compareTo(lbl);
126         }
127 
128         @Override
129         public void patch(final JsonObject json) throws IOException {
130             this.label.patch(json);
131         }
132 
133         @Override
134         public JsonObject json() throws IOException {
135             return this.label.json();
136         }
137     }
138 
139     /**
140      * Unmodified Label with extra features.
141      */
142     @Immutable
143     @ToString
144     @Loggable(Loggable.DEBUG)
145     @EqualsAndHashCode(of = { "repo", "obj" })
146     final class Unmodified implements Label {
147         /**
148          * Encapsulated Repo.
149          */
150         private final transient Repo repo;
151         /**
152          * Encapsulated String.
153          */
154         private final transient String obj;
155 
156         /**
157          * Public ctor.
158          * @param rep Repo
159          * @param object String
160          */
161         public Unmodified(
162             final Repo rep, final String object
163         ) {
164             this.repo = rep;
165             this.obj = object;
166         }
167 
168         @Override
169         public Repo repo() {
170             return this.repo;
171         }
172 
173         @Override
174         public String name() {
175             return this.json().getString("name");
176         }
177 
178         @Override
179         public int compareTo(final Label label) {
180             return new CompareToBuilder()
181                 .append(this.repo().coordinates(), label.repo().coordinates())
182                 .append(this.obj, label.name())
183                 .build();
184         }
185 
186         @Override
187         public void patch(final JsonObject json) {
188             throw new UnsupportedOperationException("#patch()");
189         }
190 
191         @Override
192         public JsonObject json() {
193             return Json.createReader(new StringReader(this.obj)).readObject();
194         }
195     }
196 }