001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.kernel.repository;
017:
018: import java.util.StringTokenizer;
019: import java.io.Serializable;
020:
021: /**
022: * Default implementation of artifact versioning.
023: *
024: * @author <a href="mailto:brett@apache.org">Brett Porter</a>
025: * @version $Id: Version.java 476049 2006-11-17 04:35:17Z kevan $
026: */
027: // This code was lifted from Apache Maven
028: public class Version implements Comparable, Serializable {
029: // Geronimo adds Serial UID because we serialize versions into config.ser
030: private static final long serialVersionUID = 7941704456058067109L;
031:
032: private Integer majorVersion;
033:
034: private Integer minorVersion;
035:
036: private Integer incrementalVersion;
037:
038: private Integer buildNumber;
039:
040: private String qualifier;
041:
042: public Version(String version) {
043: parseVersion(version);
044: }
045:
046: public int compareTo(Object o) {
047: Version otherVersion = (Version) o;
048:
049: int result = compareIntegers(majorVersion,
050: otherVersion.majorVersion);
051: if (result == 0) {
052: result = compareIntegers(minorVersion,
053: otherVersion.minorVersion);
054: }
055: if (result == 0) {
056: result = compareIntegers(incrementalVersion,
057: otherVersion.incrementalVersion);
058: }
059: if (result == 0) {
060: if (buildNumber != null || otherVersion.buildNumber != null) {
061: result = compareIntegers(buildNumber,
062: otherVersion.buildNumber);
063: } else if (qualifier != null) {
064: if (otherVersion.qualifier != null) {
065: if (qualifier.length() > otherVersion.qualifier
066: .length()
067: && qualifier
068: .startsWith(otherVersion.qualifier)) {
069: // here, the longer one that otherwise match is considered older
070: result = -1;
071: } else if (qualifier.length() < otherVersion.qualifier
072: .length()
073: && otherVersion.qualifier
074: .startsWith(qualifier)) {
075: // here, the longer one that otherwise match is considered older
076: result = 1;
077: } else {
078: result = qualifier
079: .compareTo(otherVersion.qualifier);
080: }
081: } else {
082: // otherVersion has no qualifier but we do - that's newer
083: result = -1;
084: }
085: } else if (otherVersion.qualifier != null) {
086: // otherVersion has a qualifier but we don't, we're newer
087: result = 1;
088: }
089: }
090: return result;
091: }
092:
093: private int compareIntegers(Integer i1, Integer i2) {
094: // treat null as 0 in comparison
095: if (i1 == null ? i2 == null : i1.equals(i2)) {
096: return 0;
097: } else if (i1 == null) {
098: return -i2.intValue();
099: } else if (i2 == null) {
100: return i1.intValue();
101: } else {
102: return i1.intValue() - i2.intValue();
103: }
104: }
105:
106: public int getMajorVersion() {
107: return majorVersion != null ? majorVersion.intValue() : 0;
108: }
109:
110: public int getMinorVersion() {
111: return minorVersion != null ? minorVersion.intValue() : 0;
112: }
113:
114: public int getIncrementalVersion() {
115: return incrementalVersion != null ? incrementalVersion
116: .intValue() : 0;
117: }
118:
119: public int getBuildNumber() {
120: return buildNumber != null ? buildNumber.intValue() : 0;
121: }
122:
123: public String getQualifier() {
124: return qualifier;
125: }
126:
127: public final void parseVersion(String version) {
128: int index = version.indexOf("-");
129:
130: String part1;
131: String part2 = null;
132:
133: if (index < 0) {
134: part1 = version;
135: } else {
136: part1 = version.substring(0, index);
137: part2 = version.substring(index + 1);
138: }
139:
140: if (part2 != null) {
141: try {
142: if (part2.length() == 1 || !part2.startsWith("0")) {
143: buildNumber = Integer.valueOf(part2);
144: } else {
145: qualifier = part2;
146: }
147: } catch (NumberFormatException e) {
148: qualifier = part2;
149: }
150: }
151:
152: if (part1.indexOf(".") < 0 && !part1.startsWith("0")) {
153: try {
154: majorVersion = Integer.valueOf(part1);
155: } catch (NumberFormatException e) {
156: // qualifier is the whole version, including "-"
157: qualifier = version;
158: buildNumber = null;
159: }
160: } else {
161: boolean fallback = false;
162: StringTokenizer tok = new StringTokenizer(part1, ".");
163: try {
164: majorVersion = getNextIntegerToken(tok);
165: if (tok.hasMoreTokens()) {
166: minorVersion = getNextIntegerToken(tok);
167: }
168: if (tok.hasMoreTokens()) {
169: incrementalVersion = getNextIntegerToken(tok);
170: }
171: if (tok.hasMoreTokens()) {
172: fallback = true;
173: }
174: } catch (NumberFormatException e) {
175: fallback = true;
176: }
177:
178: if (fallback) {
179: // qualifier is the whole version, including "-"
180: qualifier = version;
181: majorVersion = null;
182: minorVersion = null;
183: incrementalVersion = null;
184: }
185: }
186: }
187:
188: private static Integer getNextIntegerToken(StringTokenizer tok) {
189: String s = tok.nextToken();
190: if (s.length() > 1 && s.startsWith("0")) {
191: throw new NumberFormatException(
192: "Number part has a leading 0: '" + s + "'");
193: }
194: return Integer.valueOf(s);
195: }
196:
197: public String toString() {
198: StringBuffer buf = new StringBuffer();
199: if (majorVersion != null) {
200: buf.append(majorVersion);
201: }
202: if (minorVersion != null) {
203: buf.append(".");
204: buf.append(minorVersion);
205: }
206: if (incrementalVersion != null) {
207: buf.append(".");
208: buf.append(incrementalVersion);
209: }
210: if (buildNumber != null) {
211: buf.append("-");
212: buf.append(buildNumber);
213: } else if (qualifier != null) {
214: if (buf.length() > 0) {
215: buf.append("-");
216: }
217: buf.append(qualifier);
218: }
219: return buf.toString();
220: }
221:
222: public boolean equals(Object other) {
223: if (this == other) {
224: return true;
225: }
226: if (other == null || this .getClass() != other.getClass()) {
227: return false;
228: }
229: Version v = (Version) other;
230: if (majorVersion == null ? v.majorVersion != null
231: : !majorVersion.equals(v.majorVersion)) {
232: return false;
233: }
234: if (minorVersion == null ? v.minorVersion != null
235: : !minorVersion.equals(v.minorVersion)) {
236: return false;
237: }
238: if (incrementalVersion == null ? v.incrementalVersion != null
239: : !incrementalVersion.equals(v.incrementalVersion)) {
240: return false;
241: }
242: if (buildNumber == null ? v.buildNumber != null : !buildNumber
243: .equals(v.buildNumber)) {
244: return false;
245: }
246: return qualifier == null ? v.qualifier == null : qualifier
247: .equals(v.qualifier);
248: }
249:
250: public int hashCode() {
251: int hashCode = 0;
252: if (majorVersion != null) {
253: hashCode = majorVersion.intValue();
254: }
255: if (minorVersion != null) {
256: hashCode = 37 * hashCode + minorVersion.intValue();
257: }
258: if (incrementalVersion != null) {
259: hashCode = 37 * hashCode + incrementalVersion.intValue();
260: }
261: if (buildNumber != null) {
262: hashCode = 37 * hashCode + buildNumber.intValue();
263: }
264: if (qualifier != null) {
265: hashCode = 37 * hashCode + qualifier.hashCode();
266: }
267: return hashCode;
268: }
269: }
|