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

org.bklab.flow.util.url.QueryParameterUtil Maven / Gradle / Ivy

There is a newer version: 22.0.1
Show newest version
package org.bklab.flow.util.url;

import com.vaadin.flow.function.SerializableFunction;
import com.vaadin.flow.router.BeforeEvent;
import com.vaadin.flow.router.Location;
import com.vaadin.flow.router.QueryParameters;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class QueryParameterUtil {

    private final Map> parameters;
    private final String queryString;

    public QueryParameterUtil(Location location) {
        this(location.getQueryParameters());
    }

    public QueryParameterUtil(BeforeEvent beforeEnterEvent) {
        this(beforeEnterEvent.getLocation().getQueryParameters());
    }

    public QueryParameterUtil(QueryParameters queryParameters) {
        this.parameters = queryParameters.getParameters();
        this.queryString = queryParameters.getQueryString();
    }

    public int getInt(String name) {
        try {
            return Integer.parseInt(get(name));
        } catch (Exception e) {
            return 0;
        }
    }

    public long getLong(String name) {
        try {
            return Long.parseLong(get(name));
        } catch (Exception e) {
            return 0;
        }
    }

    public String get(String name) {
        return Optional.ofNullable(parameters.get(name)).flatMap(s -> s.stream().findFirst()).orElse(null);
    }

    public String decode(String name) {
        return Optional.ofNullable(get(name)).map(value -> URLDecoder.decode(value, StandardCharsets.UTF_8)).orElse(null);
    }

    public Optional getOptional(String name) {
        return Optional.ofNullable(parameters.get(name)).flatMap(s -> s.stream().findFirst());
    }

    public List getList(String name) {
        return Optional.ofNullable(parameters.get(name)).orElse(new ArrayList<>());
    }

    public  List getList(String name, SerializableFunction stringTSerializableFunction) {
        List list = new ArrayList<>();
        for (String s : getList(name)) {
            try {
                list.add(stringTSerializableFunction.apply(s));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public Map> getParameters() {
        return parameters;
    }

    public String getQueryString() {
        return queryString;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy