felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1186756 - in /felix/trunk/configadmin/src: main/java/org/apache/felix/cm/impl/RankingComparator.java test/java/org/apache/felix/cm/impl/RankingComparatorTest.java
Date Thu, 20 Oct 2011 12:16:20 GMT
Author: fmeschbe
Date: Thu Oct 20 12:16:19 2011
New Revision: 1186756

URL: http://svn.apache.org/viewvc?rev=1186756&view=rev
Log:
FELIX-3175 Fix the RankingComparator to accomodate for services to be sorted in collections
(array, set) according to regular service.ranking property as well as service.cmRanking (for
ConfigurationPlugins) ordering. Added unit tests to make sure the comparator works as expected.

Added:
    felix/trunk/configadmin/src/test/java/org/apache/felix/cm/impl/RankingComparatorTest.java
Modified:
    felix/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/RankingComparator.java

Modified: felix/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/RankingComparator.java
URL: http://svn.apache.org/viewvc/felix/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/RankingComparator.java?rev=1186756&r1=1186755&r2=1186756&view=diff
==============================================================================
--- felix/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/RankingComparator.java
(original)
+++ felix/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/RankingComparator.java
Thu Oct 20 12:16:19 2011
@@ -23,66 +23,88 @@ import java.util.Comparator;
 
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.ConfigurationPlugin;
 
 
 /**
- * The <code>RankingComparator</code> TODO
+ * The <code>RankingComparator</code> may be used to maintain sorted
+ * sets or to sort arrays such that the first element in the set or
+ * array is the one to use first and the last elementis the one to
+ * use last.
  */
-public class RankingComparator implements Comparator
+abstract class RankingComparator implements Comparator
 {
 
-    private final boolean naturalOrder;
-    private final String rankProperty;
-
-
-    public RankingComparator(boolean naturalOrder)
-    {
-        this( naturalOrder, Constants.SERVICE_RANKING );
-    }
-
-
-    public RankingComparator( boolean naturalOrder, String rankProperty )
+    /**
+     * Implements a comparator to sort arrays and sets according to the
+     * specification of the <code>service.ranking</code> property. This
+     * results in collections whose first element has the highest ranking
+     * and the last element has the lowest ranking. Thus the results of
+     * this comparator are as follows:
+     * <ul>
+     * <li><code>&lt; 0</code> if obj1 has higher ranking than obj2</li>
+     * <li><code>== 0</code> if obj1 and obj2 reference the same service</li>
+     * <li><code>&gt; 0</code> if obj1 has lower ranking than obj2</li>
+     * </ul>
+     */
+    static Comparator SRV_RANKING = new RankingComparator()
     {
-        this.naturalOrder = naturalOrder;
-        this.rankProperty = rankProperty;
-    }
-
+        public int compare( Object obj1, Object obj2 )
+        {
+            final long id1 = this.getLong( ( ServiceReference ) obj1, Constants.SERVICE_ID
);
+            final long id2 = this.getLong( ( ServiceReference ) obj2, Constants.SERVICE_ID
);
 
-    public int compare( Object obj1, Object obj2 )
+            if ( id1 == id2 )
+            {
+                return 0;
+            }
+
+            final long rank1 = this.getLong( ( ServiceReference ) obj1, Constants.SERVICE_RANKING
);
+            final long rank2 = this.getLong( ( ServiceReference ) obj2, Constants.SERVICE_RANKING
);
+
+            if ( rank1 == rank2 )
+            {
+                return ( id1 < id2 ) ? -1 : 1;
+            }
+
+            return ( rank1 > rank2 ) ? -1 : 1;
+        }
+
+    };
+
+
+    /**
+     * Implements a comparator to sort arrays and sets according to the
+     * specification of the <code>service.cmRanking</code> property in
+     * the Configuration Admin specification. This results in collections
+     * where the first element has the lowest ranking value and the last
+     * element has the highest ranking value. Order amongst elements with
+     * the same ranking value is left undefined. Thus the results of this
+     * comparator are as follows:
+     * <ul>
+     * <li><code>&lt; 0</code> if obj1 has lower ranking than obj2</li>
+     * <li><code>== 0</code> if obj1 and obj2 have the same ranking</li>
+     * <li><code>&gt; 0</code> if obj1 has higher ranking than obj2</li>
+     * </ul>
+     */
+    static Comparator CM_RANKING = new RankingComparator()
     {
-        if ( obj1.equals( obj2 ) )
+        public int compare( Object obj1, Object obj2 )
         {
-            return 0;
-        }
+            final long rank1 = this.getLong( ( ServiceReference ) obj1, ConfigurationPlugin.CM_RANKING
);
+            final long rank2 = this.getLong( ( ServiceReference ) obj2, ConfigurationPlugin.CM_RANKING
);
 
-        long rank1 = this.getLong( ( ServiceReference ) obj1, rankProperty );
-        long rank2 = this.getLong( ( ServiceReference ) obj2, rankProperty );
-        boolean order = naturalOrder;
+            if ( rank1 == rank2 )
+            {
+                return 0;
+            }
 
-        // use service id, if rankings are equal
-        if ( rank1 == rank2 )
-        {
-            rank1 = this.getLong( ( ServiceReference ) obj1, Constants.SERVICE_ID );
-            rank2 = this.getLong( ( ServiceReference ) obj2, Constants.SERVICE_ID );
-            order = false; // always order lower service.id before higher
+            return ( rank1 < rank2 ) ? -1 : 1;
         }
 
-        if ( rank1 == rank2 )
-        {
-            return 0;
-        }
-        else if ( order )
-        {
-            return ( rank1 > rank2 ) ? 1 : -1;
-        }
-        else
-        {
-            return ( rank1 < rank2 ) ? 1 : -1;
-        }
-    }
-
+    };
 
-    private long getLong( ServiceReference sr, String property )
+    protected long getLong( ServiceReference sr, String property )
     {
         Object rankObj = sr.getProperty( property );
         if ( rankObj instanceof Number )

Added: felix/trunk/configadmin/src/test/java/org/apache/felix/cm/impl/RankingComparatorTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/configadmin/src/test/java/org/apache/felix/cm/impl/RankingComparatorTest.java?rev=1186756&view=auto
==============================================================================
--- felix/trunk/configadmin/src/test/java/org/apache/felix/cm/impl/RankingComparatorTest.java
(added)
+++ felix/trunk/configadmin/src/test/java/org/apache/felix/cm/impl/RankingComparatorTest.java
Thu Oct 20 12:16:19 2011
@@ -0,0 +1,261 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.cm.impl;
+
+
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+
+import junit.framework.TestCase;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.ConfigurationPlugin;
+
+
+public class RankingComparatorTest extends TestCase
+{
+
+    private final Comparator srvRank = RankingComparator.SRV_RANKING;
+    private final Comparator cmRank = RankingComparator.CM_RANKING;
+
+
+    public void test_service_ranking_no_property()
+    {
+        ServiceReference r1 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING,
null );
+        ServiceReference r2 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING,
null );
+        ServiceReference r3 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING,
null );
+
+        assertTrue( srvRank.compare( r1, r1 ) == 0 );
+        assertTrue( srvRank.compare( r1, r2 ) < 0 );
+        assertTrue( srvRank.compare( r1, r3 ) < 0 );
+
+        assertTrue( srvRank.compare( r2, r1 ) > 0 );
+        assertTrue( srvRank.compare( r2, r2 ) == 0 );
+        assertTrue( srvRank.compare( r2, r3 ) < 0 );
+
+        assertTrue( srvRank.compare( r3, r1 ) > 0 );
+        assertTrue( srvRank.compare( r3, r2 ) > 0 );
+        assertTrue( srvRank.compare( r3, r3 ) == 0 );
+
+        assertTrue( cmRank.compare( r1, r1 ) == 0 );
+        assertTrue( cmRank.compare( r1, r2 ) == 0 );
+        assertTrue( cmRank.compare( r1, r3 ) == 0 );
+
+        assertTrue( cmRank.compare( r2, r1 ) == 0 );
+        assertTrue( cmRank.compare( r2, r2 ) == 0 );
+        assertTrue( cmRank.compare( r2, r3 ) == 0 );
+
+        assertTrue( cmRank.compare( r3, r1 ) == 0 );
+        assertTrue( cmRank.compare( r3, r2 ) == 0 );
+        assertTrue( cmRank.compare( r3, r3 ) == 0 );
+    }
+
+
+    public void test_service_ranking_property()
+    {
+        ServiceReference r1 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING,
new Integer( 100 ) );
+        ServiceReference r2 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING,
new Integer( -100 ) );
+        ServiceReference r3 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING,
null );
+
+        assertTrue( srvRank.compare( r1, r1 ) == 0 );
+        assertTrue( srvRank.compare( r1, r2 ) < 0 );
+        assertTrue( srvRank.compare( r1, r3 ) < 0 );
+
+        assertTrue( srvRank.compare( r2, r1 ) > 0 );
+        assertTrue( srvRank.compare( r2, r2 ) == 0 );
+        assertTrue( srvRank.compare( r2, r3 ) > 0 );
+
+        assertTrue( srvRank.compare( r3, r1 ) > 0 );
+        assertTrue( srvRank.compare( r3, r2 ) < 0 );
+        assertTrue( srvRank.compare( r3, r3 ) == 0 );
+    }
+
+
+    public void test_service_cm_ranking_property()
+    {
+        ServiceReference r1 = new MockServiceReference()
+            .setProperty( ConfigurationPlugin.CM_RANKING, new Integer( 100 ) );
+        ServiceReference r2 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING,
+            new Integer( -100 ) );
+        ServiceReference r3 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING,
null );
+
+        assertTrue( cmRank.compare( r1, r1 ) == 0 );
+        assertTrue( cmRank.compare( r1, r2 ) > 0 );
+        assertTrue( cmRank.compare( r1, r3 ) > 0 );
+
+        assertTrue( cmRank.compare( r2, r1 ) < 0 );
+        assertTrue( cmRank.compare( r2, r2 ) == 0 );
+        assertTrue( cmRank.compare( r2, r3 ) < 0 );
+
+        assertTrue( cmRank.compare( r3, r1 ) < 0 );
+        assertTrue( cmRank.compare( r3, r2 ) > 0 );
+        assertTrue( cmRank.compare( r3, r3 ) == 0 );
+    }
+
+
+    public void test_service_ranking_sort()
+    {
+        ServiceReference r1 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING,
new Integer( 100 ) );
+        ServiceReference r2 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING,
new Integer( -100 ) );
+        ServiceReference r3 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING,
null );
+        ServiceReference[] refs =
+            { r1, r2, r3 };
+
+        assertSame( r1, refs[0] );
+        assertSame( r2, refs[1] );
+        assertSame( r3, refs[2] );
+
+        Arrays.sort( refs, srvRank );
+
+        assertSame( r1, refs[0] );
+        assertSame( r2, refs[2] );
+        assertSame( r3, refs[1] );
+    }
+
+
+    public void test_service_ranking_set()
+    {
+        ServiceReference r1 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING,
new Integer( 100 ) );
+        ServiceReference r2 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING,
new Integer( -100 ) );
+        ServiceReference r3 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING,
null );
+
+        Set refSet = new TreeSet( srvRank );
+        refSet.add( r1 );
+        refSet.add( r2 );
+        refSet.add( r3 );
+
+        Iterator refIter = refSet.iterator();
+        assertSame( r1, refIter.next() );
+        assertSame( r3, refIter.next() );
+        assertSame( r2, refIter.next() );
+    }
+
+
+    public void test_service_cm_ranking_sort()
+    {
+        ServiceReference r1 = new MockServiceReference()
+            .setProperty( ConfigurationPlugin.CM_RANKING, new Integer( 100 ) );
+        ServiceReference r2 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING,
+            new Integer( -100 ) );
+        ServiceReference r3 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING,
null );
+        ServiceReference[] refs =
+            { r1, r2, r3 };
+
+        assertSame( r1, refs[0] );
+        assertSame( r2, refs[1] );
+        assertSame( r3, refs[2] );
+
+        Arrays.sort( refs, cmRank );
+
+        assertSame( r1, refs[2] );
+        assertSame( r2, refs[0] );
+        assertSame( r3, refs[1] );
+    }
+
+
+    public void test_service_cm_ranking_set()
+    {
+        ServiceReference r1 = new MockServiceReference()
+            .setProperty( ConfigurationPlugin.CM_RANKING, new Integer( 100 ) );
+        ServiceReference r2 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING,
+            new Integer( -100 ) );
+        ServiceReference r3 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING,
null );
+
+        Set refSet = new TreeSet( cmRank );
+        refSet.add( r1 );
+        refSet.add( r2 );
+        refSet.add( r3 );
+
+        Iterator refIter = refSet.iterator();
+        assertSame( r2, refIter.next() );
+        assertSame( r3, refIter.next() );
+        assertSame( r1, refIter.next() );
+    }
+
+    private static class MockServiceReference implements ServiceReference
+    {
+
+        static long id = 0;
+
+        private final Map props = new HashMap();
+
+        {
+            props.put( Constants.SERVICE_ID, new Long( id ) );
+            id++;
+        }
+
+
+        MockServiceReference setProperty( final String key, final Object value )
+        {
+            if ( value == null )
+            {
+                props.remove( key );
+            }
+            else
+            {
+                props.put( key, value );
+            }
+            return this;
+        }
+
+
+        public Object getProperty( String key )
+        {
+            return props.get( key );
+        }
+
+
+        public String[] getPropertyKeys()
+        {
+            return ( String[] ) props.keySet().toArray( new String[props.size()] );
+        }
+
+
+        public Bundle getBundle()
+        {
+            return null;
+        }
+
+
+        public Bundle[] getUsingBundles()
+        {
+            return null;
+        }
+
+
+        public boolean isAssignableTo( Bundle bundle, String className )
+        {
+            return false;
+        }
+
+
+        public String toString()
+        {
+            return "ServiceReference " + getProperty( Constants.SERVICE_ID );
+        }
+    }
+
+}



Mime
View raw message