1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
44
45
46
47
48
49
50 @Immutable
51 @SuppressWarnings("PMD.TooManyMethods")
52 public interface Label extends Comparable<Label>, JsonReadable, JsonPatchable {
53
54
55
56
57
58
59 Repo repo();
60
61
62
63
64
65 String name();
66
67
68
69
70 @Immutable
71 @ToString
72 @Loggable(Loggable.DEBUG)
73 @EqualsAndHashCode(of = { "label", "jsn" })
74 final class Smart implements Label {
75
76
77
78 private final transient Label label;
79
80
81
82 private final transient SmartJson jsn;
83
84
85
86
87
88 public Smart(final Label lbl) {
89 this.label = lbl;
90 this.jsn = new SmartJson(lbl);
91 }
92
93
94
95
96
97
98 public String color() throws IOException {
99 return this.jsn.text("color");
100 }
101
102
103
104
105
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
141
142 @Immutable
143 @ToString
144 @Loggable(Loggable.DEBUG)
145 @EqualsAndHashCode(of = { "repo", "obj" })
146 final class Unmodified implements Label {
147
148
149
150 private final transient Repo repo;
151
152
153
154 private final transient String obj;
155
156
157
158
159
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 }