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 lombok.EqualsAndHashCode;
34 import org.apache.commons.lang3.builder.CompareToBuilder;
35
36
37
38
39
40
41
42
43 @Immutable
44 public interface Coordinates extends Comparable<Coordinates> {
45
46
47
48
49 String SEPARATOR = "/";
50
51
52
53
54
55 String user();
56
57
58
59
60
61 String repo();
62
63
64
65
66 @Immutable
67 @EqualsAndHashCode(of = {"usr", "rpo"})
68 final class Simple implements Coordinates {
69
70
71
72 private final transient String usr;
73
74
75
76 private final transient String rpo;
77
78
79
80
81
82
83 public Simple(final String user, final String repo) {
84 this.usr = user;
85 this.rpo = repo;
86 }
87
88
89
90
91
92 public Simple(final String mnemo) {
93 final String[] parts = mnemo.split(Coordinates.SEPARATOR, 2);
94 if (parts.length != 2) {
95 throw new IllegalArgumentException(
96 String.format("invalid coordinates '%s'", mnemo)
97 );
98 }
99 this.usr = parts[0];
100 this.rpo = parts[1];
101 }
102
103 @Override
104 public String toString() {
105 return String.format("%s/%s", this.usr, this.rpo);
106 }
107
108 @Override
109 public String user() {
110 return this.usr;
111 }
112
113 @Override
114 public String repo() {
115 return this.rpo;
116 }
117
118 @Override
119 public int compareTo(final Coordinates other) {
120 return new CompareToBuilder()
121 .append(this.usr, other.user())
122 .append(this.rpo, other.repo())
123 .build();
124 }
125 }
126
127
128
129
130
131 @Immutable
132 @EqualsAndHashCode
133 final class Https implements Coordinates {
134
135
136
137
138 private static final String DOMAIN = "https://github.com/";
139
140
141
142
143 private final String url;
144
145
146
147
148
149 public Https(final String https) {
150 this.url = https;
151 }
152
153 @Override
154 public String user() {
155 return this.split()[0];
156 }
157
158 @Override
159 public String repo() {
160 final String repo = this.split()[1];
161 final String suffix = ".git";
162 if (repo.endsWith(suffix)) {
163 return repo.substring(0, repo.length() - suffix.length());
164 } else {
165 return repo;
166 }
167 }
168
169 @Override
170 public int compareTo(final Coordinates other) {
171 return new CompareToBuilder()
172 .append(this.user(), other.user())
173 .append(this.repo(), other.repo())
174 .build();
175 }
176
177
178
179
180
181 private String[] split() {
182 if (!this.url.startsWith(Https.DOMAIN)) {
183 throw new IllegalArgumentException(
184 String.format(
185 "Invalid URL, the '%s' should start with '%s'",
186 this.url,
187 Https.DOMAIN
188 )
189 );
190 }
191 return this.url.substring(Https.DOMAIN.length())
192 .split(Coordinates.SEPARATOR, 2);
193 }
194 }
195 }