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

io.github.twinklekhj.ros.op.RosResponse Maven / Gradle / Ivy

There is a newer version: 1.4.2
Show newest version
package io.github.twinklekhj.ros.op;


import io.vertx.core.json.JsonObject;
import lombok.*;

import java.util.Collections;
import java.util.Map;


@Builder
@AllArgsConstructor
@RequiredArgsConstructor
@ToString
public class RosResponse implements RosOperation {
    private final Type op = Type.CALL_SERVICE;
    @NonNull
    private final String service;
    @NonNull
    private final boolean result;

    private String id;
    private Map values;

    private static RosResponseBuilder builder() {
        return new RosResponseBuilder();
    }

    public static RosResponseBuilder builder(String service, boolean result) {
        return builder().service(service).result(result);
    }

    public static RosResponse fromString(String str) {
        JsonObject node = new JsonObject(str);
        return fromJsonObject(node);
    }

    public static RosResponse fromJsonObject(JsonObject node) {
        Object valuesObj = node.getMap().get("values");

        String id = node.getString("id");

        Map values = Collections.emptyMap();
        if (valuesObj instanceof Map) {
            values = (Map) valuesObj;
        } else if (valuesObj instanceof JsonObject) {
            values = ((JsonObject) valuesObj).getMap();
        }

        boolean result = node.getBoolean("result");
        String service = node.getString("service");

        return new RosResponse(service, result, id, values);
    }

    public String getService() {
        return service;
    }

    public boolean getResult() {
        return result;
    }

    public String getId() {
        return id;
    }

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

    public Map getValues() {
        return values;
    }

    public void setValues(Map values) {
        this.values = values;
    }

    @Override
    public String toJson() {
        return new JsonObject()
                .put("op", this.op.code)
                .put("service", this.service)
                .put("values", this.values)
                .put("result", this.result)
                .put("id", this.id).toString();
    }

    @Override
    public Type getOperation() {
        return this.op;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy