All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.hal4j.resources.Resources Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
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> consumer) {
        stream().forEach(consumer);
    }

    public void forEach(Consumer consumer) {
        stream().map(Resource::value).forEach(consumer);
    }

    public Class type() {
        return elementType;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy