ai.platon.pulsar.common.config.Params Maven / Gradle / Ivy
package ai.platon.pulsar.common.config;
import ai.platon.pulsar.common.Strings;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Created by vincent on 16-9-24.
*
* @author vincent
* @version $Id: $Id
*/
public class Params {
/**
* Constant EMPTY_PARAMS
*/
public static final Params EMPTY_PARAMS = new Params();
private Logger log = LoggerFactory.getLogger(Params.class);
private List> paramsList = new LinkedList<>();
private String captionFormat = String.format("%20sParams Table%-25s\n", "----------", "----------");
private String headerFormat = String.format("%25s %-25s\n", "Name", "Value");
private String rowFormat = "%25s: %s";
private boolean cmdLineStyle = false;
private List distinctBooleanParams;
private String pairDelimiter = " ";
private String kvDelimiter = ": ";
private Logger defaultLog = null;
/**
* Constructor for Params.
*/
public Params() {
}
/**
* Constructor for Params.
*
* @param key a {@link java.lang.String} object.
* @param value a {@link java.lang.Object} object.
* @param others a {@link java.lang.Object} object.
*/
public Params(String key, Object value, Object... others) {
this.paramsList.addAll(toArgList(key, value, others));
}
/**
* Constructor for Params.
*
* @param args a {@link java.util.Map} object.
*/
public Params(Map args) {
args.forEach((key, value) -> this.paramsList.add(Pair.of(key, value)));
}
/**
* of.
*
* @param key a {@link java.lang.String} object.
* @param value a {@link java.lang.Object} object.
* @param others a {@link java.lang.Object} object.
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public static Params of(String key, Object value, Object... others) {
return new Params(key, value, others);
}
/**
* of.
*
* @param args a {@link java.util.Map} object.
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public static Params of(Map args) {
return new Params(args);
}
/**
* toArgList.
*
* @param key a {@link java.lang.String} object.
* @param value a {@link java.lang.Object} object.
* @param others a {@link java.lang.Object} object.
* @return a {@link java.util.List} object.
*/
public static List> toArgList(String key, Object value, Object... others) {
List> results = new LinkedList<>();
results.add(Pair.of(key, value));
if (others == null || others.length < 2) {
return results;
}
if (others.length % 2 != 0) {
throw new RuntimeException("expected name/value pairs");
}
for (int i = 0; i < others.length; i += 2) {
Object k = others[i];
Object v = others[i + 1];
if (k != null && v != null) {
results.add(Pair.of(String.valueOf(others[i]), others[i + 1]));
}
}
return results;
}
/**
* Convert K/V pairs array into a map.
*
* @param others A K/V pairs array, the length of the array must be a even number
* null key or null value pair is ignored
* @param key a {@link java.lang.String} object.
* @param value a {@link java.lang.Object} object.
* @return A map contains all non-null key/values
*/
public static Map toArgMap(String key, Object value, Object... others) {
Map results = new LinkedHashMap<>();
results.put(key, value);
if (others == null || others.length < 2) {
return results;
}
if (others.length % 2 != 0) {
throw new RuntimeException("expected name/value pairs");
}
for (int i = 0; i < others.length; i += 2) {
Object k = others[i];
Object v = others[i + 1];
if (k != null && v != null) {
results.put(String.valueOf(others[i]), others[i + 1]);
}
}
return results;
}
/**
* formatAsLine.
*
* @param key a {@link java.lang.String} object.
* @param value a {@link java.lang.Object} object.
* @param others a {@link java.lang.Object} object.
* @return a {@link java.lang.String} object.
*/
public static String formatAsLine(String key, Object value, Object... others) {
return Params.of(key, value, others).formatAsLine();
}
/**
* format.
*
* @param key a {@link java.lang.String} object.
* @param value a {@link java.lang.Object} object.
* @param others a {@link java.lang.Object} object.
* @return a {@link java.lang.String} object.
*/
public static String format(String key, Object value, Object... others) {
return Params.of(key, value, others).format();
}
/**
* put.
*
* @param name a {@link java.lang.String} object.
* @param value a {@link java.lang.Object} object.
*/
public void put(String name, Object value) {
paramsList.add(Pair.of(name, value));
}
/**
* remove.
*
* @param key a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean remove(String key) {
List> list = paramsList.stream().filter(entry -> !entry.getKey().equals(key)).collect(Collectors.toList());
boolean removed = list.size() < paramsList.size();
if (removed) {
this.paramsList = list;
}
return removed;
}
/**
* get.
*
* @param name a {@link java.lang.String} object.
* @return a {@link java.lang.Object} object.
*/
public Object get(String name) {
Pair entry = CollectionUtils.find(paramsList, e -> e.getKey().equals(name));
return entry == null ? null : entry.getValue();
}
/**
* get.
*
* @param name a {@link java.lang.String} object.
* @param defaultValue a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String get(String name, String defaultValue) {
String value = (String) get(name);
return value == null ? defaultValue : value;
}
/**
* getString.
*
* @param name a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String getString(String name) {
return (String) get(name);
}
/**
* getEnum.
*
* @param name a {@link java.lang.String} object.
* @param defaultValue a T object.
* @param a T object.
* @return a T object.
*/
public > T getEnum(String name, T defaultValue) {
Object val = this.get(name);
return null == val ? defaultValue : Enum.valueOf(defaultValue.getDeclaringClass(), val.toString());
}
/**
* getInt.
*
* @param name a {@link java.lang.String} object.
* @return a {@link java.lang.Integer} object.
*/
public Integer getInt(String name) {
return (Integer) get(name);
}
/**
* getInt.
*
* @param name a {@link java.lang.String} object.
* @param defaultValue a {@link java.lang.Integer} object.
* @return a {@link java.lang.Integer} object.
*/
public Integer getInt(String name, Integer defaultValue) {
Integer value = (Integer) get(name);
return value == null ? defaultValue : value;
}
/**
* getLong.
*
* @param name a {@link java.lang.String} object.
* @return a {@link java.lang.Long} object.
*/
public Long getLong(String name) {
return (Long) get(name);
}
/**
* getLong.
*
* @param name a {@link java.lang.String} object.
* @param defaultValue a {@link java.lang.Long} object.
* @return a {@link java.lang.Long} object.
*/
public Long getLong(String name, Long defaultValue) {
Long value = (Long) get(name);
return value == null ? defaultValue : value;
}
/**
* getBoolean.
*
* @param name a {@link java.lang.String} object.
* @return a {@link java.lang.Boolean} object.
*/
public Boolean getBoolean(String name) {
return (Boolean) get(name);
}
/**
* getBoolean.
*
* @param name a {@link java.lang.String} object.
* @param defaultValue a {@link java.lang.Boolean} object.
* @return a {@link java.lang.Boolean} object.
*/
public Boolean getBoolean(String name, Boolean defaultValue) {
Boolean value = (Boolean) get(name);
return value == null ? defaultValue : value;
}
/**
* getStrings.
*
* @param name a {@link java.lang.String} object.
* @param defaultValue an array of {@link java.lang.String} objects.
* @return an array of {@link java.lang.String} objects.
*/
public String[] getStrings(String name, String[] defaultValue) {
String valueString = get(name, null);
if (valueString == null) {
return defaultValue;
}
return Strings.getStrings(valueString);
}
/**
* getStringCollection.
*
* @param name a {@link java.lang.String} object.
* @param delim a {@link java.lang.String} object.
* @return a {@link java.util.Collection} object.
*/
public Collection getStringCollection(String name, String delim) {
String valueString = get(name, null);
return Strings.getStringCollection(valueString, delim);
}
/**
* getPath.
*
* @param name a {@link java.lang.String} object.
* @return a {@link java.nio.file.Path} object.
* @throws java.io.IOException if any.
*/
public Path getPath(String name) throws IOException {
String value = getString(name);
if (value == null) return null;
Path path = Paths.get(value);
Files.createDirectories(path.getParent());
return path;
}
/**
* getPath.
*
* @param name a {@link java.lang.String} object.
* @param defaultValue a {@link java.nio.file.Path} object.
* @return a {@link java.nio.file.Path} object.
* @throws java.io.IOException if any.
*/
public Path getPath(String name, Path defaultValue) throws IOException {
String value = getString(name);
Path path = value == null ? Paths.get(value) : defaultValue;
Files.createDirectories(path.getParent());
return path;
}
/**
* getInstant.
*
* @param name a {@link java.lang.String} object.
* @return a {@link java.time.Instant} object.
*/
public Instant getInstant(String name) {
return (Instant) get(name);
}
/**
* getInstant.
*
* @param name a {@link java.lang.String} object.
* @param defaultValue a {@link java.time.Instant} object.
* @return a {@link java.time.Instant} object.
*/
public Instant getInstant(String name, Instant defaultValue) {
Instant value = (Instant) get(name);
return value == null ? defaultValue : value;
}
/**
* getDuration.
*
* @param name a {@link java.lang.String} object.
* @return a {@link java.time.Duration} object.
*/
public Duration getDuration(String name) {
return (Duration) get(name);
}
/**
* getDuration.
*
* @param name a {@link java.lang.String} object.
* @param defaultValue a {@link java.time.Duration} object.
* @return a {@link java.time.Duration} object.
*/
public Duration getDuration(String name, Duration defaultValue) {
Duration value = (Duration) get(name);
return value == null ? defaultValue : value;
}
/**
* format.
*
* @return a {@link java.lang.String} object.
*/
public String format() {
return format(paramsList);
}
/**
* formatAsLine.
*
* @return a {@link java.lang.String} object.
*/
public String formatAsLine() {
return formatAsLine(paramsList);
}
/**
* withCaptionFormat.
*
* @param captionFormat a {@link java.lang.String} object.
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params withCaptionFormat(String captionFormat) {
this.captionFormat = captionFormat;
return this;
}
/**
* withHeaderFormat.
*
* @param headerFormat a {@link java.lang.String} object.
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params withHeaderFormat(String headerFormat) {
this.headerFormat = headerFormat;
return this;
}
/**
* withRowFormat.
*
* @param rowFormat a {@link java.lang.String} object.
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params withRowFormat(String rowFormat) {
this.rowFormat = rowFormat;
return this;
}
/**
* withPairDelimiter.
*
* @param pairDelimiter a {@link java.lang.String} object.
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params withPairDelimiter(String pairDelimiter) {
this.pairDelimiter = pairDelimiter;
return this;
}
/**
* withKVDelimiter.
*
* @param kvDelimiter a {@link java.lang.String} object.
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params withKVDelimiter(String kvDelimiter) {
this.kvDelimiter = kvDelimiter;
return this;
}
/**
* iscmdLineStyle.
*
* @return a boolean.
*/
public boolean isCmdLineStyle() {
return cmdLineStyle;
}
/**
* withCmdLineStyle.
*
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params withCmdLineStyle() {
return withCmdLineStyle(true);
}
/**
* withCmdLineStyle.
*
* @param isCmdLineStyle a boolean.
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params withCmdLineStyle(boolean isCmdLineStyle) {
this.cmdLineStyle = isCmdLineStyle;
return this;
}
public Params withDistinctBooleanParams(List distinctBooleanParams) {
this.distinctBooleanParams = distinctBooleanParams;
return this;
}
/**
* sorted.
*
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params sorted() {
this.paramsList = this.paramsList.stream()
.sorted(Comparator.comparing(Pair::getKey))
.collect(Collectors.toList());
return this;
}
/**
* filter.
*
* @param predicate a {@link java.util.function.Predicate} object.
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params filter(Predicate> predicate) {
this.paramsList = this.paramsList.stream()
.filter(predicate)
.collect(Collectors.toList());
return this;
}
/**
* distinct.
*
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params distinct() {
this.paramsList = this.paramsList.stream()
.distinct()
.collect(Collectors.toList());
return this;
}
/**
* merge.
*
* @param others a {@link ai.platon.pulsar.common.config.Params} object.
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params merge(Params... others) {
if (others != null && others.length > 0) {
Arrays.stream(others).forEach(params -> this.paramsList.addAll(params.getParamsList()));
}
return this;
}
/**
* merge.
*
* @param others a {@link java.util.Collection} object.
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params merge(Collection others) {
others.forEach(params -> this.paramsList.addAll(params.getParamsList()));
return this;
}
/**
* Getter for the field paramsList
.
*
* @return a {@link java.util.List} object.
*/
public List> getParamsList() {
return paramsList;
}
/**
* asMap.
*
* @return a {@link java.util.Map} object.
*/
public Map asMap() {
Map result = new HashMap<>();
paramsList.forEach(p -> result.put(p.getKey(), p.getValue()));
return result;
}
/**
* asStringMap.
*
* @return a {@link java.util.Map} object.
*/
public Map asStringMap() {
Map result = new HashMap<>();
paramsList.forEach(p -> result.put(p.getKey(), p.getValue().toString()));
return result;
}
/**
* withLogger.
*
* @param logger a {@link org.slf4j.Logger} object.
* @return a {@link ai.platon.pulsar.common.config.Params} object.
*/
public Params withLogger(Logger logger) {
this.defaultLog = logger;
return this;
}
/**
* debug.
*/
public void debug() {
debug(false);
}
/**
* debug.
*
* @param inline a boolean.
*/
public void debug(boolean inline) {
if (defaultLog != null) {
defaultLog.debug(inline ? formatAsLine() : format());
} else {
log.debug(inline ? formatAsLine() : format());
}
}
/**
* info.
*/
public void info() {
info(false);
}
/**
* info.
*
* @param inline a boolean.
*/
public void info(boolean inline) {
info("", "", inline);
}
/**
* info.
*
* @param prefix a {@link java.lang.String} object.
* @param postfix a {@link java.lang.String} object.
* @param inline a boolean.
*/
public void info(String prefix, String postfix, boolean inline) {
StringBuilder sb = new StringBuilder(prefix);
sb.append(inline ? formatAsLine() : format());
sb.append(postfix);
String message = sb.toString();
if (message.isEmpty()) {
return;
}
if (defaultLog != null) {
defaultLog.info("{}", sb);
} else {
log.info("{}", sb);
}
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return format();
}
private String format(List> params) {
if (params.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
sb.append('\n');
if (captionFormat != null) sb.append(captionFormat);
if (headerFormat != null) sb.append(headerFormat);
int i = 0;
for (Pair param : params) {
if (i++ > 0) {
sb.append("\n");
}
String key = param.getKey();
Object value = param.getValue();
if (value instanceof Map) {
Map, ?> m = (Map, ?>) value;
value = m.entrySet().stream()
.map(e -> e.getKey() + ":" + e.getValue().toString())
.collect(Collectors.joining(", "));
} else if (value instanceof Collection) {
Collection> c = (Collection>) value;
value = StringUtils.join(c, ", ");
}
sb.append(String.format(rowFormat, key, value));
}
sb.append('\n');
return sb.toString();
}
private String formatAsLine(List> params) {
if (params.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
int i = 0;
for (Pair arg : params) {
if (i++ > 0) {
sb.append(pairDelimiter);
}
String key = arg.getKey();
String value = arg.getValue().toString();
if (arg.getValue() == null) {
sb.append(key);
if (!cmdLineStyle) {
sb.append(kvDelimiter);
sb.append("null");
}
} else if (cmdLineStyle && key.startsWith("-") && "true".equals(value)) {
if (distinctBooleanParams != null && distinctBooleanParams.contains(key)) {
sb.append(key);
sb.append(kvDelimiter);
sb.append("true");
} else {
sb.append(key);
}
} else if (cmdLineStyle && key.startsWith("-") && "false".equals(value)) {
if (distinctBooleanParams != null && distinctBooleanParams.contains(key)) {
sb.append(key);
sb.append(kvDelimiter);
sb.append("false");
}
} else {
sb.append(key);
if (!value.isEmpty()) {
sb.append(kvDelimiter);
// quoted
// @see https://github.com/cbeust/jcommander/issues/458
// JCommand no longer removing double quotes when parsing arguments
if (value.contains(kvDelimiter) && !value.startsWith("\"") && !value.endsWith("\"")) {
sb.append('\"').append(value).append('\"');
} else {
sb.append(value);
}
}
}
}
return sb.toString().trim();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy