1 package com.atlassian.plugins.rest.common;
2
3 import com.google.common.base.Preconditions;
4 import org.apache.commons.lang.builder.EqualsBuilder;
5 import org.apache.commons.lang.builder.HashCodeBuilder;
6
7 import javax.xml.bind.annotation.XmlAttribute;
8 import javax.xml.bind.annotation.XmlRootElement;
9 import java.net.URI;
10
11
12
13
14
15
16 @XmlRootElement
17 public class Link {
18 @XmlAttribute
19 private final URI href;
20
21 @XmlAttribute
22 private final String type;
23
24 @XmlAttribute
25 private final String rel;
26
27
28
29 private Link() {
30 this.href = null;
31 this.rel = null;
32 this.type = null;
33 }
34
35 private Link(URI href, String rel, String type) {
36 this.href = Preconditions.checkNotNull(href);
37 this.rel = Preconditions.checkNotNull(rel);
38 this.type = type;
39 }
40
41
42
43
44
45
46
47
48 public static Link link(URI uri, String rel) {
49 return new Link(uri, rel, null);
50 }
51
52
53
54
55
56
57
58
59
60 public static Link link(URI uri, String rel, String type) {
61 return new Link(uri, rel, type);
62 }
63
64
65
66
67
68
69
70
71 public static Link self(URI uri) {
72 return link(uri, "self");
73 }
74
75
76
77
78
79
80
81
82 public static Link edit(URI uri) {
83 return link(uri, "edit");
84 }
85
86
87
88
89
90
91
92
93 public static Link add(URI uri) {
94 return link(uri, "add");
95 }
96
97
98
99
100
101
102
103
104 public static Link delete(URI uri) {
105 return link(uri, "delete");
106 }
107
108 public URI getHref() {
109 return href;
110 }
111
112 public String getRel() {
113 return rel;
114 }
115
116 @Override
117 public int hashCode() {
118 return new HashCodeBuilder(3, 7).append(href).append(rel).toHashCode();
119 }
120
121 @Override
122 public boolean equals(Object obj) {
123 if (obj == null) {
124 return false;
125 }
126 if (obj == this) {
127 return true;
128 }
129 if (obj.getClass() != getClass()) {
130 return false;
131 }
132 final Link link = (Link) obj;
133 return new EqualsBuilder().append(href, link.href).append(rel, link.rel).isEquals();
134 }
135
136 @Override
137 public String toString() {
138 return "Link{" +
139 "href=" + href +
140 ", type='" + type + '\'' +
141 ", rel='" + rel + '\'' +
142 '}';
143 }
144 }