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

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

package com.github.hal4j.resources;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.github.hal4j.resources.HALLink.REL_SELF;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;

/**
 * This class cannot be deserialized directly by Jackson
 * (there's no support for JsonUnwrapped/JsonCreator combo).
 * @param  type of the model wrapped in this resource
 */
public final class Resource extends ResourceSupport {

    public final T model;

    public Resource(T model,
                    Map> _links,
                    Map> _embedded) {
        super(_links, _embedded, null);
        this.model = model;
    }

    public Resource(T model,
                    Map> _links,
                    Map> _embedded,
                    BindingContext ctx) {
        super(_links, _embedded, ctx);
        this.model = model;
    }

    public Resource(T model, URI self) {
        super(singletonMap(REL_SELF, singletonList(HALLink.create(self))), null, null);
        this.model = model;
    }

    Resource(ResourceSupport source, T model) {
        super(source);
        this.model = model;
    }

    public T value() {
        return this.model;
    }

    public T required() {
        return asOptional().orElseThrow(() -> new IllegalStateException("Undefined HAL model"));
    }

    public Optional asOptional() {
        return Optional.ofNullable(model);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy