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

io.logspace.jvm.agent.api.json.AgentControllerCapabilitiesJsonSerializer Maven / Gradle / Ivy

The newest version!
/**
 * Logspace
 * Copyright (c) 2015 Indoqa Software Design und Beratung GmbH. All rights reserved.
 * This program and the accompanying materials are made available under the terms of
 * the Eclipse Public License Version 1.0, which accompanies this distribution and
 * is available at http://www.eclipse.org/legal/epl-v10.html.
 */
package io.logspace.jvm.agent.api.json;

import static io.logspace.jvm.agent.shaded.jackson.core.JsonEncoding.UTF8;
import static io.logspace.jvm.agent.api.order.AgentControllerCapabilities.*;
import io.logspace.jvm.agent.api.order.AgentCapabilities;
import io.logspace.jvm.agent.api.order.AgentControllerCapabilities;
import io.logspace.jvm.agent.api.order.PropertyDescription;
import io.logspace.jvm.agent.api.order.PropertyDescription.PropertyUnit;
import io.logspace.jvm.agent.api.order.TriggerType;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public final class AgentControllerCapabilitiesJsonSerializer extends AbstractJsonSerializer {

    private AgentControllerCapabilitiesJsonSerializer(OutputStream outputStream) throws IOException {
        super(outputStream);
    }

    public static String toJson(AgentControllerCapabilities capabilities) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        toJson(capabilities, baos);

        return baos.toString(UTF8.getJavaName());
    }

    public static void toJson(AgentControllerCapabilities capabilities, OutputStream outputStream) throws IOException {
        new AgentControllerCapabilitiesJsonSerializer(outputStream).serialize(capabilities);
    }

    private void serialize(AgentControllerCapabilities capabilities) throws IOException {
        this.startObject();

        this.writeAttributes(capabilities);
        this.writeAgentCapabilities(capabilities.getAgentCapabilities());

        this.endObject();

        this.finish();
    }

    private void writeAgentCapabilities(AgentCapabilities agentCapabilities) throws IOException {
        this.writeMandatoryStringField(FIELD_ID, agentCapabilities.getId());
        this.writeMandatoryStringField(FIELD_TYPE, agentCapabilities.getType());

        this.writeFieldName(FIELD_TRIGGER_TYPES);
        this.startArray();
        for (TriggerType eachTriggerType : agentCapabilities.getSupportedTriggerTypes()) {
            this.writeString(eachTriggerType.name());
        }
        this.endArray();

        this.writeFieldName(FIELD_PROPERTY_DESCRIPTIONS);
        this.startArray();
        if (agentCapabilities.hasPropertyDescriptions()) {
            for (PropertyDescription eachPropertyDescription : agentCapabilities.getPropertyDescriptions()) {
                this.startObject();
                this.writePropertyDescription(eachPropertyDescription);
                this.endObject();
            }
        }
        this.endArray();
    }

    private void writeAgentCapabilities(List agentCapabilities) throws IOException {
        if (agentCapabilities == null || agentCapabilities.isEmpty()) {
            return;
        }

        this.writeFieldName(FIELD_AGENT_CAPABILITIES);
        this.startArray();
        for (AgentCapabilities eachAgentCapabilities : agentCapabilities) {
            this.startObject();
            this.writeAgentCapabilities(eachAgentCapabilities);
            this.endObject();
        }
        this.endArray();
    }

    private void writeAttributes(AgentControllerCapabilities capabilities) throws IOException {
        this.writeMandatoryStringField(FIELD_ID, capabilities.getId());
        this.writeMandatoryStringField(FIELD_SYSTEM, capabilities.getSystem());
        this.writeOptionalField(FIELD_SPACE, capabilities.getSpace());
    }

    private void writePropertyDescription(PropertyDescription propertyDescription) throws IOException {
        this.writeMandatoryStringField(FIELD_PROPERTY_NAME, propertyDescription.getName());
        this.writeMandatoryStringField(FIELD_PROPERTY_TYPE, propertyDescription.getPropertyType().name());

        if (propertyDescription.hasUnits()) {
            this.writeFieldName(FIELD_PROPERTY_UNITS);

            this.startObject();

            for (PropertyUnit eachPropertyUnit : propertyDescription.getUnits()) {
                this.writeMandatoryDoubleField(eachPropertyUnit.getName(), eachPropertyUnit.getFactor());
            }

            this.endObject();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy