Skip to content

Commit 79ebf9b

Browse files
committed
#671 - Polishing.
Introduced Link.hasRel(…) and use that to filter link lookups in ResourceSupport.
1 parent d40bda2 commit 79ebf9b

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/main/java/org/springframework/hateoas/Link.java

+13
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ public Link expand(Map<String, ? extends Object> arguments) {
168168
return new Link(getUriTemplate().expand(arguments).toString(), getRel());
169169
}
170170

171+
/**
172+
* Returns whether the current {@link Link} has the given link relation.
173+
*
174+
* @param rel must not be {@literal null} or empty.
175+
* @return
176+
*/
177+
public boolean hasRel(String rel) {
178+
179+
Assert.hasText(rel, "Link relation must not be null or empty!");
180+
181+
return this.rel.equals(rel);
182+
}
183+
171184
private UriTemplate getUriTemplate() {
172185

173186
if (template == null) {

src/main/java/org/springframework/hateoas/ResourceSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void removeLinks() {
126126
public Optional<Link> getLink(String rel) {
127127

128128
return links.stream() //
129-
.filter(link -> link.getRel().equals(rel)) //
129+
.filter(link -> link.hasRel(rel)) //
130130
.findFirst();
131131
}
132132

@@ -151,7 +151,7 @@ public Link getRequiredLink(String rel) {
151151
public List<Link> getLinks(String rel) {
152152

153153
return links.stream() //
154-
.filter(link -> link.getRel().equals(rel)) //
154+
.filter(link -> link.hasRel(rel)) //
155155
.collect(Collectors.toList());
156156
}
157157

src/test/java/org/springframework/hateoas/LinkUnitTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,28 @@ public void parsesUriLinkRelations() {
223223
assertThat(Link.valueOf("<http://localhost>; rel=\"http://acme.com/rels/foo-bar\"").getRel()) //
224224
.isEqualTo("http://acme.com/rels/foo-bar");
225225
}
226+
227+
/**
228+
* @see #671
229+
*/
230+
@Test
231+
public void exposesLinkRelation() {
232+
233+
Link link = new Link("/", "foo");
234+
235+
assertThat(link.hasRel("foo")).isTrue();
236+
assertThat(link.hasRel("bar")).isFalse();
237+
}
238+
239+
/**
240+
* @see #671
241+
*/
242+
@Test
243+
public void rejectsInvalidRelationsOnHasRel() {
244+
245+
Link link = new Link("/");
246+
247+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> link.hasRel(null));
248+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> link.hasRel(""));
249+
}
226250
}

0 commit comments

Comments
 (0)