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

com.github.gimmi.AnyMapBuilder Maven / Gradle / Ivy

Go to download

An immutable data structure that provide an uniform interface to map, list or scalar data

There is a newer version: 0.4.0
Show newest version
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);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy