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 93f3b1182b47c11e748d874e3e2bad56ec84d306 Author: Bertrand Delacretaz <[email protected]> AuthorDate: Thu Jan 7 15:29:28 2010 +0000 SLING-1278 - make BundleVersionInfo abstract and Comparable git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/osgi@896903 13f79535-47bb-0310-9956-ffa450edef68 --- .../bundleversion/BundleBundleVersionInfo.java | 2 +- .../bundleversion/BundleVersionComparator.java | 87 ---------------------- .../osgi/bundleversion/BundleVersionInfo.java | 82 +++++++++++++++++--- .../osgi/bundleversion/FileBundleVersionInfo.java | 2 +- ...rTest.java => BundleVersionComparisonTest.java} | 27 ++----- .../osgi/bundleversion/MockBundleVersionInfo.java | 2 +- 6 files changed, 81 insertions(+), 121 deletions(-) diff --git a/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java b/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java index 8bb868f..617ff03 100644 --- a/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java +++ b/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java @@ -23,7 +23,7 @@ import org.osgi.framework.Constants; import org.osgi.framework.Version; /** BundleVersionInfo based on a Bundle object */ -public class BundleBundleVersionInfo implements BundleVersionInfo<Bundle> { +public class BundleBundleVersionInfo extends BundleVersionInfo<Bundle> { private Bundle source; private final long lastModified; diff --git a/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java b/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java deleted file mode 100644 index f23ff71..0000000 --- a/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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.sling.commons.osgi.bundleversion; - -import java.util.Comparator; - -import org.osgi.framework.Version; - -/** Compares BundleVersionInfo */ -public class BundleVersionComparator implements Comparator<BundleVersionInfo<?>>{ - - private static final int A_GREATER = 1; - private static final int B_GREATER = -1; - private static final int EQUAL = 0; - - public int compare(BundleVersionInfo<?> a, BundleVersionInfo<?> b) { - - // Handle null values - if(a == null) { - throw new IllegalArgumentException("a is null, cannot compare"); - } - if(b == null) { - throw new IllegalArgumentException("b is null, cannot compare"); - } - - // Handle non-bundles: we don't want them! - if(!a.isBundle()) { - throw new IllegalArgumentException("Not a bundle, cannot compare:" + a); - } - if(!b.isBundle()) { - throw new IllegalArgumentException("Not a bundle, cannot compare:" + b); - } - - // First compare symbolic names - int result = a.getBundleSymbolicName().compareTo(b.getBundleSymbolicName()); - - // Then compare versions - if(result == EQUAL) { - final Version va = a.getVersion(); - final Version vb = b.getVersion(); - if(va == null && vb == null) { - // result = EQUAL - } else if(vb == null) { - result = A_GREATER; - } else if(va == null) { - result = B_GREATER; - } else { - result = va.compareTo(vb); - } - - // more recent ones must come before older ones - result = -result; - } - - // Then, if snapshots, compare modification times, more recent comes first - if(result == EQUAL && a.isSnapshot()) { - final long ma = a.getBundleLastModified(); - final long mb = b.getBundleLastModified(); - if(ma > mb) { - result = A_GREATER; - } else if(mb > ma) { - result = B_GREATER; - } - - // more recent ones must come before older ones - result = -result; - } - - return result; - } -} diff --git a/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java b/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java index c9bc28c..983b0f0 100644 --- a/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java +++ b/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java @@ -23,36 +23,98 @@ import org.osgi.framework.Version; /** Provides bundle version information, which can be * extracted from bundle files or Bundle objects. */ -public interface BundleVersionInfo<T> { +public abstract class BundleVersionInfo<T> implements Comparable<BundleVersionInfo<?>> { + + private static final int A_GREATER = 1; + private static final int B_GREATER = -1; + private static final int EQUAL = 0; + /** Marker used by Maven to identify snapshots */ - String SNAPSHOT_MARKER = "SNAPSHOT"; + public static final String SNAPSHOT_MARKER = "SNAPSHOT"; /** Name of the BND attribute that provides the bundle's last modified timestamp */ - String BND_LAST_MODIFIED = "Bnd-LastModified"; + public static final String BND_LAST_MODIFIED = "Bnd-LastModified"; /** Value for {@link #getBundleLastModified} if corresponding header * is not present */ - long BND_LAST_MODIFIED_MISSING = -1L; + public static final long BND_LAST_MODIFIED_MISSING = -1L; /** Return the source of information: underlying File or Bundle */ - T getSource(); + public abstract T getSource(); /** True if the provided data is a valid bundle */ - boolean isBundle(); + public abstract boolean isBundle(); /** Return the bundle symbolic name, null if not available */ - String getBundleSymbolicName(); + public abstract String getBundleSymbolicName(); /** Return the bundle version, null if not available */ - Version getVersion(); + public abstract Version getVersion(); /** True if the bundle version indicates a snapshot */ - boolean isSnapshot(); + public abstract boolean isSnapshot(); /** Return the bundle last modification time, based on the BND_LAST_MODIFIED * manifest header, if available. This is *not* the Bundle.getLastModified() * value, which refers to actions in the OSGi framework. * @return BND_LAST_MODIFIED_MISSING if header not supplied */ - long getBundleLastModified(); + public abstract long getBundleLastModified(); + + + /** Compare based on bundle version info, and for snapshots + * based on {@link #getBundleLastModified} + */ + public int compareTo(BundleVersionInfo<?> other) { + // Handle null values + if(other == null) { + throw new IllegalArgumentException("b is null, cannot compare"); + } + + // Handle non-bundles: we don't want them! + if(!isBundle()) { + throw new IllegalArgumentException("Not a bundle, cannot compare: " + this); + } + if(!other.isBundle()) { + throw new IllegalArgumentException("Not a bundle, cannot compare:" + other); + } + + // First compare symbolic names + int result = getBundleSymbolicName().compareTo(other.getBundleSymbolicName()); + + // Then compare versions + if(result == EQUAL) { + final Version va = getVersion(); + final Version vb = other.getVersion(); + if(va == null && vb == null) { + // result = EQUAL + } else if(vb == null) { + result = A_GREATER; + } else if(va == null) { + result = B_GREATER; + } else { + result = va.compareTo(vb); + } + + // more recent ones must come before older ones + result = -result; + } + + // Then, if snapshots, compare modification times, more recent comes first + if(result == EQUAL && isSnapshot()) { + final long ma = getBundleLastModified(); + final long mb = other.getBundleLastModified(); + if(ma > mb) { + result = A_GREATER; + } else if(mb > ma) { + result = B_GREATER; + } + + // more recent ones must come before older ones + result = -result; + } + + return result; + } + } diff --git a/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java b/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java index 4305cda..627e5b1 100644 --- a/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java +++ b/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java @@ -27,7 +27,7 @@ import org.osgi.framework.Constants; import org.osgi.framework.Version; /** BundleVersionInfo based on a bundle jar file */ -public class FileBundleVersionInfo implements BundleVersionInfo<File> { +public class FileBundleVersionInfo extends BundleVersionInfo<File> { private final String symbolicName; private final Version version; diff --git a/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java b/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparisonTest.java similarity index 81% rename from src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java rename to src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparisonTest.java index f09c292..7f57b3f 100644 --- a/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java +++ b/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparisonTest.java @@ -28,7 +28,7 @@ import java.util.List; import org.junit.Test; -public class BundleVersionComparatorTest { +public class BundleVersionComparisonTest { @Test public void testSortBundles() { @@ -52,7 +52,7 @@ public class BundleVersionComparatorTest { } final String firstBeforeSort = list.get(0).toString(); - Collections.sort(list, new BundleVersionComparator()); + Collections.sort(list); final String newFirstItem = list.get(0).toString(); assertFalse("First item (" + newFirstItem + ") must have changed during sort", firstBeforeSort.equals(newFirstItem)); @@ -67,28 +67,14 @@ public class BundleVersionComparatorTest { public void testEqual() { final MockBundleVersionInfo a = new MockBundleVersionInfo("a", "1.0", 2); final MockBundleVersionInfo b = new MockBundleVersionInfo("a", "1.0", 1); - final BundleVersionComparator c = new BundleVersionComparator(); - assertEquals("Last-modified must not be relevant for non-snapshot bundles", 0, c.compare(a, b)); + assertEquals("Last-modified must not be relevant for non-snapshot bundles", 0, a.compareTo(b)); } public void testExceptionsOnNull() { final MockBundleVersionInfo a = new MockBundleVersionInfo("a", "1.0", 2); - final BundleVersionComparator c = new BundleVersionComparator(); try { - c.compare(a, null); - fail("Expected an IllegalArgumentException"); - } catch(IllegalArgumentException asExpected) { - } - - try { - c.compare(null, a); - fail("Expected an IllegalArgumentException"); - } catch(IllegalArgumentException asExpected) { - } - - try { - c.compare(null, null); + a.compareTo(null); fail("Expected an IllegalArgumentException"); } catch(IllegalArgumentException asExpected) { } @@ -97,16 +83,15 @@ public class BundleVersionComparatorTest { public void testExceptionOnNonBundle() { final MockBundleVersionInfo a = new MockBundleVersionInfo("a", "1.0", 2); final MockBundleVersionInfo nonBundle = new MockBundleVersionInfo(); - final BundleVersionComparator c = new BundleVersionComparator(); try { - c.compare(a, nonBundle); + a.compareTo(nonBundle); fail("Expected an IllegalArgumentException"); } catch(IllegalArgumentException asExpected) { } try { - c.compare(nonBundle, a); + nonBundle.compareTo(a); fail("Expected an IllegalArgumentException"); } catch(IllegalArgumentException asExpected) { } diff --git a/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java b/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java index ee5f6f6..a31a3b3 100644 --- a/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java +++ b/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java @@ -20,7 +20,7 @@ package org.apache.sling.commons.osgi.bundleversion; import org.osgi.framework.Version; -class MockBundleVersionInfo implements BundleVersionInfo<String> { +class MockBundleVersionInfo extends BundleVersionInfo<String> { private final String source; private final String symbolicName; -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
