1 23 24 package org.apache.slide.content; 25 26 import java.io.Serializable ; 27 import java.util.StringTokenizer ; 28 import org.apache.slide.common.ObjectValidationFailedException; 29 import org.apache.slide.util.Messages; 30 31 36 public final class NodeRevisionNumber implements Serializable , Cloneable { 37 38 39 41 public static final int MAJOR = 0; 42 public static final int MINOR = 1; 43 44 public static final NodeRevisionNumber HIDDEN_0_0 = 47 new NodeRevisionNumber( 0, 0 ); 49 50 52 53 56 public NodeRevisionNumber() { 57 number = new int[2]; 58 number[MAJOR] = 1; 59 number[MINOR] = 0; 60 } 61 62 63 69 public NodeRevisionNumber(int major, int minor) { 70 number = new int[2]; 71 number[MAJOR] = major; 72 number[MINOR] = minor; 73 } 74 75 76 81 public NodeRevisionNumber(String revisionNumberStr) { 82 parseString(revisionNumberStr); 83 } 84 85 86 91 public NodeRevisionNumber(NodeRevisionNumber revisionNumber) { 92 this(revisionNumber, false); 93 } 94 95 96 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 121 122 127 private int[] number; 128 129 130 132 133 138 public int getMajor() { 139 return number[MAJOR]; 140 } 141 142 143 148 public int getMinor() { 149 return number[MINOR]; 150 } 151 152 153 159 public int getNumber(int pos) { 160 return number[pos]; 161 } 162 163 164 169 public int getNbDigits() { 170 return number.length; 171 } 172 173 174 176 177 182 private void parseString(String revisionNumberStr) { 183 StringTokenizer tokenizer = 185 new StringTokenizer (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 (tokenizer.nextToken())).intValue(); 191 } 192 cachedToString=null; } else { 194 number = new int[2]; 195 number[MAJOR] = 1; 196 number[MINOR] = 0; 197 cachedToString=null; } 199 } 200 201 202 205 private void next() { 206 if (number.length > 0) { 207 number[number.length - 1] += 1; 208 cachedToString=null; } 210 } 211 212 213 215 216 221 private String cachedToString; 222 223 228 public String 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 buf = new StringBuffer (); 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 249 } 250 251 252 257 public int hashCode() { 258 return toString().hashCode(); 259 } 260 261 262 270 public boolean equals(Object 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 284 NodeRevisionNumber cloneObject() { 285 NodeRevisionNumber result = null; 286 try { 287 result = (NodeRevisionNumber) super.clone(); 288 } catch(CloneNotSupportedException e) { 289 } 290 return result; 291 } 292 293 294 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 |