KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > content > NodeRevisionNumber


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/content/NodeRevisionNumber.java,v 1.9 2004/07/28 09:37:54 ib Exp $
3  * $Revision: 1.9 $
4  * $Date: 2004/07/28 09:37:54 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.content;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import org.apache.slide.common.ObjectValidationFailedException;
29 import org.apache.slide.util.Messages;
30
31 /**
32  * Node Revision Number class.
33  *
34  * @version $Revision: 1.9 $
35  */

36 public final class NodeRevisionNumber implements Serializable JavaDoc, Cloneable JavaDoc {
37     
38     
39     // -------------------------------------------------------------- Constants
40

41     public static final int MAJOR = 0;
42     public static final int MINOR = 1;
43     
44     // The hidden revision number is used for DeltaV-related system data
45
// ("version-history" and "backup" branches in VHR and VCR resources)
46
public static final NodeRevisionNumber HIDDEN_0_0 =
47         new NodeRevisionNumber( 0, 0 ); // major=0, minor=0
48

49     
50     // ----------------------------------------------------------- Constructors
51

52     
53     /**
54      * Create a new revision number.
55      */

56     public NodeRevisionNumber() {
57         number = new int[2];
58         number[MAJOR] = 1;
59         number[MINOR] = 0;
60     }
61     
62     
63     /**
64      * Create a new revision number.
65      *
66      * @param major
67      * @param minor
68      */

69     public NodeRevisionNumber(int major, int minor) {
70         number = new int[2];
71         number[MAJOR] = major;
72         number[MINOR] = minor;
73     }
74     
75     
76     /**
77      * Create a new revision number.
78      *
79      * @param revisionNumberStr String representation of the revision number
80      */

81     public NodeRevisionNumber(String JavaDoc revisionNumberStr) {
82         parseString(revisionNumberStr);
83     }
84     
85     
86     /**
87      * Create a new revision number based on a previous revision number.
88      *
89      * @param revisionNumber Previous revision number
90      */

91     public NodeRevisionNumber(NodeRevisionNumber revisionNumber) {
92         this(revisionNumber, false);
93     }
94     
95     
96     /**
97      * Create a new revision number based on a previous revision number.
98      *
99      * @param revisionNumber Previous revision number
100      * @param createBranch True if a new branch is to be created
101      */

102     public NodeRevisionNumber(NodeRevisionNumber revisionNumber,
103                               boolean createBranch) {
104         if (revisionNumber != null) {
105             if (createBranch) {
106                 parseString(revisionNumber.toString() + ".1");
107             } else {
108                 parseString(revisionNumber.toString());
109                 next();
110             }
111         } else {
112             number = new int[2];
113             number[MAJOR] = 1;
114             number[MINOR] = 0;
115         }
116     }
117     
118     
119     // ----------------------------------------------------- Instance Variables
120

121     
122     /**
123      * Number storage.
124      * <b>Note: when this array or it's content is modified, you must reset
125      * {@link #cachedToString} to null!</b>
126      */

127     private int[] number;
128     
129     
130     // --------------------------------------------------------- Public Methods
131

132     
133     /**
134      * Get the first number in the revision number.
135      *
136      * @return int
137      */

138     public int getMajor() {
139         return number[MAJOR];
140     }
141     
142     
143     /**
144      * Get the second number in the revision number.
145      *
146      * @return int
147      */

148     public int getMinor() {
149         return number[MINOR];
150     }
151     
152     
153     /**
154      * Get a number by specifying its order.
155      *
156      * @param pos Position of the number
157      * @return int
158      */

159     public int getNumber(int pos) {
160         return number[pos];
161     }
162     
163     
164     /**
165      * Return number of digits if the revision number.
166      *
167      * @return int
168      */

169     public int getNbDigits() {
170         return number.length;
171     }
172     
173     
174     // -------------------------------------------------------- Private Methods
175

176     
177     /**
178      * Parse a String.
179      *
180      * @param revisionNumberStr String representation of the revision number
181      */

182     private void parseString(String JavaDoc revisionNumberStr) {
183         // We tokenize the string using "." as a delimiter.
184
StringTokenizer JavaDoc tokenizer =
185             new StringTokenizer JavaDoc(revisionNumberStr, ".");
186         int nbDigits = tokenizer.countTokens();
187         if (nbDigits > 0) {
188             number = new int[nbDigits];
189             for (int i=0; i<nbDigits; i++) {
190                 number[i] = (new Integer JavaDoc(tokenizer.nextToken())).intValue();
191             }
192             cachedToString=null; // reset cache
193
} else {
194             number = new int[2];
195             number[MAJOR] = 1;
196             number[MINOR] = 0;
197             cachedToString=null; // reset cache
198
}
199     }
200     
201     
202     /**
203      * Next revision.
204      */

205     private void next() {
206         if (number.length > 0) {
207             number[number.length - 1] += 1;
208             cachedToString=null; // reset cache
209
}
210     }
211     
212     
213     // --------------------------------------------------------- Public Methods
214

215
216     /**
217      * Caches the result of last toString()-call. This will also heavily improve
218      * hashCode() as the String.hashCode() method is also buffered.
219      * This imust be resetet to null whenever the {@link #number} array changes.
220      */

221     private String JavaDoc cachedToString;
222
223     /**
224      * Get a String representation of the revision number.
225      *
226      * @return String
227      */

228     public String JavaDoc toString() {
229         if(cachedToString!=null) return cachedToString;
230         int count=number.length;
231         if(count==2) cachedToString=(number[MAJOR]+"."+number[MINOR]);
232         else
233         {
234           StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
235           buf.append(number[MAJOR]);
236           for (int i=1; i<count; i++) {
237               buf.append('.').append(number[i]);
238           }
239           cachedToString=buf.toString();
240         }
241         return cachedToString;
242         /* old code being replaced by the cached one
243         StringBuffer buf = new StringBuffer();
244         buf.append(number[MAJOR]);
245         for (int i=1; i<number.length; i++) {
246             buf.append(".").append(number[i]);
247         }
248         return buf.toString();*/

249     }
250     
251     
252     /**
253      * HashCode.
254      *
255      * @return int
256      */

257     public int hashCode() {
258         return toString().hashCode();
259     }
260     
261     
262     /**
263      * Equals.
264      *
265      * @param obj Object to test
266      * @return boolean True if the two object are equal :
267      * <li>obj is of type NodeRevisionNumber and is not null</li>
268      * <li>The string representations are equal are equal</li>
269      */

270     public boolean equals(Object JavaDoc obj) {
271         boolean result = false;
272         if ((obj != null) && (obj instanceof NodeRevisionNumber)) {
273             result = (this.toString().equals(obj.toString()));
274         }
275         return result;
276     }
277     
278     
279     /**
280      * Clone.
281      *
282      * @return Object clone
283      */

284     NodeRevisionNumber cloneObject() {
285         NodeRevisionNumber result = null;
286         try {
287             result = (NodeRevisionNumber) super.clone();
288         } catch(CloneNotSupportedException JavaDoc e) {
289         }
290         return result;
291     }
292     
293     
294     /**
295      * Validate.
296      */

297     public void validate() {
298         
299         if (number == null)
300             throw new ObjectValidationFailedException
301                 (Messages.message
302                  (NodeRevisionNumber.class.getName() + ".nullNumber"));
303         
304         if (number.length < 2)
305             throw new ObjectValidationFailedException
306                 (Messages.message
307                  (NodeRevisionNumber.class.getName() + ".invalidNumber"));
308         
309     }
310     
311     
312 }
313
Popular Tags