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 {
19 @XmlAttribute
20 private final URI href;
21
22 @XmlAttribute
23 private final String type;
24
25 @XmlAttribute
26 private final String rel;
27
28
29
30 private Link()
31 {
32 this.href = null;
33 this.rel = null;
34 this.type = null;
35 }
36
37 private Link(URI href, String rel, String type)
38 {
39 this.href = Preconditions.checkNotNull(href);
40 this.rel = Preconditions.checkNotNull(rel);
41 this.type = type;
42 }
43
44
45
46
47
48
49
50
51 public static Link link(URI uri, String rel)
52 {
53 return new Link(uri, rel, null);
54 }
55
56
57
58
59
60
61
62
63
64 public static Link link(URI uri, String rel, String type)
65 {
66 return new Link(uri, rel, type);
67 }
68
69
70
71
72
73
74
75
76 public static Link self(URI uri)
77 {
78 return link(uri, "self");
79 }
80
81
82
83
84
85
86
87
88 public static Link edit(URI uri)
89 {
90 return link(uri, "edit");
91 }
92
93
94
95
96
97
98
99
100 public static Link add(URI uri)
101 {
102 return link(uri, "add");
103 }
104
105
106
107
108
109
110
111
112 public static Link delete(URI uri)
113 {
114 return link(uri, "delete");
115 }
116
117 public URI getHref()
118 {
119 return href;
120 }
121
122 public String getRel()
123 {
124 return rel;
125 }
126
127 @Override
128 public int hashCode()
129 {
130 return new HashCodeBuilder(3, 7).append(href).append(rel).toHashCode();
131 }
132
133 @Override
134 public boolean equals(Object obj)
135 {
136 if (obj == null)
137 {
138 return false;
139 }
140 if (obj == this)
141 {
142 return true;
143 }
144 if (obj.getClass() != getClass())
145 {
146 return false;
147 }
148 final Link link = (Link) obj;
149 return new EqualsBuilder().append(href, link.href).append(rel, link.rel).isEquals();
150 }
151 }