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

io.logspace.agent.api.json.AbstractEventPropertyJsonHandler Maven / Gradle / Ivy

There is a newer version: 0.3.2
Show 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.agent.api.json;

import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;

import io.logspace.agent.shaded.jackson.core.JsonGenerator;
import io.logspace.agent.shaded.jackson.core.JsonParser;
import io.logspace.agent.shaded.jackson.core.JsonToken;

import io.logspace.agent.api.event.EventProperties;
import io.logspace.agent.api.event.EventProperty;

public abstract class AbstractEventPropertyJsonHandler implements EventPropertyJsonHandler {

    @Override
    public void readEventProperties(EventProperties eventProperties, String propertyName, JsonParser jsonParser) throws IOException {
        while (true) {
            JacksonUtils.prepareToken(jsonParser);
            if (jsonParser.getCurrentToken() == JsonToken.END_ARRAY) {
                break;
            }

            this.readPropertyValue(eventProperties, propertyName, jsonParser);

            JacksonUtils.consumeToken(jsonParser);
        }
    }

    @Override
    public void writeEventProperties(JsonGenerator jsonGenerator, Collection> properties)
            throws IOException {
        Map> propertyValues = this.getPropertyValues(properties);

        jsonGenerator.writeFieldName(this.getFieldName());

        jsonGenerator.writeStartObject();
        for (Entry> eachEntry : propertyValues.entrySet()) {
            jsonGenerator.writeArrayFieldStart(eachEntry.getKey());

            for (T eachPropertyValue : eachEntry.getValue()) {
                this.writePropertyValue(jsonGenerator, eachPropertyValue);
            }
            jsonGenerator.writeEndArray();
        }

        jsonGenerator.writeEndObject();
    }

    protected abstract void readPropertyValue(EventProperties eventProperties, String propertyName, JsonParser jsonParser)
            throws IOException;

    protected abstract void writePropertyValue(JsonGenerator jsonGenerator, T propertyValue) throws IOException;

    private Map> getPropertyValues(Collection> properties) {
        Map> result = new LinkedHashMap>();

        for (EventProperty eachProperty : properties) {
            List values = result.get(eachProperty.getKey());
            if (values == null) {
                values = new ArrayList();
                result.put(eachProperty.getKey(), values);
            }

            values.add(eachProperty.getValue());
        }

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy