You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2007/09/04 20:20:30 UTC
svn commit: r572768 [2/2] -
/felix/sandbox/clement/maven-obr-plugin/src/main/java/org/osgi/impl/bundle/obr/resource/
Added: felix/sandbox/clement/maven-obr-plugin/src/main/java/org/osgi/impl/bundle/obr/resource/VersionImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/maven-obr-plugin/src/main/java/org/osgi/impl/bundle/obr/resource/VersionImpl.java?rev=572768&view=auto
==============================================================================
--- felix/sandbox/clement/maven-obr-plugin/src/main/java/org/osgi/impl/bundle/obr/resource/VersionImpl.java (added)
+++ felix/sandbox/clement/maven-obr-plugin/src/main/java/org/osgi/impl/bundle/obr/resource/VersionImpl.java Tue Sep 4 11:20:28 2007
@@ -0,0 +1,121 @@
+/*
+ * $Header: /cvshome/bundles/bundles.obr/src/bundles/obr/resource/VersionImpl.java,v 1.3 2006/02/15 16:36:57 pkriens Exp $
+ *
+ * Copyright (c) OSGi Alliance (2004, 2005). All Rights Reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this
+ * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html.
+ */
+
+package org.osgi.impl.bundle.obr.resource;
+
+import org.osgi.framework.Version;
+
+/**
+ * Version identifier for bundles and packages.
+ *
+ * <p>
+ * Version identifiers have four components.
+ * <ol>
+ * <li>Major version. A non-negative integer.</li>
+ * <li>Minor version. A non-negative integer.</li>
+ * <li>Micro version. A non-negative integer.</li>
+ * <li>Qualifier. A text string. See <code>Version(String)</code> for the
+ * format of the qualifier string.</li>
+ * </ol>
+ *
+ * <p>
+ * <code>Version</code> objects are immutable.
+ *
+ * @version $Revision: 1.3 $
+ * @since 1.3
+ */
+
+public class VersionImpl extends Version {
+ VersionRange range;
+
+ /**
+ * Creates a version identifier from the specified numerical components.
+ *
+ * <p>
+ * The qualifier is set to the empty string.
+ *
+ * @param major Major component of the version identifier.
+ * @param minor Minor component of the version identifier.
+ * @param micro Micro component of the version identifier.
+ * @throws IllegalArgumentException If the numerical components are
+ * negative.
+ */
+ public VersionImpl(int major, int minor, int micro) {
+ this(major, minor, micro, null);
+ }
+
+ /**
+ * Creates a version identifier from the specifed components.
+ *
+ * @param major Major component of the version identifier.
+ * @param minor Minor component of the version identifier.
+ * @param micro Micro component of the version identifier.
+ * @param qualifier Qualifier component of the version identifier. If
+ * <code>null</code> is specified, then the qualifier will be set
+ * to the empty string.
+ * @throws IllegalArgumentException If the numerical components are negative
+ * or the qualifier string is invalid.
+ */
+ public VersionImpl(int major, int minor, int micro, String qualifier) {
+ super(major, minor, micro, qualifier);
+ }
+
+ // TODO Ugly!
+ public VersionImpl(String string) {
+ super(
+ string.indexOf("[") >= 0 || string.indexOf("(") >= 0 ? new VersionRange(
+ string).getMinimum().toString()
+ : string);
+ if ( string.indexOf("[") >= 0 || string.indexOf("(") >= 0 )
+ range = new VersionRange(string);
+ }
+
+ VersionRange getRange() {
+ return range;
+ }
+ /**
+ * this other 0 1 -1
+ *
+ * @param o
+ * @return
+ * @see org.osgi.framework.Version#compareTo(java.lang.Object)
+ */
+ public int compareTo(Object o) {
+ if ( o instanceof VersionImpl ) {
+ VersionImpl other = (VersionImpl) o;
+ int cs = 0;
+ if ( range != null )
+ cs++;
+ if ( other.range!=null)
+ cs+=2;
+ switch (cs ) {
+ case 0: // V1 V2
+ return super.compareTo(other);
+
+ case 1: // R1 V2
+ return range.isIncluded(other) ? 0 : 1;
+
+ case 2: // V1 R2
+ return other.range.isIncluded(this) ? 0 : 1;
+
+ // TODO experimental
+ case 3: // R1 R2
+ return range.isIncluded(other.range.getMinimum()) && range.isIncluded(other.range.getMaximum()) ? 0 : 1;
+ }
+ return -1;
+ } else {
+ return super.compareTo(o);
+ }
+ }
+
+ public boolean equals(Object other) {
+ return compareTo(other) == 0;
+ }
+}
\ No newline at end of file
Added: felix/sandbox/clement/maven-obr-plugin/src/main/java/org/osgi/impl/bundle/obr/resource/VersionRange.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/maven-obr-plugin/src/main/java/org/osgi/impl/bundle/obr/resource/VersionRange.java?rev=572768&view=auto
==============================================================================
--- felix/sandbox/clement/maven-obr-plugin/src/main/java/org/osgi/impl/bundle/obr/resource/VersionRange.java (added)
+++ felix/sandbox/clement/maven-obr-plugin/src/main/java/org/osgi/impl/bundle/obr/resource/VersionRange.java Tue Sep 4 11:20:28 2007
@@ -0,0 +1,166 @@
+/*******************************************************************************
+ * Copyright (c) 2003, 2005 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.osgi.impl.bundle.obr.resource;
+
+import org.osgi.framework.Version;
+
+/**
+ * This class represents a version range.
+ * @since 3.1
+ */
+public class VersionRange {
+ private static final Version versionMax = new Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
+ /**
+ * An empty version
+ */
+ public static final VersionRange emptyRange = new VersionRange(null);
+
+ private Version minVersion;
+ private boolean includeMin;
+ private Version maxVersion;
+ private boolean includeMax;
+
+ /**
+ * Constructs a VersionRange with the specified minVersion and maxVersion.
+ * @param minVersion the minimum version of the range
+ * @param maxVersion the maximum version of the range
+ */
+ public VersionRange(Version minVersion, boolean includeMin, Version maxVersion, boolean includeMax) {
+ this.minVersion = minVersion;
+ this.includeMin = includeMin;
+ this.maxVersion = maxVersion;
+ this.includeMax = includeMax;
+ }
+
+ /**
+ * Constructs a VersionRange from the given versionRange String.
+ * @param versionRange a version range String that specifies a range of
+ * versions.
+ */
+ public VersionRange(String versionRange) {
+ if (versionRange == null || versionRange.length() == 0) {
+ minVersion = Version.emptyVersion;
+ includeMin = true;
+ maxVersion = VersionRange.versionMax;
+ includeMax = true;
+ return;
+ }
+ versionRange = versionRange.trim();
+ if (versionRange.charAt(0) == '[' || versionRange.charAt(0) == '(') {
+ int comma = versionRange.indexOf(',');
+ if (comma < 0)
+ throw new IllegalArgumentException();
+ char last = versionRange.charAt(versionRange.length() - 1);
+ if (last != ']' && last != ')')
+ throw new IllegalArgumentException();
+
+ minVersion = Version.parseVersion(versionRange.substring(1, comma).trim());
+ includeMin = versionRange.charAt(0) == '[';
+ maxVersion = Version.parseVersion(versionRange.substring(comma + 1, versionRange.length() - 1).trim());
+ includeMax = last == ']';
+ } else {
+ minVersion = Version.parseVersion(versionRange.trim());
+ includeMin = true;
+ maxVersion = VersionRange.versionMax;
+ includeMax = true;
+ }
+ }
+
+ /**
+ * Returns the minimum Version of this VersionRange
+ * @return the minimum Version of this VersionRange
+ */
+ public Version getMinimum() {
+ return minVersion;
+ }
+
+ /**
+ * Indicates if the minimum version is included in the version range.
+ * @return true if the minimum version is included in the version range;
+ * otherwise false is returned
+ */
+ public boolean getIncludeMinimum() {
+ return includeMin;
+ }
+
+ /**
+ * Returns the maximum Version of this VersionRange
+ * @return the maximum Version of this VersionRange
+ */
+ public Version getMaximum() {
+ return maxVersion;
+ }
+
+ /**
+ * Indicates if the maximum version is included in the version range.
+ * @return true if the maximum version is included in the version range;
+ * otherwise false is returned
+ */
+ public boolean getIncludeMaximum() {
+ return includeMax;
+ }
+
+ /**
+ * Returns whether the given version is included in this VersionRange.
+ * This will depend on the minimum and maximum versions of this VersionRange
+ * and the given version.
+ *
+ * @param version a version to be tested for inclusion in this VersionRange.
+ * (may be <code>null</code>)
+ * @return <code>true</code> if the version is include,
+ * <code>false</code> otherwise
+ */
+ public boolean isIncluded(Version version) {
+ Version minRequired = getMinimum();
+ if (minRequired == null)
+ return true;
+ if (version == null)
+ return false;
+ Version maxRequired = getMaximum() == null ? VersionRange.versionMax : getMaximum();
+ int minCheck = includeMin ? 0 : 1;
+ int maxCheck = includeMax ? 0 : -1;
+ return version.compareTo(minRequired) >= minCheck && version.compareTo(maxRequired) <= maxCheck;
+
+ }
+
+ public boolean equals(Object object) {
+ if (!(object instanceof VersionRange))
+ return false;
+ VersionRange vr = (VersionRange) object;
+ if (minVersion != null && vr.getMinimum() != null) {
+ if (minVersion.equals(vr.getMinimum()) && includeMin == vr.includeMin)
+ if (maxVersion != null && vr.getMaximum() != null) {
+ if (maxVersion.equals(vr.getMaximum()) && includeMax == vr.includeMax)
+ return true;
+ }
+ else
+ return maxVersion == vr.getMaximum();
+ }
+ else {
+ return minVersion == vr.getMinimum();
+ }
+ return false;
+ }
+
+ public String toString() {
+ if (minVersion == null)
+ return Version.emptyVersion.toString();
+ if (VersionRange.versionMax.equals(maxVersion))
+ return minVersion.toString();
+ StringBuffer result = new StringBuffer();
+ result.append(includeMin ? '[' : '(');
+ result.append(minVersion);
+ result.append(',');
+ result.append(maxVersion);
+ result.append(includeMax ? ']' : ')');
+ return result.toString();
+ }
+}