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

com.graphaware.reco.generic.web.KeyValueConfigParser Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2013-2016 GraphAware
 *
 * This file is part of the GraphAware Framework.
 *
 * GraphAware Framework is free software: you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of
 * the GNU General Public License along with this program.  If not, see
 * .
 */

package com.graphaware.reco.generic.web;

import com.graphaware.reco.generic.config.KeyValueConfig;
import com.graphaware.reco.generic.config.MapBasedConfig;

import java.util.HashMap;
import java.util.Map;

import static com.graphaware.reco.generic.util.Assert.hasLength;

/**
 * {@link ConfigParser} producing {@link KeyValueConfig}. By default, it produces {@link MapBasedConfig} with {@link String}
 * values.
 */
public class KeyValueConfigParser implements ConfigParser {

    private final String separator;

    /**
     * @param separator used for separating keys, values, and key-value pair from each other. Must not be null or empty.
     */
    public KeyValueConfigParser(String separator) {
        hasLength(separator);

        this.separator = separator;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public KeyValueConfig produceConfig(int limit, String config) {
        return new MapBasedConfig(limit, produceConfigMap(config, separator));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public KeyValueConfig produceConfig(int limit, long maxTime, String config) {
        return new MapBasedConfig(limit, maxTime, produceConfigMap(config, separator));
    }

    /**
     * Produce a map from a String config and separator.
     *
     * @param config    as String.
     * @param separator separator.
     * @return kay-value map.
     */
    protected static Map produceConfigMap(String config, String separator) {
        Map result = new HashMap<>();

        if (config == null || config.length() <= 0) {
            return result;
        }

        String[] keyValue = config.split(separator);

        if (keyValue.length % 2 != 0) {
            throw new IllegalArgumentException("Config must contain an even number of tokens separated by " + separator);
        }

        for (int i = 0; i < keyValue.length - 1; i = i + 2) {
            result.put(keyValue[i], keyValue[i + 1]);
        }

        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy