KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/content/NodeRevisionDescriptors.java,v 1.17.2.1 2004/09/26 14:14:56 luetzkendorf Exp $
3  * $Revision: 1.17.2.1 $
4  * $Date: 2004/09/26 14:14:56 $
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.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import org.apache.slide.common.ObjectValidationFailedException;
32 import org.apache.slide.util.Messages;
33
34 /**
35  * Node Revision Descriptors class.
36  *
37  * @version $Revision: 1.17.2.1 $
38  */

39 public final class NodeRevisionDescriptors implements Serializable JavaDoc, Cloneable JavaDoc {
40     
41     
42     // -------------------------------------------------------------- Constants
43

44     
45     public static final String JavaDoc MAIN_BRANCH = "main";
46     
47     
48     protected static final NodeRevisionNumber initialRevision
49         = new NodeRevisionNumber();
50     
51     
52     // ----------------------------------------------------------- Constructors
53

54     
55     /**
56      * Client side constructor.
57      */

58     public NodeRevisionDescriptors() {
59         this.latestRevisionNumbers = new Hashtable JavaDoc();
60         this.branches = new Hashtable JavaDoc();
61         this.workingRevisions = new Hashtable JavaDoc();
62         this.useVersioning = false;
63     }
64     
65     
66     /**
67      * Client side constructor.
68      */

69     public NodeRevisionDescriptors(boolean isVersioned) {
70         this.latestRevisionNumbers = new Hashtable JavaDoc();
71         this.branches = new Hashtable JavaDoc();
72         this.workingRevisions = new Hashtable JavaDoc();
73         this.useVersioning = isVersioned;
74     }
75     
76     
77     /**
78      * Store Constructor.
79      */

80     public NodeRevisionDescriptors(String JavaDoc uri,
81                                    NodeRevisionNumber initialRevision,
82                                    Hashtable JavaDoc workingRevisions,
83                                    Hashtable JavaDoc latestRevisionNumbers,
84                                    Hashtable JavaDoc branches, boolean isVersioned) {
85         this.uri = uri;
86         this.latestRevisionNumbers = latestRevisionNumbers;
87         this.branches = branches;
88         this.workingRevisions = workingRevisions;
89         this.useVersioning = isVersioned;
90     }
91     
92     
93     // ----------------------------------------------------- Instance Variables
94

95     
96     /**
97      * Use versioning.
98      */

99     private boolean useVersioning;
100     
101     
102     /**
103      * Uri.
104      */

105     private String JavaDoc uri;
106     
107     
108     /**
109      * The original URI (in case URI was redirected).
110      */

111     private String JavaDoc originalUri;
112     
113     
114     /**
115      * Working revisions.
116      *
117      * <p>TODO: <code>workingRevisions</code> are only writen never read!
118      */

119     private Hashtable JavaDoc workingRevisions;
120     
121     
122     /**
123      * Latest revision numbers.
124      */

125     private Hashtable JavaDoc latestRevisionNumbers;
126     
127     
128     /**
129      * Branches.
130      */

131     private Hashtable JavaDoc branches;
132     
133     
134     // ------------------------------------------------------------- Properties
135

136     
137     /**
138      * Use versioning accessor.
139      */

140     public boolean isVersioned() {
141         return this.useVersioning;
142     }
143     
144     
145     /**
146      * Use versioning mutator.
147      */

148     void setVersioned(boolean useVersioning) {
149         this.useVersioning = useVersioning;
150     }
151     
152     
153     /**
154      * Has revision ?
155      */

156     public boolean hasRevisions() {
157         return !(this.latestRevisionNumbers.isEmpty());
158     }
159     
160     
161     /**
162      * Uri accessor.
163      */

164     public String JavaDoc getUri() {
165         return this.uri;
166     }
167     
168     
169     /**
170      * OriginalUri accessor.
171      */

172     public String JavaDoc getOriginalUri() {
173         if( this.originalUri != null )
174             return this.originalUri;
175         else
176             return this.uri;
177     }
178     
179     
180     /**
181      * OriginalUri mutator.
182      */

183     void setOriginalUri( String JavaDoc originalUri ) {
184         this.originalUri = originalUri;
185     }
186     
187     
188     /**
189      * Uri mutator.
190      */

191     public void setUri(String JavaDoc uri) {
192         this.uri = uri;
193     }
194     
195     
196     /**
197      * Get initial revision.
198      */

199     public NodeRevisionNumber getInitialRevision() {
200         return NodeRevisionDescriptors.initialRevision;
201     }
202     
203     
204     /**
205      * Get latest revision from main branch.
206      */

207     public NodeRevisionNumber getLatestRevision() {
208         return getLatestRevision(MAIN_BRANCH);
209     }
210     
211     
212     /**
213      * Get latest revision from a branch.
214      */

215     public NodeRevisionNumber getLatestRevision(String JavaDoc branchName) {
216         Object JavaDoc number = null;
217         number = this.latestRevisionNumbers.get(branchName);
218         if (number != null) {
219             return (NodeRevisionNumber) number;
220         } else {
221             return null;
222         }
223     }
224     
225     
226     /**
227      * Latest revision mutator.
228      */

229     void setLatestRevision(NodeRevisionNumber number) {
230         this.latestRevisionNumbers.put(MAIN_BRANCH, number);
231     }
232     
233     
234     /**
235      * Latest revision mutator.
236      */

237     void setLatestRevision(String JavaDoc branch, NodeRevisionNumber number) {
238         this.latestRevisionNumbers.put(branch, number);
239     }
240     
241     
242     // --------------------------------------------------------- Public Methods
243

244     
245     /**
246      * Get relations.
247      */

248     public Enumeration JavaDoc getSuccessors(NodeRevisionNumber number) {
249         Object JavaDoc result = this.branches.get(number);
250         if (result != null) {
251             return ((Vector JavaDoc) result).elements();
252         } else {
253             return null;
254         }
255     }
256     
257     /**
258      * Return true, if ancNrn and descNrn are in an ancestor-descendant relationship in
259      * this history.
260      */

261     public boolean isAncestorDescendant( NodeRevisionNumber ancNrn, NodeRevisionNumber descNrn ) {
262         
263         if( ancNrn.equals(descNrn) )
264             return true;
265         
266         Enumeration JavaDoc ancSuccs = getSuccessors( ancNrn );
267         while( ancSuccs != null && ancSuccs.hasMoreElements() ) {
268             NodeRevisionNumber ancSuccNrn = (NodeRevisionNumber)ancSuccs.nextElement();
269             if( isAncestorDescendant(ancSuccNrn, descNrn) )
270                 return true;
271         }
272         
273         return false;
274     }
275     /**
276      * Returns true, if and only if uri != originalUri
277      */

278     public boolean isRedirected() {
279         return( !uri.equals(originalUri) );
280     }
281     
282     // -------------------------------------------------------- Package Methods
283

284     
285     /**
286      * Add relation.
287      */

288     void setSuccessors(NodeRevisionNumber number,
289                        NodeRevisionNumber successor) {
290         Vector JavaDoc tempVector = new Vector JavaDoc();
291         tempVector.addElement(successor);
292         setSuccessors(number, tempVector);
293     }
294     
295     
296     /**
297      * Add relation.
298      */

299     void setSuccessors(NodeRevisionNumber number, Vector JavaDoc successors) {
300         this.branches.put(number, successors);
301     }
302     
303     
304     /**
305      * Add relation.
306      */

307     void addSuccessor(NodeRevisionNumber number,
308                       NodeRevisionNumber successor) {
309         Object JavaDoc result = this.branches.get(number);
310         if (result != null) {
311             ((Vector JavaDoc) result).addElement(successor);
312         } else {
313             setSuccessors(number, successor);
314         }
315     }
316     
317     
318     /**
319      * Remove relation.
320      */

321     void removeSuccessor(NodeRevisionNumber number,
322                          NodeRevisionNumber successor) {
323         Object JavaDoc result = this.branches.get(number);
324         if (result != null) {
325             ((Vector JavaDoc) result).removeElement(successor);
326         }
327     }
328     
329     
330     /**
331      * Enumerate all revision numbers in all branches.
332      */

333     public Enumeration JavaDoc enumerateRevisionNumbers() {
334         return this.branches.keys();
335     }
336     
337
338     /**
339      * Enumerate all branch names.
340      */

341    public Enumeration JavaDoc enumerateBranchNames() {
342      return this.latestRevisionNumbers.keys();
343    }
344
345  
346     
347     
348     
349     // --------------------------------------------------------- Object Methods
350

351     
352     /**
353      * Clone.
354      *
355      * @return Object clone
356      */

357     public NodeRevisionDescriptors cloneObject() {
358         NodeRevisionDescriptors result = null;
359         try {
360             result = (NodeRevisionDescriptors) super.clone();
361             // TODO : No cloning of the working revisions list for now
362
// Cloning branches
363
Hashtable JavaDoc branchesClone = new Hashtable JavaDoc();
364             Enumeration JavaDoc branchesList = this.branches.keys();
365             while (branchesList.hasMoreElements()) {
366                 Object JavaDoc key = branchesList.nextElement();
367                 Vector JavaDoc value = (Vector JavaDoc) this.branches.get(key);
368                 branchesClone.put(key, value.clone());
369             }
370             result.branches = branchesClone;
371             // Cloning latest revision list
372
result.latestRevisionNumbers =
373                 (Hashtable JavaDoc) this.latestRevisionNumbers.clone();
374         } catch(CloneNotSupportedException JavaDoc e) {
375             e.printStackTrace();
376         }
377         return result;
378     }
379     
380     
381     /**
382      * Equals.
383      *
384      * @param obj Object to test
385      * @return boolean True if the two object are equal :
386      * <li>obj is of type NodeRevisionDescriptors and is not null</li>
387      * <li>it has the same Uri</li>
388      */

389     public boolean equals(Object JavaDoc obj) {
390         boolean result = false;
391         if ((obj != null) && (obj instanceof NodeRevisionDescriptors)) {
392             NodeRevisionDescriptors revisionDescriptors =
393                 (NodeRevisionDescriptors) obj;
394             result = this.getUri().equals(revisionDescriptors.getUri());
395         }
396         return result;
397     }
398     
399     
400     /**
401      * Validate.
402      */

403     public void validate(String JavaDoc expectedUri) {
404         
405         if (uri == null)
406             throw new ObjectValidationFailedException
407                 (expectedUri, Messages.message
408                  (NodeRevisionDescriptors.class.getName() + ".nullUri"));
409         
410         if (!(uri.equals(expectedUri) || uri.equals(expectedUri + "/"))) {
411             System.out.println("Uri1 : " + uri + " Uri2 : " + expectedUri);
412             //throw new ObjectValidationFailedException
413
//(expectedUri, Messages.message
414
//(NodeRevisionDescriptors.class.getName() + ".incorrectUri"));
415
}
416         
417         if (workingRevisions == null)
418             throw new ObjectValidationFailedException
419                 (uri, Messages.message
420                  (NodeRevisionDescriptors.class.getName()
421                   + ".nullWorkingRevisions"));
422         
423         if (latestRevisionNumbers == null)
424             throw new ObjectValidationFailedException
425                 (uri, Messages.message
426                  (NodeRevisionDescriptors.class.getName()
427                   + ".nullLatestRevisionNumbers"));
428         
429         if (branches == null)
430             throw new ObjectValidationFailedException
431                 (uri, Messages.message
432                  (NodeRevisionDescriptors.class.getName()
433                   + ".nullBranches"));
434         
435         // FIXME : Check branches integrity. Problem : It's quite expensive.
436

437     }
438     
439     
440 }
441
442
Popular Tags