io.github.ericdriggs.reportcard.util.StringMapUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reportcard-model Show documentation
Show all versions of reportcard-model Show documentation
test report metrics and trend analysis reporting :: reportcard-model
package io.github.ericdriggs.reportcard.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.ericdriggs.reportcard.mappers.SharedObjectMappers;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
@Slf4j
public enum StringMapUtil {
;//static methods only
private final static ObjectMapper mapper = SharedObjectMappers.simpleObjectMapper;
@SneakyThrows(JsonProcessingException.class)
public static TreeMap jsonToMap(String json) {
TypeReference> typeRef
= new TypeReference>() {
};
return mapper.readValue(json, typeRef);
}
public static String valuesOnlyColonSeparated(TreeMap map) {
//preserving treemap key order for values
List values = new ArrayList<>();
for (Map.Entry entry : map.entrySet()) {
values.add(entry.getValue());
}
return String.join(":", values);
}
public static String jsonToDefinitionList(String json) {
StringBuilder builder = new StringBuilder();
TreeMap map = jsonToMap(json);
for (Map.Entry entry : map.entrySet()) {
builder.append("" + entry.getKey() + " " + entry.getValue() + " ");
}
return "" + builder + "
";
}
public static TreeMap stringToMap(String str) {
if (str == null) {
return new TreeMap<>();
}
String[] kvs = str.split(",");
if (kvs.length == 0) {
return new TreeMap<>();
}
TreeMap map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
for (String s : kvs) {
String[] kv = s.split("=");
if (kv.length == 2) {
map.put(kv[0], kv[1]);
} else {
log.warn("unable to parse as key=value: " + s);
}
}
return map;
}
public static TreeMap lower(TreeMap map) {
if (map == null || map.isEmpty()) {
return map;
}
TreeMap ret = new TreeMap<>();
for (Map.Entry entry : map.entrySet()) {
ret.put(entry.getKey().toLowerCase(), entry.getValue().toLowerCase());
}
return ret;
}
public static String toQueryParams(Map paramMap) {
if (paramMap == null || paramMap.isEmpty()) {
return "";
}
List params = new ArrayList<>();
for (Map.Entry entry : paramMap.entrySet()) {
params.add(entry.getKey() + "=" + entry.getValue());
}
return "?" + String.join(",", params);
}
public static TreeMap> fromColonSeparated(Collection colonSeparatedValues) {
TreeMap> ret = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
for (String colonSeparatedValue : colonSeparatedValues) {
String[] parts = colonSeparatedValue.split(":");
if (parts.length != 2) {
log.warn("skipping since unable to parse colonSeparatedValue: " + colonSeparatedValue);
}
final String key = parts[0].trim();
final String value = parts[1].trim();
if (!ret.containsKey(key)) {
ret.put(key, new TreeSet<>());
}
ret.get(key).add(value);
}
return ret;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy