com.github.gimmi.AnyMapBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of any Show documentation
Show all versions of any Show documentation
An immutable data structure that provide an uniform interface to map, list or scalar data
package com.github.gimmi;
import java.util.Map;
import java.util.TreeMap;
import static com.github.gimmi.Utils.stripToEmpty;
public class AnyMapBuilder {
private final TreeMap map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
public AnyMapBuilder put(String key, Any value) {
key = stripToEmpty(key);
if (key != null) {
map.computeIfAbsent(key, k -> new AnyListBuilder()).put(value);
}
return this;
}
public int count() {
return map.values().stream()
.filter(x -> x.count() > 0)
.mapToInt(x -> 1)
.sum();
}
public Any build() {
if (map.isEmpty()) {
return Any.NULL;
} else {
TreeMap mapCopy = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
for (Map.Entry entry : map.entrySet()) {
if (entry.getValue().count() > 0) {
mapCopy.put(entry.getKey(), entry.getValue().build());
}
}
return new Any(null, mapCopy, null);
}
}
}