001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.utils.helper;
038:
039: /**
040: *
041: * @author Kirill Sorokin
042: */
043: public class Version {
044: /////////////////////////////////////////////////////////////////////////////////
045: // Static
046: public static Version getVersion(final String string) {
047: if (string.matches("([0-9]+[\\._\\-]+)*[0-9]+")) {
048: return new Version(string);
049: } else {
050: return null;
051: }
052: }
053:
054: /////////////////////////////////////////////////////////////////////////////////
055: // Instance
056: private long major;
057: private long minor;
058: private long micro;
059: private long update;
060: private long build;
061:
062: private Version(final String string) {
063: String[] split = string.split("[\\._\\-]+"); //NOI18N
064:
065: if (split.length > 0) {
066: major = new Long(split[0]);
067: }
068: if (split.length > 1) {
069: minor = new Long(split[1]);
070: }
071: if (split.length > 2) {
072: micro = new Long(split[2]);
073: }
074: if (split.length > 3) {
075: update = new Long(split[3]);
076: }
077: if (split.length > 4) {
078: build = new Long(split[4]);
079: }
080: }
081:
082: public boolean equals(final Version version) {
083: return ((major == version.major) && (minor == version.minor)
084: && (micro == version.micro)
085: && (update == version.update) && (build == version.build)) ? true
086: : false;
087: }
088:
089: public boolean newerThan(final Version version) {
090: if (major > version.major) {
091: return true;
092: } else if (major == version.major) {
093: if (minor > version.minor) {
094: return true;
095: } else if (minor == version.minor) {
096: if (micro > version.micro) {
097: return true;
098: } else if (micro == version.micro) {
099: if (update > version.update) {
100: return true;
101: } else if (update == version.update) {
102: if (build > version.build) {
103: return true;
104: }
105: }
106: }
107: }
108: }
109:
110: return false;
111: }
112:
113: public boolean newerOrEquals(final Version version) {
114: if (newerThan(version) || equals(version)) {
115: return true;
116: }
117:
118: return false;
119: }
120:
121: public boolean olderThan(final Version version) {
122: if (!newerOrEquals(version)) {
123: return true;
124: }
125:
126: return false;
127: }
128:
129: public boolean olderOrEquals(final Version version) {
130: if (!newerThan(version)) {
131: return true;
132: }
133:
134: return false;
135: }
136:
137: public VersionDistance getDistance(final Version version) {
138: return new VersionDistance(this , version);
139: }
140:
141: public long getMajor() {
142: return major;
143: }
144:
145: public long getMinor() {
146: return minor;
147: }
148:
149: public long getMicro() {
150: return micro;
151: }
152:
153: public long getUpdate() {
154: return update;
155: }
156:
157: public long getBuild() {
158: return build;
159: }
160:
161: @Override
162: public String toString() {
163: return "" + major + "." + minor + "." + micro + "." + update
164: + "." + build;
165: }
166:
167: public String toMajor() {
168: return "" + major;
169: }
170:
171: public String toMinor() {
172: return "" + major + "." + minor;
173: }
174:
175: public String toMicro() {
176: return "" + major + "." + minor + "." + micro;
177: }
178:
179: public String toJdkStyle() {
180: return ""
181: + major
182: + "."
183: + minor
184: + "."
185: + micro
186: + (update != 0 ? "_"
187: + (update < 10 ? "0" + update : update) : "");
188: }
189:
190: /////////////////////////////////////////////////////////////////////////////////
191: // Inner Classes
192: public static class VersionDistance {
193: private long majorDistance;
194: private long minorDistance;
195: private long microDistance;
196: private long updateDistance;
197: private long buildDistance;
198:
199: private VersionDistance(final Version version1,
200: final Version version2) {
201: majorDistance = Math.abs(version1.getMajor()
202: - version2.getMajor());
203: minorDistance = Math.abs(version1.getMinor()
204: - version2.getMinor());
205: microDistance = Math.abs(version1.getMicro()
206: - version2.getMicro());
207: updateDistance = Math.abs(version1.getUpdate()
208: - version2.getUpdate());
209: buildDistance = Math.abs(version1.getBuild()
210: - version2.getBuild());
211: }
212:
213: public boolean equals(final VersionDistance distance) {
214: return ((majorDistance == distance.majorDistance)
215: && (minorDistance == distance.minorDistance)
216: && (microDistance == distance.microDistance)
217: && (updateDistance == distance.updateDistance) && (buildDistance == distance.buildDistance)) ? true
218: : false;
219: }
220:
221: public boolean greaterThan(final VersionDistance distance) {
222: if (majorDistance > distance.majorDistance) {
223: return true;
224: } else if (majorDistance == distance.majorDistance) {
225: if (minorDistance > distance.minorDistance) {
226: return true;
227: } else if (minorDistance == distance.minorDistance) {
228: if (microDistance > distance.microDistance) {
229: return true;
230: } else if (microDistance == distance.microDistance) {
231: if (updateDistance > distance.updateDistance) {
232: return true;
233: } else if (updateDistance == distance.updateDistance) {
234: if (buildDistance > distance.buildDistance) {
235: return true;
236: }
237: }
238: }
239: }
240: }
241:
242: return false;
243: }
244:
245: public boolean greaterOrEquals(final VersionDistance version) {
246: if (greaterThan(version) || equals(version)) {
247: return true;
248: }
249:
250: return false;
251: }
252:
253: public boolean lessThan(final VersionDistance version) {
254: if (!greaterOrEquals(version)) {
255: return true;
256: }
257:
258: return false;
259: }
260:
261: public boolean lessOrEquals(final VersionDistance distance) {
262: if (!greaterThan(distance)) {
263: return true;
264: }
265:
266: return false;
267: }
268: }
269: }
|