Class Link

java.lang.Object
org.springframework.hateoas.Link
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
DefaultCurieProvider.Curie, LinkMixin

public class Link extends Object implements Serializable
Value object for links.
Author:
Oliver Gierke, Greg Turnquist, Jens Schauder
See Also:
  • Field Details

  • Constructor Details

    • Link

      protected Link(String href, LinkRelation rel)
      Creates a new Link to the given URI with the given rel.
      Parameters:
      href - must not be null or empty.
      rel - must not be null or empty.
    • Link

      protected Link(UriTemplate template, LinkRelation rel)
      Creates a new Link from the given UriTemplate and rel.
      Parameters:
      template - must not be null.
      rel - must not be null or empty.
    • Link

      protected Link()
      Empty constructor required by the marshaling framework.
  • Method Details

    • of

      public static Link of(String href)
      Creates a new link to the given URI with the self relation.
      Parameters:
      href - must not be null or empty.
      Returns:
      Since:
      1.1
      See Also:
    • of

      public static Link of(String href, String relation)
      Creates a new Link to the given href with the given relation.
      Parameters:
      href - must not be null or empty.
      relation - must not be null or empty.
      Returns:
      Since:
      1.1
    • of

      public static Link of(String href, LinkRelation relation)
      Creates a new Link to the given href and LinkRelation.
      Parameters:
      href - must not be null or empty.
      relation - must not be null.
      Returns:
      Since:
      1.1
    • of

      public static Link of(UriTemplate template, String relation)
      Creates a new Link to the given UriTemplate and link relation.
      Parameters:
      template - must not be null.
      relation - must not be null or empty.
      Returns:
      Since:
      1.1
    • of

      public static Link of(UriTemplate template, LinkRelation relation)
      Creates a new Link to the given UriTemplate and LinkRelation.
      Parameters:
      template - must not be null.
      relation - must not be null.
      Returns:
      Since:
      1.1
    • getAffordances

      public List<Affordance> getAffordances()
      Returns safe copy of Affordances.
      Returns:
    • withSelfRel

      public Link withSelfRel()
      Returns a Link pointing to the same URI but with the self relation.
      Returns:
    • andAffordance

      public Link andAffordance(Affordance affordance)
      Create new Link with an additional Affordance.
      Parameters:
      affordance - must not be null.
      Returns:
    • andAffordances

      public Link andAffordances(List<Affordance> affordances)
      Create new Link with additional Affordances.
      Parameters:
      affordances - must not be null.
      Returns:
    • withAffordances

      public Link withAffordances(List<Affordance> affordances)
      Creats a new Link with the given Affordances.
      Parameters:
      affordances - must not be null.
      Returns:
      will never be null.
    • getVariableNames

      public List<String> getVariableNames()
      Returns the variable names contained in the template.
      Returns:
    • getVariables

      public List<TemplateVariable> getVariables()
      Returns all TemplateVariables contained in the Link.
      Returns:
    • isTemplated

      public boolean isTemplated()
      Returns whether or not the link is templated.
      Returns:
    • expand

      public Link expand(Object... arguments)
      Turns the current template into a Link by expanding it using the given parameters.
      Parameters:
      arguments -
      Returns:
    • expand

      public Link expand(Map<String,?> arguments)
      Turns the current template into a Link by expanding it using the given parameters.
      Parameters:
      arguments - must not be null.
      Returns:
    • withRel

      public Link withRel(LinkRelation relation)
      Creates a new Link with the same href but given LinkRelation.
      Parameters:
      relation - must not be null.
      Returns:
      will never be null.
    • withRel

      public Link withRel(String relation)
      Creates a new Link with the same href but given LinkRelation.
      Parameters:
      relation - must not be null or empty.
      Returns:
      will never be null.
    • hasRel

      public boolean hasRel(String rel)
      Returns whether the current Link has the given link relation.
      Parameters:
      rel - must not be null or empty.
      Returns:
    • hasRel

      public boolean hasRel(LinkRelation rel)
      Returns whether the Link has the given LinkRelation.
      Parameters:
      rel - must not be null.
      Returns:
    • toUri

      public URI toUri()
      Returns the current href as URI after expanding the links without any arguments, i.e. all optional URI TemplateVariables will be dropped. If the href contains mandatory TemplateVariables, the URI creation will fail with an IllegalStateException.
      Returns:
      will never be null.
      Throws:
      IllegalStateException - in case the href contains mandatory URI TemplateVariables.
    • valueOf

      public static Link valueOf(String element)
      Factory method to easily create Link instances from RFC-8288 compatible String representations of a link.
      Parameters:
      element - an RFC-8288 compatible representation of a link.
      Returns:
      Throws:
      IllegalArgumentException - if a String was given that does not adhere to RFC-8288.
      IllegalArgumentException - if no rel attribute could be found.
    • withHref

      public Link withHref(String href)
      Create a new Link by copying all attributes and applying the new href.
      Parameters:
      href - can be null
      Returns:
      will never be null.
    • withHreflang

      public Link withHreflang(@Nullable String hreflang)
      Create a new Link by copying all attributes and applying the new hrefleng.
      Parameters:
      hreflang - can be null
      Returns:
      will never be null.
    • withMedia

      public Link withMedia(@Nullable String media)
      Create a new Link by copying all attributes and applying the new media.
      Parameters:
      media - can be null
      Returns:
      will never be null.
    • withTitle

      public Link withTitle(@Nullable String title)
      Create a new Link by copying all attributes and applying the new title.
      Parameters:
      title - can be null
      Returns:
      will never be null.
    • withType

      public Link withType(@Nullable String type)
      Create a new Link by copying all attributes and applying the new type.
      Parameters:
      type - can be null
      Returns:
      will never be null.
    • withDeprecation

      public Link withDeprecation(@Nullable String deprecation)
      Create a new Link by copying all attributes and applying the new deprecation.
      Parameters:
      deprecation - can be null
      Returns:
      will never be null.
    • withProfile

      public Link withProfile(@Nullable String profile)
      Create a new Link by copying all attributes and applying the new profile.
      Parameters:
      profile - can be null
      Returns:
      will never be null.
    • withName

      public Link withName(@Nullable String name)
      Create a new Link by copying all attributes and applying the new name.
      Parameters:
      name - can be null
      Returns:
      will never be null.
    • getRel

      public LinkRelation getRel()
    • getHref

      public String getHref()
    • getHreflang

      @Nullable public String getHreflang()
    • getMedia

      @Nullable public String getMedia()
    • getTitle

      @Nullable public String getTitle()
    • getType

      @Nullable public String getType()
    • getDeprecation

      @Nullable public String getDeprecation()
    • getProfile

      @Nullable public String getProfile()
    • getName

      @Nullable public String getName()
    • getTemplate

      public UriTemplate getTemplate()
    • equals

      public boolean equals(@Nullable Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object