org.bklab.flow.util.url.QueryParameterBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluent-vaadin-flow Show documentation
Show all versions of fluent-vaadin-flow Show documentation
Broderick Labs for fluent vaadin flow. Inherits common Vaadin components.
package org.bklab.flow.util.url;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.function.SerializableSupplier;
import com.vaadin.flow.router.QueryParameters;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class QueryParameterBuilder implements SerializableSupplier {
private final Map> map;
public QueryParameterBuilder() {
this.map = new LinkedHashMap<>();
}
public QueryParameterBuilder(QueryParameters queryParameters) {
this.map = queryParameters.getParameters();
}
@SafeVarargs
public final QueryParameterBuilder add(Function function, String name, T... params) {
this.map.put(name, params == null || params.length < 1
? Collections.emptyList()
: Stream.of(params).map(function).collect(Collectors.toList()));
return this;
}
public QueryParameterBuilder add(String name, Object... params) {
this.map.put(name, params == null || params.length < 1
? Collections.emptyList()
: Stream.of(params).map(String::valueOf).collect(Collectors.toList()));
return this;
}
public QueryParameterBuilder encode(String name, Object ... params) {
return add(name, Arrays.stream(params).map(String::valueOf)
.map(s -> URLEncoder.encode(s, StandardCharsets.UTF_8))
.collect(Collectors.toList()).toArray(new Object[]{}));
}
public QueryParameterBuilder add(boolean add, String name, Object... params) {
return add ? add(name, params) : this;
}
@SafeVarargs
public final QueryParameterBuilder add(Predicate add, String name, T... params) {
return add(name, params == null ? Collections.emptyList()
: Stream.of(params).filter(add).collect(Collectors.toList()));
}
public QueryParameterBuilder peek(Consumer consumer) {
consumer.accept(this);
return this;
}
@Override
public QueryParameters get() {
return new QueryParameters(map);
}
public > Consumer getNavigateConsumer(C target) {
return ui -> navigate(ui, target);
}
public > QueryParameterBuilder navigate(C target) {
return navigate(UI.getCurrent(), target);
}
public > QueryParameterBuilder navigate(UI ui, C target) {
ui.getInternals().getRouter().getRegistry().getTargetUrl(target).ifPresent(uri -> ui.navigate(uri, get()));
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy