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

io.nosqlbench.nb.api.config.Synonyms Maven / Gradle / Ivy

There is a newer version: 4.15.102
Show newest version
package io.nosqlbench.nb.api.config;

import org.apache.logging.log4j.Logger;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.regex.Pattern;

/**
 * This class is just a central reference point for the names of parameters
 * or other configuration-level primitives which have been given better names.
 * For the sake of backwards compatibility, the old names are retained, but
 * deprecated and warned against.
 */
public class Synonyms {

    /**
     * Each entry in this list is a list of synonyms in configuration.
     */
    public final static Map> PARAM_SYNONYMS = new HashMap<>() {{
        put("hosts", Set.of("host" ));
        put("workload", Set.of("yaml" ));
        put("driver", Set.of("type" ));
        put("rate", Set.of("targetrate", "cyclerate" ));
        put("parameterized", Set.of("parametrized" )); // mispelling safety net
    }};

    /**
     * use this method to convert deprecated
     * @param input A configuration string from a user or file
     * @param synonyms A list of known synonym lists with the preferred values first, like {@link #PARAM_SYNONYMS}
     * @param warnings An BiConsumer which can handle (deprecated, preferred) for subsitutions.
     * @return The configuration string in canonicalized form, with the preferred names used where possible
     */
    public static String canonicalize(String input, Map> synonyms, BiConsumer warnings) {
        String replaced = input;
        for (Map.Entry> syns : synonyms.entrySet()) {
            String preferred = syns.getKey();
            for (String deprecated : syns.getValue()) {
                Pattern p = Pattern.compile("\\b" + deprecated + "\\b");
                String prior = replaced;
                replaced = replaced.replaceAll(p.pattern(),preferred);
                if (!prior.equals(replaced) && warnings!=null) {
                    warnings.accept(deprecated,preferred);
                }
            }
        }
        return replaced;
    }

    public static String canonicalize(String arg, Logger logger) {
        return canonicalize(arg, PARAM_SYNONYMS, (d, p) -> logger.warn(
            "The param name '" + p + "' is preferred. The use of '" + d + "' may be deprecated in the future."
        ));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy