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