You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2008/05/27 16:38:33 UTC

svn commit: r660533 - /incubator/sling/trunk/commons/osgi/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java

Author: cziegeler
Date: Tue May 27 07:38:31 2008
New Revision: 660533

URL: http://svn.apache.org/viewvc?rev=660533&view=rev
Log:
Add method for handling integer properties.

Modified:
    incubator/sling/trunk/commons/osgi/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java

Modified: incubator/sling/trunk/commons/osgi/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/commons/osgi/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java?rev=660533&r1=660532&r2=660533&view=diff
==============================================================================
--- incubator/sling/trunk/commons/osgi/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java (original)
+++ incubator/sling/trunk/commons/osgi/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java Tue May 27 07:38:31 2008
@@ -64,10 +64,10 @@
     }
 
     /**
-     * Returns the named service reference property as an integer or the
+     * Returns the named service reference property as a long or the
      * <code>defaultValue</code> if no such reference property exists or if
      * the property is not an <code>Integer</code> and cannot be converted to
-     * an <code>Integer</code> from the property's string value.
+     * a <code>Long</code> from the property's string value.
      */
     public static long toLong(Object propValue, long defaultValue) {
         propValue = toObject(propValue);
@@ -85,6 +85,27 @@
     }
 
     /**
+     * Returns the named service reference property as an integer or the
+     * <code>defaultValue</code> if no such reference property exists or if
+     * the property is not an <code>Integer</code> and cannot be converted to
+     * an <code>Integer</code> from the property's string value.
+     */
+    public static long toInteger(Object propValue, int defaultValue) {
+        propValue = toObject(propValue);
+        if (propValue instanceof Integer) {
+            return (Integer) propValue;
+        } else if (propValue != null) {
+            try {
+                return Integer.valueOf(String.valueOf(propValue));
+            } catch (NumberFormatException nfe) {
+                // don't care, fall through to default value
+            }
+        }
+
+        return defaultValue;
+    }
+
+    /**
      * Returns the named service reference property as a double or the
      * <code>defaultValue</code> if no such reference property exists or if
      * the property is not an <code>Double</code> and cannot be converted to
@@ -140,7 +161,7 @@
         if (propValue == null) {
             // no value at all
             return null;
-            
+
         } else if (propValue instanceof String) {
             // single string
             return new String[] { (String) propValue };