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

io.katharsis.resource.Document Maven / Gradle / Ivy

The newest version!
package io.katharsis.resource;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.katharsis.core.internal.resource.DocumentDataDeserializer;
import io.katharsis.core.internal.resource.NullableSerializer;
import io.katharsis.errorhandling.ErrorData;
import io.katharsis.utils.Nullable;

public class Document implements MetaContainer, LinksContainer {

	@JsonInclude(Include.NON_EMPTY)
	@JsonDeserialize(using = DocumentDataDeserializer.class)
	@JsonSerialize(using = NullableSerializer.class)
	private Nullable data = Nullable.empty();

	@JsonInclude(Include.NON_EMPTY)
	private List included;

	@JsonInclude(Include.NON_EMPTY)
	private ObjectNode links;

	@JsonInclude(Include.NON_EMPTY)
	private ObjectNode meta;

	@JsonInclude(Include.NON_EMPTY)
	private List errors;

	@Override
	public ObjectNode getLinks() {
		return links;
	}

	@Override
	public void setLinks(ObjectNode links) {
		this.links = links;
	}

	@Override
	public ObjectNode getMeta() {
		return meta;
	}

	@Override
	public void setMeta(ObjectNode meta) {
		this.meta = meta;
	}

	public Nullable getData() {
		return data;
	}

	public void setData(Nullable data) {
		if (data == null) {
			throw new NullPointerException("make use of Nullable instead of null");
		}
		this.data = data;
	}

	public List getIncluded() {
		return included;
	}

	public void setIncluded(List includes) {
		this.included = includes;
	}

	public List getErrors() {
		return errors;
	}

	public void setErrors(List errors) {
		this.errors = errors;
	}

	@JsonIgnore
	public boolean isMultiple() {
		return data.get() instanceof Collection;
	}

	@JsonIgnore
	public Nullable getSingleData() {
		return (Nullable) (Nullable) data;
	}

	@Override
	public int hashCode() {
		return Objects.hash(data, errors, included, links, meta);
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Document))
			return false;
		Document other = (Document) obj;
		return Objects.equals(data, other.data) && Objects.equals(errors, other.errors) // NOSONAR
				&& Objects.equals(included, other.included) && Objects.equals(meta, other.meta) && Objects.equals(links, other.links);
	}

	@JsonIgnore
	public Nullable> getCollectionData() {
		if (!data.isPresent()) {
			return Nullable.empty();
		}
		Object value = data.get();
		if (value == null) {
			return Nullable.of((List) (List) Collections.emptyList());
		}
		if (!(value instanceof Iterable)) {
			return Nullable.of((Collections.singletonList((Resource) value)));
		}
		return Nullable.of((List) value);
	}
}