FELIX-2169: Improve dev:show-tree performance and avoid NPE when no matching export is found

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@918963 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/commons/pom.xml b/karaf/commons/pom.xml
new file mode 100644
index 0000000..f491e69
--- /dev/null
+++ b/karaf/commons/pom.xml
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+  <!--
+
+      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.
+  -->
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <artifactId>karaf</artifactId>
+    <groupId>org.apache.felix.karaf</groupId>
+    <version>1.5.0-SNAPSHOT</version>
+  </parent>
+
+  <groupId>org.apache.felix.karaf</groupId>
+  <artifactId>org.apache.felix.karaf.commons</artifactId>
+  <version>1.5.0-SNAPSHOT</version>
+  <packaging>bundle</packaging>
+  <name>Apache Felix Karaf :: Commons</name>
+
+  <properties>
+    <appendedResourcesDirectory>${basedir}/../etc/appended-resources</appendedResourcesDirectory>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <scope>provided</scope>
+    </dependency>    
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <configuration>
+          <instructions>
+            <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+            <Export-Package>${pom.artifactId}*;version=${project.version}</Export-Package>
+            <Import-Package>
+                !${pom.artifactId}*,
+                *
+            </Import-Package>
+            <_versionpolicy>${bnd.version.policy}</_versionpolicy>
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>
\ No newline at end of file
diff --git a/karaf/commons/src/main/java/org/apache/felix/karaf/commons/osgi/VersionRange.java b/karaf/commons/src/main/java/org/apache/felix/karaf/commons/osgi/VersionRange.java
new file mode 100644
index 0000000..47045de
--- /dev/null
+++ b/karaf/commons/src/main/java/org/apache/felix/karaf/commons/osgi/VersionRange.java
@@ -0,0 +1,158 @@
+/*
+ * 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.karaf.commons.osgi;
+
+import org.osgi.framework.Version;
+
+public class VersionRange
+{
+    private final Version m_low;
+    private final boolean m_isLowInclusive;
+    private final Version m_high;
+    private final boolean m_isHighInclusive;
+    public static final VersionRange infiniteRange = new VersionRange(Version.emptyVersion, true, null, true);
+
+    public VersionRange(
+        Version low, boolean isLowInclusive,
+        Version high, boolean isHighInclusive)
+    {
+        m_low = low;
+        m_isLowInclusive = isLowInclusive;
+        m_high = high;
+        m_isHighInclusive = isHighInclusive;
+    }
+
+    public Version getLow()
+    {
+        return m_low;
+    }
+
+    public boolean isLowInclusive()
+    {
+        return m_isLowInclusive;
+    }
+
+    public Version getHigh()
+    {
+        return m_high;
+    }
+
+    public boolean isHighInclusive()
+    {
+        return m_isHighInclusive;
+    }
+
+    public boolean isInRange(Version version)
+    {
+        // We might not have an upper end to the range.
+        if (m_high == null)
+        {
+            return (version.compareTo(m_low) >= 0);
+        }
+        else if (isLowInclusive() && isHighInclusive())
+        {
+            return (version.compareTo(m_low) >= 0) && (version.compareTo(m_high) <= 0);
+        }
+        else if (isHighInclusive())
+        {
+            return (version.compareTo(m_low) > 0) && (version.compareTo(m_high) <= 0);
+        }
+        else if (isLowInclusive())
+        {
+            return (version.compareTo(m_low) >= 0) && (version.compareTo(m_high) < 0);
+        }
+        return (version.compareTo(m_low) > 0) && (version.compareTo(m_high) < 0);
+    }
+
+    public static VersionRange parse(String range)
+    {
+        // Check if the version is an interval.
+        if (range.indexOf(',') >= 0)
+        {
+            String s = range.substring(1, range.length() - 1);
+            String vlo = s.substring(0, s.indexOf(',')).trim();
+            String vhi = s.substring(s.indexOf(',') + 1, s.length()).trim();
+            return new VersionRange (
+                new Version(vlo), (range.charAt(0) == '['),
+                new Version(vhi), (range.charAt(range.length() - 1) == ']'));
+        }
+        else
+        {
+            return new VersionRange(new Version(range), true, null, false);
+        }
+    }
+
+    public boolean equals(Object obj)
+    {
+        if (obj == null)
+        {
+            return false;
+        }
+        if (getClass() != obj.getClass())
+        {
+            return false;
+        }
+        final VersionRange other = (VersionRange) obj;
+        if (m_low != other.m_low && (m_low == null || !m_low.equals(other.m_low)))
+        {
+            return false;
+        }
+        if (m_isLowInclusive != other.m_isLowInclusive)
+        {
+            return false;
+        }
+        if (m_high != other.m_high && (m_high == null || !m_high.equals(other.m_high)))
+        {
+            return false;
+        }
+        if (m_isHighInclusive != other.m_isHighInclusive)
+        {
+            return false;
+        }
+        return true;
+    }
+
+    public int hashCode()
+    {
+        int hash = 5;
+        hash = 97 * hash + (m_low != null ? m_low.hashCode() : 0);
+        hash = 97 * hash + (m_isLowInclusive ? 1 : 0);
+        hash = 97 * hash + (m_high != null ? m_high.hashCode() : 0);
+        hash = 97 * hash + (m_isHighInclusive ? 1 : 0);
+        return hash;
+    }
+
+    public String toString()
+    {
+        if (m_high != null)
+        {
+            StringBuffer sb = new StringBuffer();
+            sb.append(m_isLowInclusive ? '[' : '(');
+            sb.append(m_low.toString());
+            sb.append(',');
+            sb.append(m_high.toString());
+            sb.append(m_isHighInclusive ? ']' : ')');
+            return sb.toString();
+        }
+        else
+        {
+            return m_low.toString();
+        }
+    }
+}
\ No newline at end of file