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

ca.ibodrov.mica.server.data.QueryParams Maven / Gradle / Ivy

The newest version!
package ca.ibodrov.mica.server.data;

import java.net.URLDecoder;
import java.util.*;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.*;

public class QueryParams {

    private final Map> params;

    public QueryParams(String s) {
        this.params = parse(s);
    }

    public Optional getFirst(String key) {
        return Optional.ofNullable(params.get(key))
                .flatMap(r -> Optional.ofNullable(r.get(0)));
    }

    private static Map> parse(String s) {
        if (s == null || s.isBlank()) {
            return Map.of();
        }
        return Arrays.stream(s.split("&"))
                .map(QueryParams::split)
                .collect(groupingBy(Map.Entry::getKey, HashMap::new, mapping(Map.Entry::getValue, toList())));
    }

    private static AbstractMap.SimpleEntry split(String it) {
        var idx = it.indexOf("=");
        var key = idx > 0 ? it.substring(0, idx) : it;
        var value = idx > 0 && it.length() > idx + 1 ? it.substring(idx + 1) : null;
        return new AbstractMap.SimpleEntry<>(
                URLDecoder.decode(key, UTF_8),
                value != null ? URLDecoder.decode(value, UTF_8) : null);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy