com.algodefu.yeti.web.rest.ClusterFormatSerializer.java Source code

Java tutorial

Introduction

Here is the source code for com.algodefu.yeti.web.rest.ClusterFormatSerializer.java

Source

/*
 * Copyright 2014 Algodefu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.algodefu.yeti.web.rest;

import com.algodefu.yeti.data.ClusterFormat;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.format.DateTimeFormatter;

public class ClusterFormatSerializer extends JsonSerializer<ClusterFormat> {
    private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

    @Override
    public void serialize(ClusterFormat clusterFormat, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        StringBuilder sb = new StringBuilder();
        String sep = "; ";

        // symbol
        sb.append(clusterFormat.getSymbol());
        sb.append(sep);

        // type
        sb.append(clusterFormat.getType());
        sb.append(sep);

        // beginDt
        sb.append(clusterFormat.getBeginDt().format(dtf));
        sb.append(sep);

        // endDt
        sb.append(clusterFormat.getEndDt().format(dtf));
        sb.append(sep);

        // count
        sb.append("count: " + clusterFormat.getCount());
        sb.append(sep);

        switch (clusterFormat.getType()) {
        case TIMEFRAME:
        case TICKTIME:
            sb.append("timeInterval: " + clusterFormat.getTimeInterval());
            sb.append(sep);
            sb.append("chronoUnit: " + clusterFormat.getChronoUnit());
            break;
        case PRICERANGE:
            sb.append("priceRange: " + clusterFormat.getPriceRange());
            break;
        case VOLUME:
            sb.append("clusterVolume: " + clusterFormat.getClusterVolume());
            break;
        case TRADECOUNT:
            sb.append("maxTicksInCluster: " + clusterFormat.getMaxTicksInCluster());
            break;
        }

        jgen.writeString(sb.toString());

    }
}