From 3c62de720f7f7a21a885e179cf5876ae6b725f8c Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 21 Jan 2014 14:27:17 +0100 Subject: [PATCH] #137 - Improved Link and UriTemplate API. Added constructor to Link to take a UriTemplate. Added constructor to UriTemplate to take a base URI and TemplateVariables. --- .../java/org/springframework/hateoas/Link.java | 16 +++++++++++++--- .../org/springframework/hateoas/UriTemplate.java | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/hateoas/Link.java b/src/main/java/org/springframework/hateoas/Link.java index 40447bfe..fc4c7c39 100755 --- a/src/main/java/org/springframework/hateoas/Link.java +++ b/src/main/java/org/springframework/hateoas/Link.java @@ -71,13 +71,23 @@ public class Link implements Serializable { * @param rel must not be {@literal null} or empty. */ public Link(String href, String rel) { + this(new UriTemplate(href), rel); + } - Assert.hasText(href, "Href must not be null or empty!"); + /** + * Creates a new Link from the given {@link UriTemplate} and rel. + * + * @param template must not be {@literal null}. + * @param rel must not be {@literal null} or empty. + */ + public Link(UriTemplate template, String rel) { + + Assert.notNull(template, "UriTempalte must not be null!"); Assert.hasText(rel, "Rel must not be null or empty!"); - this.href = href; + this.template = template; + this.href = template.toString(); this.rel = rel; - this.template = new UriTemplate(href); } /** diff --git a/src/main/java/org/springframework/hateoas/UriTemplate.java b/src/main/java/org/springframework/hateoas/UriTemplate.java index 6e2c68bb..74efd6bc 100644 --- a/src/main/java/org/springframework/hateoas/UriTemplate.java +++ b/src/main/java/org/springframework/hateoas/UriTemplate.java @@ -76,6 +76,20 @@ public class UriTemplate implements Iterable { this.baseUri = template.substring(0, baseUriEndIndex); } + /** + * Creates a new {@link UriTemplate} from the given base URI and {@link TemplateVariables}. + * + * @param baseUri must not be {@literal null} or empty. + * @param variables defaults to {@link TemplateVariables#NONE}. + */ + public UriTemplate(String baseUri, TemplateVariables variables) { + + Assert.hasText("Base URI must not be null or empty!"); + + this.baseUri = baseUri; + this.variables = variables == null ? TemplateVariables.NONE : variables; + } + /** * Returns whether the given candidate is a URI template. *