|
1 |
| -/* |
2 |
| - * Copyright 2012-2015 the original author or authors. |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
15 |
| - */ |
16 |
| -package org.springframework.hateoas; |
17 |
| - |
18 |
| -import java.util.ArrayList; |
19 |
| -import java.util.Arrays; |
20 |
| -import java.util.List; |
21 |
| - |
22 |
| -import javax.xml.bind.annotation.XmlElement; |
23 |
| - |
24 |
| -import org.springframework.util.Assert; |
25 |
| - |
26 |
| -import com.fasterxml.jackson.annotation.JsonIgnore; |
27 |
| -import com.fasterxml.jackson.annotation.JsonProperty; |
28 |
| - |
29 |
| -/** |
30 |
| - * Base class for DTOs to collect links. |
31 |
| - * |
32 |
| - * @author Oliver Gierke |
33 |
| - */ |
34 |
| -public class ResourceSupport implements Identifiable<Link> { |
35 |
| - |
36 |
| - private final List<Link> links; |
37 |
| - |
38 |
| - public ResourceSupport() { |
39 |
| - this.links = new ArrayList<Link>(); |
40 |
| - } |
41 |
| - |
42 |
| - /** |
43 |
| - * Returns the {@link Link} with a rel of {@link Link#REL_SELF}. |
44 |
| - */ |
45 |
| - @JsonIgnore |
46 |
| - public Link getId() { |
47 |
| - return getLink(Link.REL_SELF); |
48 |
| - } |
49 |
| - |
50 |
| - /** |
51 |
| - * Adds the given link to the resource. |
52 |
| - * |
53 |
| - * @param link |
54 |
| - */ |
55 |
| - public void add(Link link) { |
56 |
| - Assert.notNull(link, "Link must not be null!"); |
57 |
| - this.links.add(link); |
58 |
| - } |
59 |
| - |
60 |
| - /** |
61 |
| - * Adds all given {@link Link}s to the resource. |
62 |
| - * |
63 |
| - * @param links |
64 |
| - */ |
65 |
| - public void add(Iterable<Link> links) { |
66 |
| - Assert.notNull(links, "Given links must not be null!"); |
67 |
| - for (Link candidate : links) { |
68 |
| - add(candidate); |
69 |
| - } |
70 |
| - } |
71 |
| - |
72 |
| - /** |
73 |
| - * Adds all given {@link Link}s to the resource. |
74 |
| - * |
75 |
| - * @param links must not be {@literal null}. |
76 |
| - */ |
77 |
| - public void add(Link... links) { |
78 |
| - Assert.notNull(links, "Given links must not be null!"); |
79 |
| - add(Arrays.asList(links)); |
80 |
| - } |
81 |
| - |
82 |
| - /** |
83 |
| - * Returns whether the resource contains {@link Link}s at all. |
84 |
| - * |
85 |
| - * @return |
86 |
| - */ |
87 |
| - public boolean hasLinks() { |
88 |
| - return !this.links.isEmpty(); |
89 |
| - } |
90 |
| - |
91 |
| - /** |
92 |
| - * Returns whether the resource contains a {@link Link} with the given rel. |
93 |
| - * |
94 |
| - * @param rel |
95 |
| - * @return |
96 |
| - */ |
97 |
| - public boolean hasLink(String rel) { |
98 |
| - return getLink(rel) != null; |
99 |
| - } |
100 |
| - |
101 |
| - /** |
102 |
| - * Returns all {@link Link}s contained in this resource. |
103 |
| - * |
104 |
| - * @return |
105 |
| - */ |
106 |
| - @XmlElement(name = "link", namespace = Link.ATOM_NAMESPACE) |
107 |
| - @JsonProperty("links") |
108 |
| - public List<Link> getLinks() { |
109 |
| - return links; |
110 |
| - } |
111 |
| - |
112 |
| - /** |
113 |
| - * Removes all {@link Link}s added to the resource so far. |
114 |
| - */ |
115 |
| - public void removeLinks() { |
116 |
| - this.links.clear(); |
117 |
| - } |
118 |
| - |
119 |
| - /** |
120 |
| - * Returns the link with the given rel. |
121 |
| - * |
122 |
| - * @param rel |
123 |
| - * @return the link with the given rel or {@literal null} if none found. |
124 |
| - */ |
125 |
| - public Link getLink(String rel) { |
126 |
| - |
127 |
| - for (Link link : links) { |
128 |
| - if (link.getRel().equals(rel)) { |
129 |
| - return link; |
130 |
| - } |
131 |
| - } |
132 |
| - |
133 |
| - return null; |
134 |
| - } |
135 |
| - |
136 |
| - /* |
137 |
| - * (non-Javadoc) |
138 |
| - * @see java.lang.Object#toString() |
139 |
| - */ |
140 |
| - @Override |
141 |
| - public String toString() { |
142 |
| - return String.format("links: %s", links.toString()); |
143 |
| - } |
144 |
| - |
145 |
| - /* |
146 |
| - * (non-Javadoc) |
147 |
| - * @see java.lang.Object#equals(java.lang.Object) |
148 |
| - */ |
149 |
| - @Override |
150 |
| - public boolean equals(Object obj) { |
151 |
| - |
152 |
| - if (this == obj) { |
153 |
| - return true; |
154 |
| - } |
155 |
| - |
156 |
| - if (obj == null || !obj.getClass().equals(this.getClass())) { |
157 |
| - return false; |
158 |
| - } |
159 |
| - |
160 |
| - ResourceSupport that = (ResourceSupport) obj; |
161 |
| - |
162 |
| - return this.links.equals(that.links); |
163 |
| - } |
164 |
| - |
165 |
| - /* |
166 |
| - * (non-Javadoc) |
167 |
| - * @see java.lang.Object#hashCode() |
168 |
| - */ |
169 |
| - @Override |
170 |
| - public int hashCode() { |
171 |
| - return this.links.hashCode(); |
172 |
| - } |
173 |
| -} |
| 1 | +/* |
| 2 | + * Copyright 2012-2015 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.hateoas; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import javax.xml.bind.annotation.XmlElement; |
| 23 | + |
| 24 | +import org.springframework.util.Assert; |
| 25 | + |
| 26 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 27 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 28 | + |
| 29 | +/** |
| 30 | + * Base class for DTOs to collect links. |
| 31 | + * |
| 32 | + * @author Oliver Gierke |
| 33 | + */ |
| 34 | +public class ResourceSupport implements Identifiable<Link> { |
| 35 | + |
| 36 | + private final List<Link> links; |
| 37 | + |
| 38 | + public ResourceSupport() { |
| 39 | + this.links = new ArrayList<Link>(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns the {@link Link} with a rel of {@link Link#REL_SELF}. |
| 44 | + */ |
| 45 | + @JsonIgnore |
| 46 | + public Link getId() { |
| 47 | + return getLink(Link.REL_SELF); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Adds the given link to the resource. |
| 52 | + * |
| 53 | + * @param link |
| 54 | + */ |
| 55 | + public void add(Link link) { |
| 56 | + Assert.notNull(link, "Link must not be null!"); |
| 57 | + this.links.add(link); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Adds all given {@link Link}s to the resource. |
| 62 | + * |
| 63 | + * @param links |
| 64 | + */ |
| 65 | + public void add(Iterable<Link> links) { |
| 66 | + Assert.notNull(links, "Given links must not be null!"); |
| 67 | + for (Link candidate : links) { |
| 68 | + add(candidate); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Adds all given {@link Link}s to the resource. |
| 74 | + * |
| 75 | + * @param links must not be {@literal null}. |
| 76 | + */ |
| 77 | + public void add(Link... links) { |
| 78 | + Assert.notNull(links, "Given links must not be null!"); |
| 79 | + add(Arrays.asList(links)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns whether the resource contains {@link Link}s at all. |
| 84 | + * |
| 85 | + * @return |
| 86 | + */ |
| 87 | + public boolean hasLinks() { |
| 88 | + return !this.links.isEmpty(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Returns whether the resource contains a {@link Link} with the given rel. |
| 93 | + * |
| 94 | + * @param rel |
| 95 | + * @return |
| 96 | + */ |
| 97 | + public boolean hasLink(String rel) { |
| 98 | + return getLink(rel) != null; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Returns all {@link Link}s contained in this resource. |
| 103 | + * |
| 104 | + * @return |
| 105 | + */ |
| 106 | + @XmlElement(name = "link", namespace = Link.ATOM_NAMESPACE) |
| 107 | + @JsonProperty("links") |
| 108 | + public List<Link> getLinks() { |
| 109 | + return links; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Removes all {@link Link}s added to the resource so far. |
| 114 | + */ |
| 115 | + public void removeLinks() { |
| 116 | + this.links.clear(); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the link with the given rel. |
| 121 | + * |
| 122 | + * @param rel |
| 123 | + * @return the link with the given rel or {@literal null} if none found. |
| 124 | + */ |
| 125 | + public Link getLink(String rel) { |
| 126 | + |
| 127 | + for (Link link : links) { |
| 128 | + if (link.getRel().equals(rel)) { |
| 129 | + return link; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns all {@link Link}s with the given relation type. |
| 138 | + * |
| 139 | + * @return the links in a {@link List} |
| 140 | + */ |
| 141 | + public List<Link> getLinks(String rel) { |
| 142 | + |
| 143 | + List<Link> relatedLinks = new ArrayList<Link>(); |
| 144 | + |
| 145 | + for (Link link : links) { |
| 146 | + if (link.getRel().equals(rel)) { |
| 147 | + relatedLinks.add(link); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return relatedLinks; |
| 152 | + } |
| 153 | + |
| 154 | + /* |
| 155 | + * (non-Javadoc) |
| 156 | + * @see java.lang.Object#toString() |
| 157 | + */ |
| 158 | + @Override |
| 159 | + public String toString() { |
| 160 | + return String.format("links: %s", links.toString()); |
| 161 | + } |
| 162 | + |
| 163 | + /* |
| 164 | + * (non-Javadoc) |
| 165 | + * @see java.lang.Object#equals(java.lang.Object) |
| 166 | + */ |
| 167 | + @Override |
| 168 | + public boolean equals(Object obj) { |
| 169 | + |
| 170 | + if (this == obj) { |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
| 174 | + if (obj == null || !obj.getClass().equals(this.getClass())) { |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + ResourceSupport that = (ResourceSupport) obj; |
| 179 | + |
| 180 | + return this.links.equals(that.links); |
| 181 | + } |
| 182 | + |
| 183 | + /* |
| 184 | + * (non-Javadoc) |
| 185 | + * @see java.lang.Object#hashCode() |
| 186 | + */ |
| 187 | + @Override |
| 188 | + public int hashCode() { |
| 189 | + return this.links.hashCode(); |
| 190 | + } |
| 191 | +} |
0 commit comments