View Javadoc

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