#1322 - Added Links.and(boolean, Supplier<Link>) to conditionally add links.

This commit is contained in:
Oliver Drotbohm
2020-07-19 17:35:43 +02:00
parent d64d3d308c
commit 8af6b4c099
2 changed files with 46 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collector;
@@ -133,6 +134,35 @@ public class Links implements Iterable<Link> {
return and(Arrays.asList(links));
}
/**
* Adds the given links if the given condition is {@literal true}. The given {@link Supplier}s will only be resolved
* if the given condition is true. Essentially syntactic sugar to write:<br />
* <code>
* if (a > 3) {
* links = links.and(…);
* }
* </code> as <code>
* links = link.and(a > 3, …);
* </code>
*
* @param condition
* @param links must not be {@literal null}.
* @return
*/
@SafeVarargs
public final Links and(boolean condition, Supplier<Link>... links) {
Assert.notNull(links, "Links must not be null!");
if (!condition) {
return this;
}
return and(Arrays.stream(links) //
.map(Supplier::get) //
.collect(Collectors.toList()));
}
/**
* Creates a new {@link Links} instance with all given {@link Link}s added. For conditional adding see
* {@link #merge(Iterable)}.