blob: 807116628fdfc52938d9809975078821ca459c70 [file] [log] [blame]
Stefano Lenzi476013d2007-09-21 23:59:54 +00001/*
2 * $Header: /cvshome/bundles/bundles.obr/src/bundles/obr/resource/VersionImpl.java,v 1.3 2006/02/15 16:36:57 pkriens Exp $
3 *
4 * Copyright (c) OSGi Alliance (2004, 2005). All Rights Reserved.
5 *
6 * This program and the accompanying materials are made available under the
7 * terms of the Eclipse Public License v1.0 which accompanies this
8 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html.
9 */
10
11package org.osgi.impl.bundle.obr.resource;
12
13import org.osgi.framework.Version;
14
15/**
16 * Version identifier for bundles and packages.
17 *
18 * <p>
19 * Version identifiers have four components.
20 * <ol>
21 * <li>Major version. A non-negative integer.</li>
22 * <li>Minor version. A non-negative integer.</li>
23 * <li>Micro version. A non-negative integer.</li>
24 * <li>Qualifier. A text string. See <code>Version(String)</code> for the
25 * format of the qualifier string.</li>
26 * </ol>
27 *
28 * <p>
29 * <code>Version</code> objects are immutable.
30 *
31 * @version $Revision: 1.3 $
32 * @since 1.3
33 */
34
35public class VersionImpl extends Version {
36 VersionRange range;
37
38 /**
39 * Creates a version identifier from the specified numerical components.
40 *
41 * <p>
42 * The qualifier is set to the empty string.
43 *
44 * @param major Major component of the version identifier.
45 * @param minor Minor component of the version identifier.
46 * @param micro Micro component of the version identifier.
47 * @throws IllegalArgumentException If the numerical components are
48 * negative.
49 */
50 public VersionImpl(int major, int minor, int micro) {
51 this(major, minor, micro, null);
52 }
53
54 /**
55 * Creates a version identifier from the specifed components.
56 *
57 * @param major Major component of the version identifier.
58 * @param minor Minor component of the version identifier.
59 * @param micro Micro component of the version identifier.
60 * @param qualifier Qualifier component of the version identifier. If
61 * <code>null</code> is specified, then the qualifier will be set
62 * to the empty string.
63 * @throws IllegalArgumentException If the numerical components are negative
64 * or the qualifier string is invalid.
65 */
66 public VersionImpl(int major, int minor, int micro, String qualifier) {
67 super(major, minor, micro, qualifier);
68 }
69
70 // TODO Ugly!
71 public VersionImpl(String string) {
72 super(
73 string.indexOf("[") >= 0 || string.indexOf("(") >= 0 ? new VersionRange(
74 string).getMinimum().toString()
75 : string);
76 if ( string.indexOf("[") >= 0 || string.indexOf("(") >= 0 )
77 range = new VersionRange(string);
78 }
79
80 VersionRange getRange() {
81 return range;
82 }
83 /**
84 * this other 0 1 -1
85 *
86 * @param o
87 * @return
88 * @see org.osgi.framework.Version#compareTo(java.lang.Object)
89 */
90 public int compareTo(Object o) {
91 if ( o instanceof VersionImpl ) {
92 VersionImpl other = (VersionImpl) o;
93 int cs = 0;
94 if ( range != null )
95 cs++;
96 if ( other.range!=null)
97 cs+=2;
98 switch (cs ) {
99 case 0: // V1 V2
100 return super.compareTo(other);
101
102 case 1: // R1 V2
103 return range.isIncluded(other) ? 0 : 1;
104
105 case 2: // V1 R2
106 return other.range.isIncluded(this) ? 0 : 1;
107
108 // TODO experimental
109 case 3: // R1 R2
110 return range.isIncluded(other.range.getMinimum()) && range.isIncluded(other.range.getMaximum()) ? 0 : 1;
111 }
112 return -1;
113 } else {
114 return super.compareTo(o);
115 }
116 }
117
118 public boolean equals(Object other) {
119 return compareTo(other) == 0;
120 }
121}