felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickh...@apache.org
Subject svn commit: r899303 - /felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/cs/CapabilitySet.java
Date Thu, 14 Jan 2010 17:01:01 GMT
Author: rickhall
Date: Thu Jan 14 17:01:01 2010
New Revision: 899303

URL: http://svn.apache.org/viewvc?rev=899303&view=rev
Log:
Implement mandatory attributes for explicit capability matching.

Modified:
    felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/cs/CapabilitySet.java

Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/cs/CapabilitySet.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/cs/CapabilitySet.java?rev=899303&r1=899302&r2=899303&view=diff
==============================================================================
--- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/cs/CapabilitySet.java
(original)
+++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/cs/CapabilitySet.java
Thu Jan 14 17:01:01 2010
@@ -189,48 +189,76 @@
         return matches;
     }
 
-    private Set<Capability> matchMandatory(Set<Capability> caps, SimpleFilter
sf)
+    public static boolean matches(Capability cap, SimpleFilter sf)
     {
-        for (Iterator<Capability> it = caps.iterator(); it.hasNext(); )
+        boolean matched = true;
+
+        if (sf.getOperation() == SimpleFilter.AND)
         {
-            Capability cap = it.next();
-            List<Attribute> attrs = cap.getAttributes();
-            for (int attrIdx = 0; attrIdx < attrs.size(); attrIdx++)
+            // Evaluate each subfilter against the remaining capabilities.
+            // For AND we calculate the intersection of each subfilter.
+            // We can short-circuit the AND operation if there are no
+            // remaining capabilities.
+            List<SimpleFilter> sfs = (List<SimpleFilter>) sf.getValue();
+            for (int i = 0; matched && (i < sfs.size()); i++)
             {
-                if (attrs.get(attrIdx).isMandatory())
-                {
-                    if (!matchMandatory(attrs.get(attrIdx), sf))
-                    {
-                        it.remove();
-                        break;
-                    }
-                }
+                matched = matches(cap, sfs.get(i));
             }
         }
-        return caps;
-    }
-
-    private boolean matchMandatory(Attribute attr, SimpleFilter sf)
-    {
-        if ((sf.getOperation() == SimpleFilter.EQ)
-            && sf.getName().equals(attr.getName()))
+        else if (sf.getOperation() == SimpleFilter.OR)
         {
-            return true;
+            // Evaluate each subfilter against the remaining capabilities.
+            // For OR we calculate the union of each subfilter.
+            matched = false;
+            List<SimpleFilter> sfs = (List<SimpleFilter>) sf.getValue();
+            for (int i = 0; !matched && (i < sfs.size()); i++)
+            {
+                matched = matches(cap, sfs.get(i));
+            }
         }
-        else if ((sf.getOperation() == SimpleFilter.AND))
+        else if (sf.getOperation() == SimpleFilter.NOT)
         {
-            List list = (List) sf.getValue();
-            for (int i = 0; i < list.size(); i++)
+            // Evaluate each subfilter against the remaining capabilities.
+            // For OR we calculate the union of each subfilter.
+            List<SimpleFilter> sfs = (List<SimpleFilter>) sf.getValue();
+            for (int i = 0; i < sfs.size(); i++)
             {
-                SimpleFilter sf2 = (SimpleFilter) list.get(i);
-                if ((sf2.getOperation() == SimpleFilter.EQ)
-                    && sf2.getName().equals(attr.getName()))
+                matched = !(matches(cap, sfs.get(i)));
+            }
+        }
+        else
+        {
+            matched = false;
+            Attribute attr = cap.getAttribute(sf.getName());
+            Comparable value = (Comparable)
+                ((attr == null) ? null : cap.getAttribute(sf.getName()).getValue());
+            if (value != null)
+            {
+                switch (sf.getOperation())
                 {
-                    return true;
+                    case SimpleFilter.EQ:
+                        if (value.compareTo(coerceType(value, (String) sf.getValue())) ==
0)
+                        {
+                            matched = true;
+                        }
+                        break;
+                    case SimpleFilter.LTE:
+                        if (value.compareTo(coerceType(value, (String) sf.getValue())) <=
0)
+                        {
+                            matched = true;
+                        }
+                        break;
+                    case SimpleFilter.GTE:
+                        if (value.compareTo(coerceType(value, (String) sf.getValue())) >=
0)
+                        {
+                            matched = true;
+                        }
+                        break;
                 }
             }
         }
-        return false;
+
+        return matched && matchMandatory(cap, sf);
     }
 
     private static final Class[] STRING_CLASS = new Class[] { String.class };
@@ -276,75 +304,53 @@
         return rhsComparable;
     }
 
-    public static boolean matches(Capability cap, SimpleFilter sf)
+    private static Set<Capability> matchMandatory(Set<Capability> caps, SimpleFilter
sf)
     {
-        boolean matched = true;
-
-        if (sf.getOperation() == SimpleFilter.AND)
+        for (Iterator<Capability> it = caps.iterator(); it.hasNext(); )
         {
-            // Evaluate each subfilter against the remaining capabilities.
-            // For AND we calculate the intersection of each subfilter.
-            // We can short-circuit the AND operation if there are no
-            // remaining capabilities.
-            List<SimpleFilter> sfs = (List<SimpleFilter>) sf.getValue();
-            for (int i = 0; matched && (i < sfs.size()); i++)
+            Capability cap = it.next();
+            if (!matchMandatory(cap, sf))
             {
-                matched = matches(cap, sfs.get(i));
+                it.remove();
             }
         }
-        else if (sf.getOperation() == SimpleFilter.OR)
+        return caps;
+    }
+
+    private static boolean matchMandatory(Capability cap, SimpleFilter sf)
+    {
+        List<Attribute> attrs = cap.getAttributes();
+        for (int attrIdx = 0; attrIdx < attrs.size(); attrIdx++)
         {
-            // Evaluate each subfilter against the remaining capabilities.
-            // For OR we calculate the union of each subfilter.
-            matched = false;
-            List<SimpleFilter> sfs = (List<SimpleFilter>) sf.getValue();
-            for (int i = 0; !matched && (i < sfs.size()); i++)
+            if (attrs.get(attrIdx).isMandatory()
+                && !matchMandatory(attrs.get(attrIdx), sf))
             {
-                matched = matches(cap, sfs.get(i));
+                return false;
             }
         }
-        else if (sf.getOperation() == SimpleFilter.NOT)
+        return true;
+    }
+
+    private static boolean matchMandatory(Attribute attr, SimpleFilter sf)
+    {
+        if ((sf.getOperation() == SimpleFilter.EQ)
+            && sf.getName().equals(attr.getName()))
         {
-            // Evaluate each subfilter against the remaining capabilities.
-            // For OR we calculate the union of each subfilter.
-            List<SimpleFilter> sfs = (List<SimpleFilter>) sf.getValue();
-            for (int i = 0; i < sfs.size(); i++)
-            {
-                matched = !(matches(cap, sfs.get(i)));
-            }
+            return true;
         }
-        else
+        else if ((sf.getOperation() == SimpleFilter.AND))
         {
-            matched = false;
-            Attribute attr = cap.getAttribute(sf.getName());
-            Comparable value = (Comparable)
-                ((attr == null) ? null : cap.getAttribute(sf.getName()).getValue());
-            if (value != null)
+            List list = (List) sf.getValue();
+            for (int i = 0; i < list.size(); i++)
             {
-                switch (sf.getOperation())
+                SimpleFilter sf2 = (SimpleFilter) list.get(i);
+                if ((sf2.getOperation() == SimpleFilter.EQ)
+                    && sf2.getName().equals(attr.getName()))
                 {
-                    case SimpleFilter.EQ:
-                        if (value.compareTo(coerceType(value, (String) sf.getValue())) ==
0)
-                        {
-                            matched = true;
-                        }
-                        break;
-                    case SimpleFilter.LTE:
-                        if (value.compareTo(coerceType(value, (String) sf.getValue())) <=
0)
-                        {
-                            matched = true;
-                        }
-                        break;
-                    case SimpleFilter.GTE:
-                        if (value.compareTo(coerceType(value, (String) sf.getValue())) >=
0)
-                        {
-                            matched = true;
-                        }
-                        break;
+                    return true;
                 }
             }
         }
-
-        return matched;
+        return false;
     }
 }
\ No newline at end of file



Mime
View raw message