001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.Validator;
026:
027: import java.util.Map;
028: import java.util.StringTokenizer;
029:
030: /**
031: * <a href="Version.java.html"><b><i>View Source</i></b></a>
032: *
033: * @author Jorge Ferrer
034: *
035: */
036: public class Version implements Comparable {
037:
038: public static final String UNKNOWN = "unknown";
039:
040: public static Version getInstance(String version) {
041: Version versionObj = (Version) _versions.get(version);
042:
043: if (versionObj == null) {
044: versionObj = new Version(version);
045:
046: _versions.put(version, versionObj);
047: }
048:
049: return versionObj;
050: }
051:
052: public String getMajor() {
053: if (_major == null) {
054: return "0";
055: }
056:
057: return _major;
058: }
059:
060: public String getMinor() {
061: if (_minor == null) {
062: return "0";
063: }
064:
065: return _minor;
066: }
067:
068: public String getBugFix() {
069: if (_bugFix == null) {
070: return "0";
071: }
072:
073: return _bugFix;
074: }
075:
076: public String getBuildNumber() {
077: return _buildNumber;
078: }
079:
080: public boolean isLaterVersionThan(String version) {
081: if (compareTo(getInstance(version)) > 0) {
082: return true;
083: } else {
084: return false;
085: }
086: }
087:
088: public boolean isPreviousVersionThan(String version) {
089: if (compareTo(getInstance(version)) < 0) {
090: return true;
091: } else {
092: return false;
093: }
094: }
095:
096: public boolean isSameVersionAs(String version) {
097: if (compareTo(getInstance(version)) == 0) {
098: return true;
099: } else {
100: return false;
101: }
102: }
103:
104: public boolean includes(Version version) {
105: if (equals(version)) {
106: return true;
107: }
108:
109: if (getMajor().equals(StringPool.STAR)) {
110: return true;
111: }
112:
113: if (getMajor().equals(version.getMajor())) {
114: if (getMinor().equals(StringPool.STAR)) {
115: return true;
116: }
117:
118: if (getMinor().equals(version.getMinor())) {
119: if (getBugFix().equals(StringPool.STAR)) {
120: return true;
121: }
122:
123: if (getBugFix().equals(version.getBugFix())) {
124: if (getBuildNumber().equals(StringPool.STAR)
125: || getBuildNumber().equals(
126: version.getBuildNumber())) {
127:
128: return true;
129: }
130: } else if (_contains(getBugFix(), version.getBugFix())) {
131: return true;
132: }
133: } else if (_contains(getMinor(), version.getMinor())) {
134: return true;
135: }
136: } else if (_contains(getMajor(), version.getMajor())) {
137: return true;
138: }
139:
140: return false;
141: }
142:
143: public int compareTo(Object obj) {
144: if ((obj == null) || (!(obj instanceof Version))) {
145: return 1;
146: }
147:
148: Version version = (Version) obj;
149:
150: // Unknown is always considered a lower version
151:
152: if (version.toString().equals(UNKNOWN)) {
153: return 1;
154: }
155:
156: if (toString().equals(UNKNOWN)) {
157: return -1;
158: }
159:
160: int result = getMajor().compareTo(version.getMajor());
161:
162: if (result != 0) {
163: return result;
164: }
165:
166: result = getMinor().compareTo(version.getMinor());
167:
168: if (result != 0) {
169: return result;
170: }
171:
172: result = getBugFix().compareTo(version.getBugFix());
173:
174: if (result != 0) {
175: return result;
176: }
177:
178: return getBuildNumber().compareTo(version.getBuildNumber());
179: }
180:
181: public boolean equals(Object obj) {
182: if ((obj == null) || (!(obj instanceof Version))) {
183: return false;
184: }
185:
186: Version version = (Version) obj;
187:
188: String versionString1 = toString();
189: String versionString2 = version.toString();
190:
191: if (versionString1.equals(UNKNOWN)
192: || versionString2.equals(UNKNOWN)) {
193: return false;
194: }
195:
196: return versionString1.equals(versionString2);
197: }
198:
199: public int hashCode() {
200: return toString().hashCode();
201: }
202:
203: public String toString() {
204: StringMaker sm = new StringMaker();
205:
206: sm.append(_major);
207:
208: if (Validator.isNotNull(_minor)) {
209: sm.append(_SEPARATOR);
210: sm.append(_minor);
211:
212: if (Validator.isNotNull(_bugFix)) {
213: sm.append(_SEPARATOR);
214: sm.append(_bugFix);
215:
216: if (Validator.isNotNull(_buildNumber)) {
217: sm.append(_SEPARATOR);
218: sm.append(_buildNumber);
219: }
220: }
221: }
222:
223: return sm.toString();
224: }
225:
226: protected Version(String version) {
227: StringTokenizer st = new StringTokenizer(version, _SEPARATOR);
228:
229: _major = st.nextToken();
230:
231: if (st.hasMoreTokens()) {
232: _minor = st.nextToken();
233: }
234:
235: if (st.hasMoreTokens()) {
236: _bugFix = st.nextToken();
237: }
238:
239: StringMaker buildNumber = new StringMaker();
240:
241: while (st.hasMoreTokens()) {
242: buildNumber.append(st.nextToken());
243:
244: if (st.hasMoreTokens()) {
245: buildNumber.append(_SEPARATOR);
246: }
247: }
248:
249: _buildNumber = buildNumber.toString();
250: }
251:
252: private boolean _contains(String containerStr, String numberStr) {
253: if (containerStr.endsWith(StringPool.PLUS)) {
254: String containerNumberStr = containerStr.substring(0,
255: containerStr.length() - 1);
256:
257: try {
258: int containerNumber = Integer
259: .parseInt(containerNumberStr);
260: int number = Integer.parseInt(numberStr);
261:
262: return containerNumber <= number;
263: } catch (NumberFormatException nfe) {
264: return false;
265: }
266: }
267: return false;
268: }
269:
270: private static final String _SEPARATOR = StringPool.PERIOD;
271:
272: private static Map _versions = CollectionFactory.getSyncHashMap();
273:
274: private String _major;
275: private String _minor;
276: private String _bugFix;
277: private String _buildNumber;
278:
279: }
|