com.github.hal4j.resources.Resources 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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class Resources extends ResourceSupport {
private final Class elementType;
public Resources(Map> _links,
Map> _embedded,
Class elementType,
BindingContext ctx) {
super(_links, _embedded, ctx);
this.elementType = elementType;
}
Resources(ResourceSupport resource, Class elementType) {
super(resource);
this.elementType = elementType;
}
public Resources(Map> links,
Map> attachments,
Class elementType,
List> collection,
BindingContext ctx) {
super(links, merge(attachments, collection), ctx);
this.elementType = elementType;
}
@SuppressWarnings("unchecked")
private static Map> merge(Map> attachments, List> collection) {
Map> result = attachments != null ? new HashMap<>(attachments) : new HashMap<>();
result.put(HALLink.REL_ITEMS, (List) collection);
return result;
}
public List> items() {
return stream().collect(Collectors.toList());
}
public int size() {
return embedded().count(HALLink.REL_ITEMS);
}
public Stream values() {
return stream().map(Resource::value);
}
@SuppressWarnings("unchecked")
public Stream> stream() {
return embedded().selectAll(HALLink.REL_ITEMS)
.map(item -> item instanceof Resource
? (Resource) item
: context().bindResource(item, type()));
}
public void forEachResource(Consumer super Resource> consumer) {
stream().forEach(consumer);
}
public void forEach(Consumer super T> consumer) {
stream().map(Resource::value).forEach(consumer);
}
public Class type() {
return elementType;
}
}