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

no.arktekk.siren.Action Maven / Gradle / Ivy

package no.arktekk.siren;

import io.vavr.control.Option;
import static io.vavr.control.Option.none;

import no.arktekk.siren.field.FieldSerializer;
import no.arktekk.siren.field.WWWUrlEncodedFieldSerializer;

import java.net.URI;
import java.util.Objects;

public final class Action {
    public final String name;
    public final URI href;
    public final Option classes;
    public final Option title;
    public final Option method;
    public final Option type;
    public final Option fields;

    public Action(String name, URI href, Option classes, Option title, Option method, Option type, Option fields) {
        this.name = Objects.requireNonNull(name, "name may not be null");
        this.href = Objects.requireNonNull(href, "href may not be null");
        this.classes = classes;
        this.title = title;
        this.method = method;
        this.type = type;
        this.fields = fields;
    }

    public static Action of(String name, URI href) {
        return new Action(name, href, none(), none(), none(), none(), none());
    }

    public Action with(Classes classes) {
        return new Action(name, href, Option.of(classes), title, method, type, fields);
    }

    public Action with(String title) {
        return new Action(name, href, classes, Option.of(title), method, type, fields);
    }

    public Action with(Method method) {
        return new Action(name, href, classes, title, Option.of(method), type, fields);
    }

    public Action with(MIMEType type) {
        return new Action(name, href, classes, title, method, Option.of(type), fields);
    }

    public Action with(Fields fields) {
        return new Action(name, href, classes, title, method, type, Option.of(fields));
    }

    public Option format(FieldSerializer serializer) {
        return serializer.serialize(type.getOrElse(MIMEType.URLEncoded), fields);
    }

    public Option format() {
        return format(WWWUrlEncodedFieldSerializer.INSTANCE);
    }

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

        Action action = (Action) o;

        if (!name.equals(action.name)) return false;
        if (!classes.equals(action.classes)) return false;
        if (!href.equals(action.href)) return false;
        if (!title.equals(action.title)) return false;
        if (!method.equals(action.method)) return false;
        if (!type.equals(action.type)) return false;
        return fields.equals(action.fields);

    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + classes.hashCode();
        result = 31 * result + href.hashCode();
        result = 31 * result + title.hashCode();
        result = 31 * result + method.hashCode();
        result = 31 * result + type.hashCode();
        result = 31 * result + fields.hashCode();
        return result;
    }

    public String getName() {
        return name;
    }

    public URI getHref() {
        return href;
    }

    public Option getClasses() {
        return classes;
    }

    public Option getTitle() {
        return title;
    }

    public Option getMethod() {
        return method;
    }

    public Option getType() {
        return type;
    }

    public Option getFields() {
        return fields;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy