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

net.intelie.pipes.HelpData Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
package net.intelie.pipes;

import java.io.Serializable;
import java.util.Objects;

public class HelpData implements Serializable {
    public static final HelpData EMPTY = new HelpData(null, null, null, null, null, null, null, null);
    private static final long serialVersionUID = 1L;
    private final String type;
    private final String name;
    private final String usage;
    private final String description;
    private final String since;
    private final String docKey;
    private final String docUrl;
    private final String text;

    public HelpData(String type, String name, String usage, String description, String since, String docKey, String docUrl, String text) {
        if (type == null) type = typeByName(name);

        String[] descs = description != null ? description.split("[\\r\\n]", 2) : new String[0];
        String maybeText = ((descs.length > 1 ? descs[1] : "").trim() + (text != null ? ("\n\n" + text.trim()) : "")).trim();

        this.type = type;
        this.name = name;
        this.usage = usage;
        this.description = descs.length > 0 ? descs[0].trim() : null;
        this.since = since;
        this.docKey = docKey;
        this.docUrl = docUrl;
        this.text = !maybeText.isEmpty() ? maybeText : null;
    }

    public static String typeByName(String name) {
        if (name == null) return null;
        return name.startsWith("@@") ? "macro" :
                name.startsWith("@") ? "pipe" :
                        "function";
    }

    public static HelpData merge(String name, HelpData a, HelpData b) {
        return new HelpData(
                first(a.type(), b.type()),
                name,
                first(a.usage(), b.usage()),
                first(a.description(), b.description()),
                first(a.since(), b.since()),
                first(a.docKey(), b.docKey()),
                first(a.docUrl(), b.docUrl()),
                first(a.text(), b.text())
        );
    }

    private static String first(String a, String b) {
        if (a != null && !a.isEmpty()) return a;
        return b;
    }

    public String type() {
        return type;
    }

    public String name() {
        return name;
    }

    public String usage() {
        return usage;
    }

    public String description() {
        return description;
    }

    public String since() {
        return since;
    }

    public String docKey() {
        return docKey;
    }

    public String docUrl() {
        return docUrl;
    }

    public String text() {
        return text;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        HelpData that = (HelpData) o;

        return Objects.equals(this.type, that.type) &&
                Objects.equals(this.name, that.name) &&
                Objects.equals(this.usage, that.usage) &&
                Objects.equals(this.description, that.description) &&
                Objects.equals(this.since, that.since) &&
                Objects.equals(this.docKey, that.docKey) &&
                Objects.equals(this.text, that.text) &&
                Objects.equals(this.docUrl, that.docUrl);
    }

    @Override
    public int hashCode() {
        return Objects.hash(type, name, usage, description, since, docKey, docUrl, text);
    }

    @Override
    public String toString() {
        return "HelpData{" +
                "type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", usage='" + usage + '\'' +
                ", description='" + description + '\'' +
                ", since='" + since + '\'' +
                ", docKey='" + docKey + '\'' +
                ", docUrl='" + docUrl + '\'' +
                ", text='" + text + '\'' +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy