KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > module > CmsModuleVersion


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/module/CmsModuleVersion.java,v $
3  * Date : $Date: 2006/03/27 14:53:03 $
4  * Version: $Revision: 1.16 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.module;
33
34 import org.opencms.main.CmsIllegalArgumentException;
35 import org.opencms.main.CmsRuntimeException;
36 import org.opencms.util.CmsStringUtil;
37
38 /**
39  * A version number for an OpenCms module.<p>
40  *
41  * A module version number has the form <code>n1.n2.n3.n4</code>.
42  * Only <code>n1</code> is required, <code>n2 - n4</code> are optional.<p>
43  *
44  * The valid range for each <code>n</code> is 0 - 999.
45  * Examples for valid version numbers are <code>0.9</code>, <code>1.0.0.5</code>
46  * or <code>5</code>.
47  * The maximum version number is <code>999.999.999.999</code>.<p>
48  *
49  * The comparison is started with <code>n1</code> being the most important value,
50  * followed by <code>n2 - n4</code>.
51  * For example <code>5.0.0.1 > 4.999.999.999</code> since <code>5 > 4</code>.<p>
52  *
53  * For any <code>n1 - n4</code>, if <code>n > 0</code> leading zeros are ignored.
54  * So <code>001.002.004.004 = 1.2.3.4</code>. Unrequired leading zeros are automatically
55  * stripped from version numbers.<p>
56  *
57  * @version $Revision: 1.16 $
58  *
59  * @since 6.0.0
60  */

61 public class CmsModuleVersion implements Comparable JavaDoc {
62
63     /** Default version for new modules. */
64     public static final String JavaDoc DEFAULT_VERSION = "0.1";
65
66     /** The dot count of the version. */
67     private int m_dots;
68
69     /** The version number (for comparisons). */
70     private long m_number;
71
72     /** Indicates if the module version was already updated. */
73     private boolean m_updated;
74
75     /** The version String. */
76     private String JavaDoc m_version;
77
78     /**
79      * Creates a new module version based on a String.<p>
80      *
81      * @param version the version to set
82      */

83     public CmsModuleVersion(String JavaDoc version) {
84
85         setVersion(version);
86     }
87
88     /**
89      * @see java.lang.Comparable#compareTo(java.lang.Object)
90      */

91     public int compareTo(Object JavaDoc obj) {
92
93         if (obj == this) {
94             return 0;
95         }
96         if (obj instanceof CmsModuleVersion) {
97             if (m_version == null) {
98                 return 0;
99             }
100             CmsModuleVersion other = (CmsModuleVersion)obj;
101             if (m_number == other.m_number) {
102                 return 0;
103             }
104             return (m_number > other.m_number) ? 1 : -1;
105         }
106         return 0;
107     }
108
109     /**
110      * @see java.lang.Object#equals(java.lang.Object)
111      */

112     public boolean equals(Object JavaDoc obj) {
113
114         if (obj == this) {
115             return true;
116         }
117         if (obj instanceof CmsModuleVersion) {
118             CmsModuleVersion other = (CmsModuleVersion)obj;
119             if (m_version == null) {
120                 return (other.m_version == null);
121             }
122             return m_version.equals(other.m_version);
123         }
124         return false;
125     }
126
127     /**
128      * Returns the current version String.<p>
129      *
130      * @return the current version String
131      */

132     public String JavaDoc getVersion() {
133
134         return m_version;
135     }
136
137     /**
138      * @see java.lang.Object#hashCode()
139      */

140     public int hashCode() {
141
142         if (m_version == null) {
143             return 0;
144         }
145
146         return m_version.hashCode();
147     }
148
149     /**
150      * Sets the version as a String.<p>
151      *
152      * @param version the version String to set
153      */

154     public void setVersion(String JavaDoc version) {
155
156         m_number = 0L;
157         if ((version == null) || (version.charAt(0) == '.') || (version.charAt(version.length() - 1) == '.')) {
158             throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NOT_NUMBER_0));
159         }
160         String JavaDoc[] split = CmsStringUtil.splitAsArray(version, '.');
161         m_dots = split.length;
162         if (m_dots > 4) {
163             throw new CmsIllegalArgumentException(Messages.get().container(
164                 Messages.ERR_INVALID_VERSION_LENGTH_1,
165                 version));
166         }
167         String JavaDoc[] numbers = new String JavaDoc[5];
168         System.arraycopy(split, 0, numbers, 1, m_dots);
169         numbers[0] = "1";
170         for (int i = 1 + m_dots; i < 5; i++) {
171             numbers[i] = "0";
172         }
173         for (int i = numbers.length - 1; i >= 0; i--) {
174             try {
175                 int number = Integer.valueOf(numbers[numbers.length - i - 1]).intValue();
176
177                 if ((number > 999) || (number < 0)) {
178                     throw new CmsIllegalArgumentException(Messages.get().container(
179                         Messages.ERR_INVALID_VERSION_SUBNUMBER_1,
180                         new Integer JavaDoc(number)));
181                 }
182                 m_number = (long)Math.pow(1000.0, i) * number + m_number;
183             } catch (NumberFormatException JavaDoc e) {
184                 // no valid version provided
185
throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NOT_NUMBER_0));
186             }
187         }
188
189         setVersion(m_number);
190     }
191
192     /**
193      * @see java.lang.Object#toString()
194      */

195     public String JavaDoc toString() {
196
197         return getVersion();
198     }
199
200     /**
201      * Increments this version number by 1 in the last digit.<p>
202      */

203     protected void increment() {
204
205         if (m_number < 1999999999999L) {
206             m_number += (long)Math.pow(1000.0, (4 - m_dots));
207             setVersion(m_number);
208         } else {
209             throw new CmsRuntimeException(Messages.get().container(Messages.ERR_MODULE_VERSION_NUMBER_0));
210         }
211     }
212
213     /**
214      * Returns the updated status.<p>
215      *
216      * @return the updated status
217      */

218     protected boolean isUpdated() {
219
220         return m_updated;
221     }
222
223     /**
224      * Sets the updated status.<p>
225      *
226      * @param updated the updated status to set
227      */

228     protected void setUpdated(boolean updated) {
229
230         m_updated = updated;
231     }
232
233     /**
234      * Sets the version as a number.<p>
235      *
236      * @param number the version number to set
237      */

238     private void setVersion(long number) {
239
240         String JavaDoc result = "";
241         for (int i = 0; i < 4; i++) {
242             long mod = number % 1000L;
243             number = number / 1000L;
244             if (m_dots >= (4 - i)) {
245                 if (m_dots > (4 - i)) {
246                     result = '.' + result;
247                 }
248                 result = "" + mod + result;
249             }
250         }
251
252         m_version = result;
253     }
254 }
Popular Tags