com.github.hal4j.resources.GenericResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hal4j-api Show documentation
Show all versions of hal4j-api Show documentation
HAL API specification for Java
package com.github.hal4j.resources;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Resource containing generic model as a Map with String keys
*/
public final class GenericResource extends ResourceSupport {
public final Map model;
public GenericResource(Map model,
Map> _links,
Map> _embedded,
BindingContext ctx) {
super(_links, _embedded, ctx);
this.model = model != null ? Collections.unmodifiableMap(model) : null;
}
public GenericResource(Map model,
Map> _links,
Map> _embedded) {
this(model, _links, _embedded, null);
}
public GenericResource(Map> _links,
Map> _attachments) {
this(null, _links, _attachments, null);
}
/**
* Parse the generic model into object of given type and return it
* as a resource with all the links and attachments.
* @param clazz Class object used as a metamodel for mapping
* @param type of the expected object
* @return this resource as a resource of given type
*/
public Resource as(Class clazz) {
T instance = context().bind(this.model, clazz);
return new Resource<>(this, instance);
}
/**
* Parse the generic model into the collection of objects of given type and return it
* as a resource with all the links and attachments. Collection items are parsed from
* the embedded objects with relation "items".
* @param clazz Class object used as a metamodel for mapping
* @param type of the expected object
* @return this resource as a collection resource of given type
*/
@SuppressWarnings("unchecked")
public Resources asCollectionOf(Class clazz) {
List