You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:24:28 UTC

[sling-org-apache-sling-commons-osgi] 24/29: SLING-1431 : Utility method to get the service ranking

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.commons.osgi-2.0.6
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-osgi.git

commit 2602d927d6a5e63e7a39cd7ca8d291c6a072a262
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Mar 9 18:08:24 2010 +0000

    SLING-1431 : Utility method to get the service ranking
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/osgi@921025 13f79535-47bb-0310-9956-ffa450edef68
---
 .../org/apache/sling/commons/osgi/OsgiUtil.java    | 86 ++++++++++++----------
 1 file changed, 48 insertions(+), 38 deletions(-)

diff --git a/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java b/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
index 0216457..88779f1 100644
--- a/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
+++ b/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
@@ -300,50 +300,60 @@ public class OsgiUtil {
      * @since 2.0.6
      */
     public static Comparable<Object> getComparableForServiceRanking(final Map<String, Object> props) {
+        return new ComparableImplementation(props);
+    }
 
-        return new Comparable<Object>() {
-
-            @SuppressWarnings("unchecked")
-            public int compareTo(Object reference) {
-                final Long otherId;
-                Object otherRankObj;
-                if ( reference instanceof ServiceReference ) {
-                    final ServiceReference other = (ServiceReference) reference;
-                    otherId = (Long) other.getProperty(Constants.SERVICE_ID);
-                    otherRankObj = other.getProperty(Constants.SERVICE_RANKING);
-                } else {
-                    final Map<String, Object> otherProps = (Map<String, Object>)reference;
-                    otherId = (Long) otherProps.get(Constants.SERVICE_ID);
-                    otherRankObj = otherProps.get(Constants.SERVICE_RANKING);
-                }
-                final Long id = (Long) props.get(Constants.SERVICE_ID);
-                if (id.equals(otherId)) {
-                    return 0; // same service
-                }
+    private static final class ComparableImplementation implements Comparable<Object> {
+
+        private final Map<String, Object> props;
+
+        private ComparableImplementation(Map<String, Object> props) {
+            this.props = props;
+        }
 
-                Object rankObj = props.get(Constants.SERVICE_RANKING);
+        @SuppressWarnings("unchecked")
+        public int compareTo(Object reference) {
+            final Long otherId;
+            Object otherRankObj;
+            if ( reference instanceof ServiceReference ) {
+                final ServiceReference other = (ServiceReference) reference;
+                otherId = (Long) other.getProperty(Constants.SERVICE_ID);
+                otherRankObj = other.getProperty(Constants.SERVICE_RANKING);
+            } else if (reference instanceof Map){
+                final Map<String, Object> otherProps = (Map<String, Object>)reference;
+                otherId = (Long) otherProps.get(Constants.SERVICE_ID);
+                otherRankObj = otherProps.get(Constants.SERVICE_RANKING);
+            } else {
+                final ComparableImplementation other = (ComparableImplementation)reference;
+                otherId = (Long) other.props.get(Constants.SERVICE_ID);
+                otherRankObj = other.props.get(Constants.SERVICE_RANKING);
+            }
+            final Long id = (Long) props.get(Constants.SERVICE_ID);
+            if (id.equals(otherId)) {
+                return 0; // same service
+            }
 
-                // If no rank, then spec says it defaults to zero.
-                rankObj = (rankObj == null) ? new Integer(0) : rankObj;
-                otherRankObj = (otherRankObj == null) ? new Integer(0) : otherRankObj;
+            Object rankObj = props.get(Constants.SERVICE_RANKING);
 
-                // If rank is not Integer, then spec says it defaults to zero.
-                Integer rank = (rankObj instanceof Integer)
-                    ? (Integer) rankObj : new Integer(0);
-                Integer otherRank = (otherRankObj instanceof Integer)
-                    ? (Integer) otherRankObj : new Integer(0);
+            // If no rank, then spec says it defaults to zero.
+            rankObj = (rankObj == null) ? new Integer(0) : rankObj;
+            otherRankObj = (otherRankObj == null) ? new Integer(0) : otherRankObj;
 
-                // Sort by rank in ascending order.
-                if (rank.compareTo(otherRank) < 0) {
-                    return -1; // lower rank
-                } else if (rank.compareTo(otherRank) > 0) {
-                    return 1; // higher rank
-                }
+            // If rank is not Integer, then spec says it defaults to zero.
+            Integer rank = (rankObj instanceof Integer)
+                ? (Integer) rankObj : new Integer(0);
+            Integer otherRank = (otherRankObj instanceof Integer)
+                ? (Integer) otherRankObj : new Integer(0);
 
-                // If ranks are equal, then sort by service id in descending order.
-                return (id.compareTo(otherId) < 0) ? 1 : -1;
+            // Sort by rank in ascending order.
+            if (rank.compareTo(otherRank) < 0) {
+                return -1; // lower rank
+            } else if (rank.compareTo(otherRank) > 0) {
+                return 1; // higher rank
             }
-        };
-    }
 
+            // If ranks are equal, then sort by service id in descending order.
+            return (id.compareTo(otherId) < 0) ? 1 : -1;
+        }
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.