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

org.n52.io.response.ParameterOutput Maven / Gradle / Ivy

There is a newer version: 2.0.1-alpha.8
Show newest version
/*
 * Copyright (C) 2013-2019 52°North Initiative for Geospatial Open Source
 * Software GmbH
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation.
 *
 * If the program is linked with libraries which are licensed under one of
 * the following licenses, the combination of the program with the linked
 * library is not considered a "derivative work" of the program:
 *
 *     - Apache License, version 2.0
 *     - Apache Software License, version 1.0
 *     - GNU Lesser General Public License, version 3
 *     - Mozilla Public License, versions 1.0, 1.1 and 2.0
 *     - Common Development and Distribution License (CDDL), version 1.0
 *
 * Therefore the distribution of the program linked with libraries licensed
 * under the aforementioned licenses, is permitted by the copyright holders
 * if the distribution is compliant with both the GNU General Public License
 * version 2 and the aforementioned licenses.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 */
package org.n52.io.response;

import java.text.Collator;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;

import org.n52.io.request.IoParameters;
import org.n52.series.spi.srv.RawFormats;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public abstract class ParameterOutput implements CollatorComparable, RawFormats {

    public static final String ID = "id";
    public static final String HREF = "href";
    public static final String HREF_BASE = HREF;
    public static final String DOMAIN_ID = "domainid";
    public static final String LABEL = "label";
    public static final String EXTRAS = "extras";
    public static final String RAWFORMATS = "service";

    private String id;

    private OptionalOutput href;

    private OptionalOutput hrefBase;

    private OptionalOutput domainId;

    private OptionalOutput label;

    @Deprecated
    private OptionalOutput license;

    private OptionalOutput> extras;

    private OptionalOutput> rawFormats;

    public  void setValue(String parameter,
                             T value,
                             IoParameters parameters,
                             Consumer> consumer) {
        Set fields = parameters.getFields();
        boolean serialize = fields.isEmpty() || fields.contains(parameter);
        consumer.accept(OptionalOutput.of(value, serialize));
    }

    protected  T getIfSerialized(OptionalOutput optional) {
        return getIfSet(optional, false);
    }

    protected , E> T getIfSerializedCollection(OptionalOutput optional) {
        return getIfSetCollection(optional, false);
    }

    protected  Map getIfSerializedMap(OptionalOutput> optional) {
        return getIfSetMap(optional, false);
    }

    protected  T getIfSet(OptionalOutput optional, boolean forced) {
        return isSet(optional)
                ? optional.getValue(forced)
                : null;
    }

    protected , E> T getIfSetCollection(OptionalOutput optional, boolean forced) {
        return resolvesToNonNullValue(optional) && !optional.getValue()
                                                            .isEmpty()
                                                                    ? optional.getValue(forced)
                                                                    : null;
    }

    protected  Map getIfSetMap(OptionalOutput> optional, boolean forced) {
        return resolvesToNonNullValue(optional) && !optional.getValue()
                                                            .isEmpty()
                                                                    ? optional.getValue(forced)
                                                                    : null;
    }

    protected  boolean isSet(OptionalOutput optional) {
        return optional != null && optional.isPresent();
    }

    protected  boolean resolvesToNonNullValue(OptionalOutput optional) {
        return isSet(optional) && optional.isSerialize();
    }

    public String getId() {
        return id;
    }

    public ParameterOutput setId(String id) {
        this.id = id;
        return this;
    }

    public String getHref() {
        if (getHrefBase() == null && href == null) {
            return null;
        }
        return !isSet(href) && getHrefBase() != null
                ? getHrefBase() + "/" + getId()
                : href.getValue();
    }

    public ParameterOutput setHref(OptionalOutput href) {
        this.href = href;
        return this;
    }

    @JsonIgnore
    public String getHrefBase() {
        return getIfSerialized(hrefBase);
    }

    public ParameterOutput setHrefBase(OptionalOutput hrefBase) {
        this.hrefBase = hrefBase;
        return this;
    }

    /**
     * Returns the domain id of the parameter, e.g. a natural id (not arbitrarily generated) or the original
     * id actually being used by proxied data sources.
     *
     * @return the domain id
     */
    public String getDomainId() {
        return getIfSerialized(domainId);
    }

    /**
     * Sets the domain id of the parameter, e.g. a natural (not arbitrarily generated) id or the original id
     * actually being used by proxied data sources.
     *
     * @param domainId
     *        the domain id of the parameter
     * @return the instance to enable chaining
     */
    public ParameterOutput setDomainId(OptionalOutput domainId) {
        this.domainId = domainId;
        return this;
    }

    /**
     * @return the label. Returns null if label is not set.
     */
    public String getLabel() {
        return getIfSerialized(label);
    }

    public ParameterOutput setLabel(OptionalOutput label) {
        this.label = label;
        return this;
    }

    @Deprecated
    public String getLicense() {
        return getIfSerialized(license);
    }

    @Deprecated
    public ParameterOutput setLicense(OptionalOutput license) {
        this.license = license;
        return this;
    }

    /**
     * @return a list of extra identifiers available via /{resource}/extras
     */
    public Collection getExtras() {
        return getIfSerializedCollection(extras);
    }

    public ParameterOutput setExtras(OptionalOutput> extras) {
        this.extras = extras;
        return this;
    }

    @Override
    public Set getRawFormats() {
        return getIfSerializedCollection(rawFormats);
    }

    @Override
    public ParameterOutput setRawFormats(OptionalOutput> formats) {
        this.rawFormats = formats;
        return this;
    }

    @Override
    public int compare(Collator collator, ParameterOutput o) {
        String thisLabel = getLabel();
        String otherLabel = o.getLabel();
        return collator.compare(thisLabel.toLowerCase(), otherLabel.toLowerCase());
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, domainId, label);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof ParameterOutput)) {
            return false;
        }
        ParameterOutput other = (ParameterOutput) obj;
        return Objects.equals(id, other.id)
                && Objects.equals(domainId, other.domainId)
                && Objects.equals(label, other.label);
    }

    /**
     * Takes the labels to compare.
     *
     * @param 
     *        the actual type.
     * @return a label comparing {@link Comparator}
     */
    public static  Comparator defaultComparator() {
        return (T o1, T o2) -> {
            // some outputs don't have labels, e.g. GeometryInfo
            Comparator nullsFirst = Comparator.nullsFirst(Comparator.naturalOrder());
            return Comparator.comparing(ParameterOutput::getLabel, nullsFirst)
                             .thenComparing(ParameterOutput::getId)
                             .compare(o1, o2);
        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy