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

com.at.avro.formatters.EnumFormatter Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.at.avro.formatters;

import com.at.avro.config.FormatterConfig;
import com.at.avro.types.Enum;

/**
 * @author [email protected]
 */
public class EnumFormatter implements Formatter {
    @Override
    public String toJson(Enum anEnum, FormatterConfig config) {
        String template = "{ \"type\":\"enum\", \"name\":\"%s\", \"symbols\":[%s] }".replaceAll(":", config.colon());
        return String.format(template, anEnum.getName(), formatSymbols(anEnum.getSymbols(), config));
    }

    private String formatSymbols(String[] symbols, FormatterConfig config) {
        StringBuilder symbolsJson = new StringBuilder();
        if (symbols.length > 0) {
            for (String symbol : symbols) {
                symbolsJson.append("\"").append(symbol.trim()).append("\", ");
            }
            symbolsJson.setLength(symbolsJson.length() - 2);
        }
        return symbolsJson.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy