Skip to content
Snippets Groups Projects

Iss #62 - Resolve some of the code smell in the common lib

Merged Martin Lowe requested to merge malowe/eclipsefdn-api-common:malowe/main/62-2 into main
13 files
+ 161
183
Compare changes
  • Side-by-side
  • Inline
Files
13
@@ -13,7 +13,8 @@ package org.eclipsefoundation.core.helper;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
@@ -36,51 +37,50 @@ import io.quarkus.runtime.Startup;
@ApplicationScoped
public class ParameterHelper {
@Inject
Instance<UrlParameterNamespace> urlParamImpls;
@Inject
Instance<UrlParameterNamespace> urlParamImpls;
// maintain internal list of key names to reduce churn. These are only created
// at build time
private List<String> urlParamNames;
// maintain internal list of key names to reduce churn. These are only created
// at build time
private List<String> urlParamNames;
/**
* Create the list of parameter key names once upon creation of this class
*/
@PostConstruct
void initialize() {
// use array list for better random access + lookups
this.urlParamNames = new ArrayList<>();
// populate a list with each
urlParamImpls.forEach(
namespace -> namespace.getParameters().stream().forEach(param -> urlParamNames.add(param.getName())));
}
/**
* Create the list of parameter key names once upon creation of this class
*/
@PostConstruct
void initialize() {
// use array list for better random access + lookups
this.urlParamNames = new ArrayList<>();
// populate a list with each
urlParamImpls.forEach(namespace -> namespace.getParameters().stream().forEach(param -> urlParamNames.add(param.getName())));
}
/**
* Filters out unknown parameters by name/key. Creates and returns a new map
* with the known/tracked parameters. Tracked parameters are added through
* implementations of the {@link UrlParameterNamespace} interface.
*
* @param src multivalued parameter map to filter
* @return a map containing the filtered parameter values
*/
public MultivaluedMap<String, String> filterUnknownParameters(MultivaluedMap<String, String> src) {
MultivaluedMap<String, String> out = new MultivaluedMapImpl<>();
Set<String> keys = src.keySet();
for (String key : keys) {
if (urlParamNames.contains(key)) {
out.addAll(key, src.get(key));
}
}
return out;
}
/**
* Filters out unknown parameters by name/key. Creates and returns a new map with the known/tracked parameters. Tracked
* parameters are added through implementations of the {@link UrlParameterNamespace} interface.
*
* @param src multivalued parameter map to filter
* @return a map containing the filtered parameter values
*/
public MultivaluedMap<String, String> filterUnknownParameters(MultivaluedMap<String, String> src) {
return src
.entrySet()
.stream()
.filter(e -> urlParamNames.contains(e.getKey()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (prevVal, addVals) -> {
// merge the 2 lists and return
prevVal.addAll(addVals);
return prevVal;
}, MultivaluedMapImpl<String, String>::new));
}
/**
* Returns a list of parameter names, as generated from reading in the
* parameters present in the {@link UrlParameterNamespace} implementations.
*
* @return list of tracked parameter names.
*/
public List<String> getValidParameters() {
return new ArrayList<>(urlParamNames);
}
/**
* Returns a list of parameter names, as generated from reading in the parameters present in the
* {@link UrlParameterNamespace} implementations.
*
* @return list of tracked parameter names.
*/
public List<String> getValidParameters() {
return new ArrayList<>(urlParamNames);
}
}
Loading