From d1a06bbcc8d3d87a61ef8015c44bbadd20e3973e Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 2 Jul 2019 20:05:07 +0200 Subject: [PATCH] #1014 - Added API to conditionally add links to RepresentationModel. This allows more fluent usage of the API. --- .../hateoas/RepresentationModel.java | 36 +++++++++++++++++++ .../hateoas/RepresentationModelUnitTest.java | 24 +++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/main/java/org/springframework/hateoas/RepresentationModel.java b/src/main/java/org/springframework/hateoas/RepresentationModel.java index f8577b56..9939d5df 100755 --- a/src/main/java/org/springframework/hateoas/RepresentationModel.java +++ b/src/main/java/org/springframework/hateoas/RepresentationModel.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; +import java.util.function.Supplier; import java.util.stream.Collectors; import org.springframework.lang.Nullable; @@ -102,6 +103,41 @@ public class RepresentationModel> { return (T) this; } + /** + * Adds the {@link Link} produced by the given Supplier if the guard is {@literal true}. + * + * @param guard whether to add the {@link Link} produced by the given {@link Supplier}. + * @param link the {@link Link} to add in case the guard is {@literal true}. + * @return + */ + @SuppressWarnings("unchecked") + public T addIf(boolean guard, Supplier link) { + + if (guard) { + add(link.get()); + } + + return (T) this; + } + + /** + * Adds all {@link Link}s produced by the given Supplier if the guard is {@literal true}. + * + * @param guard whether to add the {@link Link}s produced by the given {@link Supplier}. + * @param links the {@link Link}s to add in case the guard is {@literal true}. + * @return + * @see Links + */ + @SuppressWarnings("unchecked") + public T addAllIf(boolean guard, Supplier> links) { + + if (guard) { + add(links.get()); + } + + return (T) this; + } + /** * Returns whether the resource contains {@link Link}s at all. * diff --git a/src/test/java/org/springframework/hateoas/RepresentationModelUnitTest.java b/src/test/java/org/springframework/hateoas/RepresentationModelUnitTest.java index cf0e62bb..b7badfcf 100755 --- a/src/test/java/org/springframework/hateoas/RepresentationModelUnitTest.java +++ b/src/test/java/org/springframework/hateoas/RepresentationModelUnitTest.java @@ -170,4 +170,28 @@ class RepresentationModelUnitTest { assertThat(support.hasLink("self")).isTrue(); assertThat(support.hasLink("another")).isTrue(); } + + @Test // #1014 + void addsGuardedLink() { + + RepresentationModel model = new RepresentationModel<>(); + + model.addIf(true, () -> new Link("added", "foo")); + assertThat(model.hasLink("foo")).isTrue(); + + model.addIf(false, () -> new Link("not-added", "bar")); + assertThat(model.hasLink("bar")).isFalse(); + } + + @Test // #1014 + void addsGuardedLinks() { + + RepresentationModel model = new RepresentationModel<>(); + + model.addAllIf(true, () -> Links.of(new Link("added", "foo"))); + assertThat(model.hasLink("foo")).isTrue(); + + model.addAllIf(false, () -> Links.of(new Link("not-added", "bar"))); + assertThat(model.hasLink("bar")).isFalse(); + } }