Class RepresentationModel<T extends RepresentationModel<? extends T>>

java.lang.Object
org.springframework.hateoas.RepresentationModel<T>
Direct Known Subclasses:
CollectionModel, EntityModel, RepresentationModelMixin, VndErrors.VndError

public class RepresentationModel<T extends RepresentationModel<? extends T>> extends Object
Base class for DTOs to collect links.
Author:
Oliver Gierke, Johhny Lim, Greg Turnquist
  • Constructor Details

    • RepresentationModel

      public RepresentationModel()
    • RepresentationModel

      public RepresentationModel(Link initialLink)
    • RepresentationModel

      public RepresentationModel(Iterable<Link> initialLinks)
  • Method Details

    • of

      public static <T> RepresentationModel<?> of(@Nullable T object)
      Creates a new RepresentationModel for the given content object and no links.
      Parameters:
      object - can be null.
      Returns:
      See Also:
    • of

      public static <T> RepresentationModel<?> of(@Nullable T object, Iterable<Link> links)
      Creates a new RepresentationModel for the given content object and links. Will return a simple RepresentationModel if the content is null, a CollectionModel in case the given content object is a Collection or an EntityModel otherwise.
      Parameters:
      object - can be null.
      links - must not be null.
      Returns:
    • add

      public T add(Link link)
      Adds the given link to the resource.
      Parameters:
      link -
    • add

      public T add(Iterable<Link> links)
      Adds all given Links to the resource.
      Parameters:
      links - must not be null.
      See Also:
    • add

      public T add(Link... links)
      Adds all given Links to the resource.
      Parameters:
      links - must not be null.
    • addIf

      public T addIf(boolean guard, Supplier<Link> link)
      Adds the Link produced by the given Supplier if the guard is true.
      Parameters:
      guard - whether to add the Link produced by the given Supplier.
      link - the Link to add in case the guard is true.
      Returns:
    • addAllIf

      public T addAllIf(boolean guard, Supplier<? extends Iterable<Link>> links)
      Adds all Links produced by the given Supplier if the guard is true.
      Parameters:
      guard - whether to add the Links produced by the given Supplier.
      links - the Links to add in case the guard is true.
      Returns:
      See Also:
    • hasLinks

      public boolean hasLinks()
      Returns whether the resource contains Links at all.
      Returns:
    • hasLink

      public boolean hasLink(String rel)
      Returns whether the resource contains a Link with the given rel.
      Parameters:
      rel -
      Returns:
    • hasLink

      public boolean hasLink(LinkRelation rel)
    • getLinks

      public Links getLinks()
      Returns all Links contained in this resource.
      Returns:
    • removeLinks

      public T removeLinks()
      Removes all Links added to the resource so far.
    • getLink

      public Optional<Link> getLink(String relation)
      Returns the link with the given relation.
      Parameters:
      relation - must not be null or empty.
      Returns:
      the link with the given relation or Optional.empty() if none found.
    • getLink

      public Optional<Link> getLink(LinkRelation relation)
      Returns the link with the given LinkRelation.
      Parameters:
      relation -
      Returns:
    • getRequiredLink

      public Link getRequiredLink(String relation)
      Returns the link with the given relation.
      Parameters:
      relation - must not be null or empty.
      Returns:
      the link with the given relation.
      Throws:
      IllegalArgumentException - in case no link with the given relation can be found.
    • getRequiredLink

      public Link getRequiredLink(LinkRelation relation)
      Returns the link with the given relation.
      Parameters:
      relation - must not be null.
      Returns:
      the link with the given relation.
      Throws:
      IllegalArgumentException - in case no link with the given relation can be found.
    • getLinks

      public List<Link> getLinks(String relation)
      Returns all Links with the given relation.
      Parameters:
      relation - must not be null.
      Returns:
      the links in a List
    • getLinks

      public List<Link> getLinks(LinkRelation relation)
      Returns all Links with the given relation.
      Parameters:
      relation - must not be null.
      Returns:
      the links in a List
    • mapLink

      public T mapLink(LinkRelation relation, Function<Link,Link> mapper)
      Replaces the link(s) with the given LinkRelation with the mapper applied to each of the links.
      Parameters:
      relation - the LinkRelation to select the source link(s), must not be null.
      mapper - the Function to apply to the current link, must not be null.
      Returns:
      will never be null.
      Since:
      1.3
    • mapLinkIf

      public T mapLinkIf(boolean condition, LinkRelation relation, Function<Link,Link> mapper)
      Replaces the link(s) with the given LinkRelation with the mapper applied to each of the links if the given condition is true.
      Parameters:
      condition - the condition that needs to be true to apply the mapping.
      relation - the LinkRelation to select the source link(s), must not be null.
      mapper - the Function to apply to the current link, must not be null.
      Returns:
      will never be null.
      Since:
      1.3
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object