Search in sources :

Example 1 with PatriciaTrie

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;
}
Also used : MergedValidationModel(org.apache.sling.validation.impl.model.MergedValidationModel) ValidationModel(org.apache.sling.validation.model.ValidationModel) PatriciaTrie(org.apache.commons.collections4.trie.PatriciaTrie) ValidationModelProvider(org.apache.sling.validation.model.spi.ValidationModelProvider) Nonnull(javax.annotation.Nonnull)

Example 2 with PatriciaTrie

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();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) PatriciaTrie(org.apache.commons.collections4.trie.PatriciaTrie) Builder(com.google.common.collect.ImmutableList.Builder) IdentifierType(org.orcid.pojo.IdentifierType) HashSet(java.util.HashSet) Cacheable(org.springframework.cache.annotation.Cacheable)

Aggregations

PatriciaTrie (org.apache.commons.collections4.trie.PatriciaTrie)2 Builder (com.google.common.collect.ImmutableList.Builder)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Nonnull (javax.annotation.Nonnull)1 MergedValidationModel (org.apache.sling.validation.impl.model.MergedValidationModel)1 ValidationModel (org.apache.sling.validation.model.ValidationModel)1 ValidationModelProvider (org.apache.sling.validation.model.spi.ValidationModelProvider)1 IdentifierType (org.orcid.pojo.IdentifierType)1 Cacheable (org.springframework.cache.annotation.Cacheable)1