KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > service > cmr > repository > NodeRef


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.service.cmr.repository;
18
19 import java.io.Serializable JavaDoc;
20
21 import org.alfresco.error.AlfrescoRuntimeException;
22
23 /**
24  * Reference to a node
25  *
26  * @author Derek Hulley
27  */

28 public final class NodeRef implements EntityRef, Serializable JavaDoc
29 {
30    
31     private static final long serialVersionUID = 3760844584074227768L;
32     private static final String JavaDoc URI_FILLER = "/";
33     
34     private final StoreRef storeRef;
35     private final String JavaDoc id;
36
37     /**
38      * Construct a Node Reference from a Store Reference and Node Id
39      *
40      * @param storeRef store reference
41      * @param id the manually assigned identifier of the node
42      */

43     public NodeRef(StoreRef storeRef, String JavaDoc id)
44     {
45         if (storeRef == null)
46         {
47             throw new IllegalArgumentException JavaDoc(
48                     "Store reference may not be null");
49         }
50         if (id == null)
51         {
52             throw new IllegalArgumentException JavaDoc("Node id may not be null");
53         }
54
55         this.storeRef = storeRef;
56         this.id = id;
57     }
58
59     /**
60      * Construct a Node Reference from a string representation of a Node Reference.
61      * <p>
62      * The string representation of a Node Reference is as follows:
63      * <p>
64      * <pre><storeref>/<nodeId></pre>
65      *
66      * @param nodeRef the string representation of a node ref
67      */

68     public NodeRef(String JavaDoc nodeRef)
69     {
70         int lastForwardSlash = nodeRef.lastIndexOf('/');
71         if(lastForwardSlash == -1)
72         {
73             throw new AlfrescoRuntimeException("Invalid node ref - does not contain forward slash: " + nodeRef);
74         }
75         this.storeRef = new StoreRef(nodeRef.substring(0, lastForwardSlash));
76         this.id = nodeRef.substring(lastForwardSlash+1);
77     }
78
79     public String JavaDoc toString()
80     {
81         return storeRef.toString() + URI_FILLER + id;
82     }
83
84     /**
85      * Override equals for this ref type
86      *
87      * @see java.lang.Object#equals(java.lang.Object)
88      */

89     public boolean equals(Object JavaDoc obj)
90     {
91         if (this == obj)
92         {
93             return true;
94         }
95         if (obj instanceof NodeRef)
96         {
97             NodeRef that = (NodeRef) obj;
98             return (this.id.equals(that.id)
99                     && this.storeRef.equals(that.storeRef));
100         }
101         else
102         {
103             return false;
104         }
105     }
106     
107     /**
108      * Hashes on ID alone. As the number of copies of a particular node will be minimal, this is acceptable
109      */

110     public int hashCode()
111     {
112         return id.hashCode();
113     }
114
115     /**
116      * @return The StoreRef part of this reference
117      */

118     public final StoreRef getStoreRef()
119     {
120         return storeRef;
121     }
122
123     /**
124      * @return The Node Id part of this reference
125      */

126     public final String JavaDoc getId()
127     {
128         return id;
129     }
130
131     /**
132      * Helper class to convey the status of a <b>node</b>.
133      *
134      * @author Derek Hulley
135      */

136     public static class Status
137     {
138         private final String JavaDoc changeTxnId;
139         private final boolean deleted;
140         
141         public Status(String JavaDoc changeTxnId, boolean deleted)
142         {
143             this.changeTxnId = changeTxnId;
144             this.deleted = deleted;
145         }
146         /**
147          * @return Returns the ID of the last transaction to change the node
148          */

149         public String JavaDoc getChangeTxnId()
150         {
151             return changeTxnId;
152         }
153         /**
154          * @return Returns true if the node has been deleted, otherwise false
155          */

156         public boolean isDeleted()
157         {
158             return deleted;
159         }
160     }
161 }
Popular Tags