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
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.complib;
043:
044: /**
045: * Represents a generic Version object
046: *
047: * @author Edwin Goei
048: */
049: public class Version implements Comparable<Version> {
050:
051: private int major;
052:
053: private int minor;
054:
055: private int micro;
056:
057: /**
058: * @param major
059: * @param minor
060: * @param micro
061: */
062: public Version(int major, int minor, int micro) {
063: if (major < 0 || minor < 0 || micro < 0) {
064: throw new IllegalArgumentException(
065: "No arguments must be less than 0");
066: }
067: this .major = major;
068: this .minor = minor;
069: this .micro = micro;
070: }
071:
072: /**
073: * Format: "major.minor[.micro]", for example "2.1.3". Major and minor parts
074: * are required but micro is optional.
075: *
076: * @param versionString
077: */
078: public Version(String versionString) {
079: String[] parts = versionString.split("\\.");
080: try {
081: major = Integer.parseInt(parts[0]);
082: if (major < 0) {
083: throw new IllegalArgumentException(
084: "Bad version format, required major version < 0: "
085: + versionString);
086: }
087: minor = Integer.parseInt(parts[1]);
088: if (minor < 0) {
089: throw new IllegalArgumentException(
090: "Bad version format, required minor version < 0: "
091: + versionString);
092: }
093:
094: if (parts.length > 2) {
095: micro = Integer.parseInt(parts[2]);
096: if (micro < 0) {
097: throw new IllegalArgumentException(
098: "Bad version format, optional micro version < 0: "
099: + versionString);
100: }
101: } else {
102: // Make version "X.Y" equal "X.Y.0" for any valid X or Y
103: micro = 0;
104: }
105: } catch (NumberFormatException e) {
106: throw new IllegalArgumentException("Bad version format "
107: + versionString);
108: }
109: }
110:
111: public boolean equals(Object anObject) {
112: if (this == anObject) {
113: return true;
114: }
115: if (anObject instanceof Version) {
116: Version anotherVersion = (Version) anObject;
117: if (getMajor() == anotherVersion.getMajor()
118: && getMinor() == anotherVersion.getMinor()) {
119: return (getMicro() == anotherVersion.getMicro()) ? true
120: : false;
121: }
122: return false;
123: }
124: return false;
125: }
126:
127: public int hashCode() {
128: return getMajor() + getMinor() + getMicro();
129: }
130:
131: /**
132: * Version as a String with an optional micro value if set.
133: */
134: public String toString() {
135: String val = major + "." + minor;
136: if (micro != 0) {
137: val += "." + micro;
138: }
139: return val;
140: }
141:
142: public int getMajor() {
143: return major;
144: }
145:
146: public int getMicro() {
147: return micro;
148: }
149:
150: public int getMinor() {
151: return minor;
152: }
153:
154: public int compareTo(Version o) {
155: int sgn = new Integer(major).compareTo(o.major);
156: if (sgn != 0) {
157: return sgn;
158: }
159:
160: sgn = new Integer(minor).compareTo(o.minor);
161: if (sgn != 0) {
162: return sgn;
163: }
164:
165: return new Integer(micro).compareTo(o.micro);
166: }
167: }
|