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   * Represents a link to a given entity. The {@link #href}
13   * @since 1.0
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      // For JAXB's usage
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       * Creates a link using the specified uri and rel values
44       * @param uri the {@link URI} to use for the link URI,
45       * @param rel the rel attribute of the link.
46       * @return a link
47       */
48      public static Link link(URI uri, String rel)
49      {
50          return new Link(uri, rel, null);
51      }
52  
53      /**
54       * Creates a link using the given URI builder to build the URI.
55       * @param uri the {@link URI} to use for the link URI,
56       * @param rel the rel attribute of the link.
57       * @param type the type attribute of the link.
58       * @return a link
59       */
60      public static Link link(URI uri, String rel, String type)
61      {
62          return new Link(uri, rel, type);
63      }
64  
65      /**
66       * Creates a link using the given URI builder to build the URI.
67       * The {@code rel} attribute of the link is set to {@code self}.
68       * @param uri the {@link URI} to use for the link URI.
69       * @return a link
70       */
71      public static Link self(URI uri)
72      {
73          return link(uri, "self");
74      }
75  
76      /**
77       * Creates a link using the given URI builder to build the URI.
78       * The {@code rel} attribute of the link is set to {@code edit}.
79       * @param uri the {@link URI} to use for the link URI.
80       * @return a link
81       */
82      public static Link edit(URI uri)
83      {
84          return link(uri, "edit");
85      }
86  
87      /**
88       * Creates a link using the given URI builder to build the URI.
89       * The {@code rel} attribute of the link is set to {@code add}.
90       * @param uri the {@link URI} to use for the link URI.
91       * @return a link
92       */
93      public static Link add(URI uri)
94      {
95          return link(uri, "add");
96      }
97  
98      /**
99       * Creates a link using the given URI builder to build the URI.
100      * The {@code rel} attribute of the link is set to {@code delete}.
101      * @param uri the {@link URI} to use for the link URI.
102      * @return a link
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 }