felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickh...@apache.org
Subject svn commit: r899755 - in /felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver: CapabilityImpl.java cs/CapabilitySet.java
Date Fri, 15 Jan 2010 18:57:31 GMT
Author: rickhall
Date: Fri Jan 15 18:57:27 2010
New Revision: 899755

URL: http://svn.apache.org/viewvc?rev=899755&view=rev
Log:
Finish support for multi-valued attributes.

Modified:
    felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/CapabilityImpl.java
    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/CapabilityImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/CapabilityImpl.java?rev=899755&r1=899754&r2=899755&view=diff
==============================================================================
--- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/CapabilityImpl.java
(original)
+++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/CapabilityImpl.java
Fri Jan 15 18:57:27 2010
@@ -68,50 +68,53 @@
         return this;
     }
 
-    public CapabilityImpl with(String attr)
+    public CapabilityImpl with(String attrStr)
     {
-        int idx = attr.indexOf('=');
-        if (idx > 0)
-        {
-            String n = attr.substring(0, idx);
-            String v = attr.substring(idx + 1);
-            if (getAttribute(n) == null)
-            {
-                if (n.equalsIgnoreCase(Constants.VERSION_ATTRIBUTE)
-                    || n.equalsIgnoreCase(Constants.BUNDLE_VERSION_ATTRIBUTE))
-                {
-                    m_attrs.add(new Attribute(n, Version.parseVersion(v), false));
-                }
-                else
-                {
-                    m_attrs.add(new Attribute(n, v, false));
-                }
-            }
-        }
+        with(attrStr, false);
         return this;
     }
 
-    public CapabilityImpl withMandatory(String attr)
+    public CapabilityImpl withMandatory(String attrStr)
     {
-        int idx = attr.indexOf('=');
+        with(attrStr, true);
+        return this;
+    }
+
+    private void with(String attrStr, boolean mandatory)
+    {
+        int idx = attrStr.indexOf('=');
         if (idx > 0)
         {
-            String n = attr.substring(0, idx);
-            String v = attr.substring(idx + 1);
-            if (getAttribute(n) == null)
+            String n = attrStr.substring(0, idx);
+            String v = attrStr.substring(idx + 1);
+            Object value = v;
+            if (n.equalsIgnoreCase(Constants.VERSION_ATTRIBUTE)
+                || n.equalsIgnoreCase(Constants.BUNDLE_VERSION_ATTRIBUTE))
             {
-                if (n.equalsIgnoreCase(Constants.VERSION_ATTRIBUTE)
-                    || n.equalsIgnoreCase(Constants.BUNDLE_VERSION_ATTRIBUTE))
+                value = Version.parseVersion(v);
+            }
+            Attribute existing = getAttribute(n);
+            if (existing == null)
+            {
+                m_attrs.add(new Attribute(n, value, mandatory));
+            }
+            else
+            {
+                if (existing.getValue() instanceof List)
                 {
-                    m_attrs.add(new Attribute(n, Version.parseVersion(v), true));
+                    ((List) existing.getValue()).add(value);
                 }
                 else
                 {
-                    m_attrs.add(new Attribute(n, v, true));
+                    m_attrs.remove(existing);
+                    Attribute aggregate =
+                        new Attribute(existing.getName(), new ArrayList(), mandatory);
+                    ((List) aggregate.getValue()).add(existing.getValue());
+                    ((List) aggregate.getValue()).add(value);
+                    m_attrs.add(aggregate);
                 }
             }
         }
-        return this;
     }
 
     public Directive getDirective(String name)

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=899755&r1=899754&r2=899755&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
Fri Jan 15 18:57:27 2010
@@ -158,8 +158,7 @@
                     if (attr != null)
                     {
                         Object lhs = attr.getValue();
-                        Object rhs = coerceType(lhs, (String) sf.getValue());
-                        if (compare(lhs, rhs, sf.getOperation()))
+                        if (compare(lhs, (String) sf.getValue(), sf.getOperation()))
                         {
                             matches.add(cap);
                         }
@@ -215,8 +214,7 @@
             if (attr != null)
             {
                 Object lhs = attr.getValue();
-                Object rhs = coerceType(lhs, (String) sf.getValue());
-                matched = compare(lhs, rhs, sf.getOperation());
+                matched = compare(lhs, (String) sf.getValue(), sf.getOperation());
             }
         }
 
@@ -275,12 +273,13 @@
 
     private static final Class[] STRING_CLASS = new Class[] { String.class };
 
-    private static boolean compare(Object lhs, Object rhs, int op)
+    private static boolean compare(Object lhs, String rhsString, int op)
     {
         // If the type is comparable, then we can just return the
         // result immediately.
         if (lhs instanceof Comparable)
         {
+            Object rhs = coerceType(lhs, rhsString);
             switch (op)
             {
                 case SimpleFilter.EQ :
@@ -299,6 +298,7 @@
         // Booleans do not implement comparable, so special case them.
         else if (lhs instanceof Boolean)
         {
+            Object rhs = coerceType(lhs, rhsString);
             switch (op)
             {
                 case SimpleFilter.EQ :
@@ -328,7 +328,7 @@
             Object[] array = (Object[]) lhs;
             for (int i = 0; i < array.length; i++)
             {
-                if (compare(array[i], rhs, op))
+                if (compare(array[i], rhsString, op))
                 {
                     return true;
                 }
@@ -340,7 +340,7 @@
         {
             for (Iterator iter = ((Collection) lhs).iterator(); iter.hasNext(); )
             {
-                if (compare(iter.next(), rhs, op))
+                if (compare(iter.next(), rhsString, op))
                 {
                     return true;
                 }
@@ -349,7 +349,7 @@
 
         // Since we cannot identify the LHS type, then we can only perform
         // equality comparison.
-        return lhs.equals(rhs);
+        return lhs.equals(coerceType(lhs, rhsString));
     }
 
     private static Object coerceType(Object lhs, String rhsString)



Mime
View raw message