use of org.apache.commons.collections4.trie.PatriciaTrie in project sling by apache.
the class ValidationModelRetrieverImpl method fillTrieForResourceType.
@Nonnull
private PatriciaTrie<ValidationModel> fillTrieForResourceType(@Nonnull String resourceType) {
// create a new (empty) trie
PatriciaTrie<ValidationModel> modelsForResourceType = new PatriciaTrie<ValidationModel>();
// lowest ranked model provider inserts first (i.e. higher ranked should overwrite)
for (ValidationModelProvider modelProvider : modelProviders) {
LOG.debug("Retrieving validation models with resource type {} from provider {}...", resourceType, modelProvider.getClass().getName());
List<ValidationModel> models = modelProvider.getValidationModels(resourceType);
for (ValidationModel model : models) {
for (String applicablePath : model.getApplicablePaths()) {
LOG.debug("Found validation model for resource type {} for applicable path {}", resourceType, applicablePath);
modelsForResourceType.put(applicablePath, model);
}
}
if (models.isEmpty()) {
LOG.debug("Found no validation model with resource type {} from provider {}", resourceType, modelProvider.getClass().getName());
}
}
return modelsForResourceType;
}
use of org.apache.commons.collections4.trie.PatriciaTrie in project ORCID-Source by ORCID.
the class IdentifierTypeManagerImpl method queryByPrefix.
/**
* Queries the identifier name and description fields for words that START WITH query.
* Returns an immutable list of matching types.
* Null locale will result in Locale.ENGLISH
*/
@Override
@Cacheable("identifier-types-map-prefix")
public List<IdentifierType> queryByPrefix(String query, Locale loc) {
Map<String, IdentifierType> results = new HashMap<String, IdentifierType>();
Map<String, IdentifierType> types = fetchIdentifierTypesByAPITypeName(loc);
// stick them in a trie so we can do a deep prefix search
PatriciaTrie<Set<IdentifierType>> trie = new PatriciaTrie<Set<IdentifierType>>();
for (String type : types.keySet()) {
IdentifierType t = types.get(type);
if (!trie.containsKey(t.getName().toLowerCase()))
trie.put(t.getName().toLowerCase(), new HashSet<IdentifierType>());
trie.get(t.getName().toLowerCase()).add(t);
for (String s : t.getDescription().toLowerCase().split(" ")) {
if (!trie.containsKey(s))
trie.put(s, new HashSet<IdentifierType>());
trie.get(s).add(t);
}
}
// dedupe and sort
SortedMap<String, Set<IdentifierType>> sorted = trie.prefixMap(query.toLowerCase());
for (Set<IdentifierType> set : sorted.values()) {
for (IdentifierType t : set) {
if (!results.containsKey(t.getDescription().toLowerCase()))
results.put(t.getDescription().toLowerCase(), t);
}
}
// put anything that starts with query at the top of the list.
Builder<IdentifierType> builder = new Builder<IdentifierType>();
for (IdentifierType t : results.values()) {
if (t.getDescription().toLowerCase().startsWith(query.toLowerCase())) {
builder.add(t);
}
}
for (IdentifierType t : results.values()) {
if (!t.getDescription().toLowerCase().startsWith(query.toLowerCase())) {
builder.add(t);
}
}
return builder.build();
}
Aggregations