Polished Resources abstraction.
Added correct equals(…)/hashCode() methods to PagedResource, PageMetadata. Polished toString() methods of ResourceSupport, Resource, Resources and PagedResources. Added factory method to PagedResources to easily create a plain instance from a set of entities.
This commit is contained in:
@@ -15,30 +15,117 @@
|
||||
*/
|
||||
package org.springframework.hateoas;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import org.codehaus.jackson.annotate.JsonAutoDetect;
|
||||
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
|
||||
import org.codehaus.jackson.annotate.JsonProperty;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* DTO to implement binding response representations of pageable collections.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@XmlRootElement(name = "entities")
|
||||
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
|
||||
public class PagedResources<T extends Resource<?>> extends Resources<T> {
|
||||
public class PagedResources<T> extends Resources<T> {
|
||||
|
||||
@JsonProperty("page")
|
||||
private PageMetadata pageMetadata;
|
||||
private PageMetadata metadata;
|
||||
|
||||
/**
|
||||
* Default constructor to allow instantiation by reflection.
|
||||
*/
|
||||
protected PagedResources() {
|
||||
this(new ArrayList<T>(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link PagedResources} from the given content and {@link PageMetadata}.
|
||||
*
|
||||
* @param content must not be {@literal null}.
|
||||
* @param metadata
|
||||
* @param links
|
||||
*/
|
||||
public PagedResources(Collection<T> content, PageMetadata metadata, Link... links) {
|
||||
super(content, links);
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the pagination metadata.
|
||||
*
|
||||
* @return the pageMetadata
|
||||
* @return the metadata
|
||||
*/
|
||||
public PageMetadata getMetadata() {
|
||||
return pageMetadata;
|
||||
return metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to easily create a {@link PagedResources} instance from a set of entities and pagination metadata.
|
||||
*
|
||||
* @param content must not be {@literal null}.
|
||||
* @param metadata
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T extends Resource<S>, S> PagedResources<T> fromEntities(Iterable<S> content, PageMetadata metadata) {
|
||||
|
||||
Assert.notNull(content);
|
||||
ArrayList<T> resources = new ArrayList<T>();
|
||||
|
||||
for (S element : content) {
|
||||
resources.add((T) new Resource<S>(element));
|
||||
}
|
||||
|
||||
return new PagedResources<T>(resources, metadata);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.ResourceSupport#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("PagedResource { content: %s, metadata: %s }", getContent(), metadata);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.Resources#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (obj == null || !getClass().equals(obj.getClass())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PagedResources<?> that = (PagedResources<?>) obj;
|
||||
boolean metadataEquals = this.metadata == null ? that.metadata == null : this.metadata.equals(that.metadata);
|
||||
|
||||
return metadataEquals ? super.equals(obj) : false;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.Resources#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = super.hashCode();
|
||||
result += this.metadata == null ? 0 : 31 * this.metadata.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,6 +152,24 @@ public class PagedResources<T extends Resource<?>> extends Resources<T> {
|
||||
@JsonProperty
|
||||
private long number;
|
||||
|
||||
protected PageMetadata() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param size
|
||||
* @param number
|
||||
* @param totalElements
|
||||
* @param totalPages
|
||||
*/
|
||||
public PageMetadata(long size, long number, long totalElements, long totalPages) {
|
||||
|
||||
this.size = size;
|
||||
this.number = number;
|
||||
this.totalElements = totalElements;
|
||||
this.totalPages = totalPages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the requested size of the page.
|
||||
*
|
||||
@@ -107,8 +212,44 @@ public class PagedResources<T extends Resource<?>> extends Resources<T> {
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Page metadata [number: %d, total pages: %d, total elements: %d, size: %d]", number,
|
||||
return String.format("Metadata { number: %d, total pages: %d, total elements: %d, size: %d }", number,
|
||||
totalPages, totalElements, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (obj == null || !obj.getClass().equals(getClass())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PageMetadata that = (PageMetadata) obj;
|
||||
|
||||
return this.number == that.number && this.size == that.size && this.totalElements == that.totalElements
|
||||
&& this.totalPages == that.totalPages;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = 17;
|
||||
result += 31 * (int) (this.number ^ (this.number >>> 32));
|
||||
result += 31 * (int) (this.size ^ (this.size >>> 32));
|
||||
result += 31 * (int) (this.totalElements ^ (this.totalElements >>> 32));
|
||||
result += 31 * (int) (this.totalPages ^ (this.totalPages >>> 32));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user