Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Coordinates |
|
| 1.25;1.25 | ||||
Coordinates$Simple |
|
| 1.25;1.25 |
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 lombok.EqualsAndHashCode; | |
34 | import org.apache.commons.lang3.builder.CompareToBuilder; | |
35 | ||
36 | /** | |
37 | * Repository coordinates. | |
38 | * | |
39 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
40 | * @version $Id: ed080c80fe93715906fb087b7b7142ffecf6fb06 $ | |
41 | * @since 0.1 | |
42 | */ | |
43 | @Immutable | |
44 | public interface Coordinates extends Comparable<Coordinates> { | |
45 | ||
46 | /** | |
47 | * Get usr name. | |
48 | * @return User name | |
49 | */ | |
50 | String user(); | |
51 | ||
52 | /** | |
53 | * Get rpo name. | |
54 | * @return Repo name | |
55 | */ | |
56 | String repo(); | |
57 | ||
58 | /** | |
59 | * Jcabi.http implementation. | |
60 | */ | |
61 | 3 | @Immutable |
62 | 24 | @EqualsAndHashCode(of = { "usr", "rpo" }) |
63 | final class Simple implements Coordinates { | |
64 | /** | |
65 | * User name. | |
66 | */ | |
67 | private final transient String usr; | |
68 | /** | |
69 | * Repository name. | |
70 | */ | |
71 | private final transient String rpo; | |
72 | /** | |
73 | * Public ctor. | |
74 | * @param user User name | |
75 | * @param repo Repository name | |
76 | */ | |
77 | 355 | public Simple(final String user, final String repo) { |
78 | 362 | this.usr = user; |
79 | 362 | this.rpo = repo; |
80 | 362 | } |
81 | /** | |
82 | * Public ctor. | |
83 | * @param mnemo Mnemo name | |
84 | */ | |
85 | 10 | public Simple(final String mnemo) { |
86 | 10 | final String[] parts = mnemo.split("/", 2); |
87 | 10 | if (parts.length != 2) { |
88 | 0 | throw new IllegalArgumentException( |
89 | 0 | String.format("invalid coordinates '%s'", mnemo) |
90 | ); | |
91 | } | |
92 | 10 | this.usr = parts[0]; |
93 | 10 | this.rpo = parts[1]; |
94 | 10 | } |
95 | @Override | |
96 | public String toString() { | |
97 | 2627 | return String.format("%s/%s", this.usr, this.rpo); |
98 | } | |
99 | @Override | |
100 | public String user() { | |
101 | 290 | return this.usr; |
102 | } | |
103 | @Override | |
104 | public String repo() { | |
105 | 270 | return this.rpo; |
106 | } | |
107 | @Override | |
108 | public int compareTo(final Coordinates other) { | |
109 | 6 | return new CompareToBuilder() |
110 | 3 | .append(this.usr, other.user()) |
111 | 3 | .append(this.rpo, other.repo()) |
112 | 3 | .build(); |
113 | } | |
114 | } | |
115 | } |