Search in sources :

Example 1 with NotExpression

use of org.osgi.service.repository.NotExpression in project bnd by bndtools.

the class BaseRepository method dispatch.

private void dispatch(RequirementExpression expression, Set<Resource> providers) {
    if (expression instanceof IdentityExpression) {
        Map<Requirement, Collection<Capability>> capabilities = findProviders(Collections.singleton(((IdentityExpression) expression).getRequirement()));
        for (Collection<Capability> caps : capabilities.values()) {
            for (Capability c : caps) providers.add(c.getResource());
        }
    } else if (expression instanceof OrExpression) {
        for (RequirementExpression re : ((OrExpression) expression).getRequirementExpressions()) dispatch(re, providers);
    } else if (expression instanceof AndExpression) {
        List<RequirementExpression> requirementExpressions = ((AndExpression) expression).getRequirementExpressions();
        if (requirementExpressions.isEmpty())
            return;
        if (requirementExpressions.size() == 1) {
            dispatch(requirementExpressions.get(0), providers);
            return;
        }
        Set<Resource> subset = new HashSet<>();
        dispatch(requirementExpressions.get(0), subset);
        for (int i = 1; i < requirementExpressions.size(); i++) {
            for (Iterator<Resource> it = subset.iterator(); it.hasNext(); ) {
                Resource resource = it.next();
                RequirementExpression re = requirementExpressions.get(i);
                if (!matches(re, resource)) {
                    it.remove();
                    if (subset.isEmpty())
                        return;
                }
            }
        }
        providers.addAll(subset);
    } else if (expression instanceof NotExpression) {
        Set<Resource> allSet = new HashSet<>();
        dispatch(all, allSet);
        RequirementExpression re = ((NotExpression) expression).getRequirementExpression();
        for (Iterator<Resource> it = allSet.iterator(); it.hasNext(); ) {
            Resource resource = it.next();
            if (matches(re, resource)) {
                it.remove();
                if (allSet.isEmpty())
                    return;
            }
        }
        providers.addAll(allSet);
    } else
        throw new UnsupportedOperationException("Unknown expression type " + expression.getClass());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Capability(org.osgi.resource.Capability) Resource(org.osgi.resource.Resource) NotExpression(org.osgi.service.repository.NotExpression) OrExpression(org.osgi.service.repository.OrExpression) Requirement(org.osgi.resource.Requirement) AndExpression(org.osgi.service.repository.AndExpression) RequirementExpression(org.osgi.service.repository.RequirementExpression) Iterator(java.util.Iterator) Collection(java.util.Collection) IdentityExpression(org.osgi.service.repository.IdentityExpression) HashSet(java.util.HashSet)

Aggregations

Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Capability (org.osgi.resource.Capability)1 Requirement (org.osgi.resource.Requirement)1 Resource (org.osgi.resource.Resource)1 AndExpression (org.osgi.service.repository.AndExpression)1 IdentityExpression (org.osgi.service.repository.IdentityExpression)1 NotExpression (org.osgi.service.repository.NotExpression)1 OrExpression (org.osgi.service.repository.OrExpression)1 RequirementExpression (org.osgi.service.repository.RequirementExpression)1