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      @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  
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       * Creates a link using the specified uri and rel values
43       *
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          return new Link(uri, rel, null);
50      }
51  
52      /**
53       * Creates a link using the given URI builder to build the URI.
54       *
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          return new Link(uri, rel, type);
62      }
63  
64      /**
65       * Creates a link using the given URI builder to build the URI.
66       * The {@code rel} attribute of the link is set to {@code self}.
67       *
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          return link(uri, "self");
73      }
74  
75      /**
76       * Creates a link using the given URI builder to build the URI.
77       * The {@code rel} attribute of the link is set to {@code edit}.
78       *
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          return link(uri, "edit");
84      }
85  
86      /**
87       * Creates a link using the given URI builder to build the URI.
88       * The {@code rel} attribute of the link is set to {@code add}.
89       *
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          return link(uri, "add");
95      }
96  
97      /**
98       * Creates a link using the given URI builder to build the URI.
99       * The {@code rel} attribute of the link is set to {@code delete}.
100      *
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         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 }