#64 - Extended support for PagedResources.
Changed Relation type to identify previous links to standardized value of prev. Added integration tests for HAL rendering of PagedResources.
This commit is contained in:
@@ -42,7 +42,7 @@ public class Link implements Serializable {
|
||||
|
||||
public static final String REL_SELF = "self";
|
||||
public static final String REL_FIRST = "first";
|
||||
public static final String REL_PREVIOUS = "previous";
|
||||
public static final String REL_PREVIOUS = "prev";
|
||||
public static final String REL_NEXT = "next";
|
||||
public static final String REL_LAST = "last";
|
||||
|
||||
@@ -101,6 +101,25 @@ public class Link implements Serializable {
|
||||
return rel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Link} pointing to the same URI but with the given relation.
|
||||
*
|
||||
* @param rel must not be {@literal null} or empty.
|
||||
* @return
|
||||
*/
|
||||
public Link withRel(String rel) {
|
||||
return new Link(href, rel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Link} pointing to the same URI but with the {@code self} relation.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Link withSelfRel() {
|
||||
return withRel(Link.REL_SELF);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
|
||||
@@ -87,6 +87,16 @@ public class Links implements Iterable<Link> {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the {@link Links} container contains a {@link Link} with the given rel.
|
||||
*
|
||||
* @param rel
|
||||
* @return
|
||||
*/
|
||||
public boolean hasLink(String rel) {
|
||||
return getLink(rel) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link Links} instance from the given RFC5988-compatible link format.
|
||||
*
|
||||
@@ -114,6 +124,15 @@ public class Links implements Iterable<Link> {
|
||||
return new Links(links);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the {@link Links} containter is empty.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return links.isEmpty();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
|
||||
@@ -34,6 +34,8 @@ import org.springframework.util.Assert;
|
||||
@com.fasterxml.jackson.annotation.JsonAutoDetect(fieldVisibility = com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY)
|
||||
public class PagedResources<T> extends Resources<T> {
|
||||
|
||||
public static PagedResources<?> NO_PAGE = new PagedResources<Object>();
|
||||
|
||||
@org.codehaus.jackson.annotate.JsonProperty("page")
|
||||
@com.fasterxml.jackson.annotation.JsonProperty("page")
|
||||
private PageMetadata metadata;
|
||||
@@ -97,13 +99,37 @@ public class PagedResources<T> extends Resources<T> {
|
||||
return new PagedResources<T>(resources, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Link pointing to the next page (if set).
|
||||
*
|
||||
* @see #addPaginationLinks(Link)
|
||||
* @return
|
||||
*/
|
||||
@com.fasterxml.jackson.annotation.JsonIgnore
|
||||
@org.codehaus.jackson.annotate.JsonIgnore
|
||||
public Link getNextLink() {
|
||||
return getLink(Link.REL_NEXT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Link pointing to the previous page (if set).
|
||||
*
|
||||
* @see #addPaginationLinks(Link)
|
||||
* @return
|
||||
*/
|
||||
@com.fasterxml.jackson.annotation.JsonIgnore
|
||||
@org.codehaus.jackson.annotate.JsonIgnore
|
||||
public Link getPreviousLink() {
|
||||
return getLink(Link.REL_PREVIOUS);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.ResourceSupport#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("PagedResource { content: %s, metadata: %s }", getContent(), metadata);
|
||||
return String.format("PagedResource { content: %s, metadata: %s, links: %s }", getContent(), metadata, getLinks());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -173,19 +199,35 @@ public class PagedResources<T> extends Resources<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link PageMetadata} from the given size, number, total elements and total pages.
|
||||
*
|
||||
* @param size
|
||||
* @param number
|
||||
* @param number zero-indexed, must be less than totalPages
|
||||
* @param totalElements
|
||||
* @param totalPages
|
||||
*/
|
||||
public PageMetadata(long size, long number, long totalElements, long totalPages) {
|
||||
|
||||
Assert.isTrue(number < totalPages,
|
||||
"The current page number cannot be greater or equal to the total number of pages!");
|
||||
|
||||
this.size = size;
|
||||
this.number = number;
|
||||
this.totalElements = totalElements;
|
||||
this.totalPages = totalPages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link PageMetadata} from the given size, numer and total elements.
|
||||
*
|
||||
* @param size the size of the page
|
||||
* @param number the number of the page
|
||||
* @param totalElements the total number of elements available
|
||||
*/
|
||||
public PageMetadata(long size, long number, long totalElements) {
|
||||
this(size, number, totalElements, totalElements / size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the requested size of the page.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user