KickJava   Java API By Example, From Geeks To Geeks.

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


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.service.namespace.QName;
22 import org.alfresco.util.EqualsHelper;
23
24 /**
25  * This class represents a child relationship between two nodes. This
26  * relationship is named.
27  * <p>
28  * So it requires the parent node ref, the child node ref and the name of the
29  * child within the particular parent.
30  * <p>
31  * This combination is not a unique identifier for the relationship with regard
32  * to structure. In use this does not matter as we have no concept of order,
33  * particularly in the index.
34  *
35  * @author andyh
36  *
37  */

38 public class ChildAssociationRef
39         implements EntityRef, Comparable JavaDoc<ChildAssociationRef>, Serializable JavaDoc
40 {
41     private static final long serialVersionUID = 4051322336257127729L;
42
43     private QName assocTypeQName;
44     private NodeRef parentRef;
45     private QName childQName;
46     private NodeRef childRef;
47     private boolean isPrimary;
48     private int nthSibling;
49     
50
51     /**
52      * Construct a representation of a parent --- name ----> child relationship.
53      *
54      * @param assocTypeQName
55      * the type of the association
56      * @param parentRef
57      * the parent reference - may be null
58      * @param childQName
59      * the qualified name of the association - may be null
60      * @param childRef
61      * the child node reference. This must not be null.
62      * @param isPrimary
63      * true if this represents the primary parent-child relationship
64      * @param nthSibling
65      * the nth association with the same properties. Usually -1 to be
66      * ignored.
67      */

68     public ChildAssociationRef(
69             QName assocTypeQName,
70             NodeRef parentRef,
71             QName childQName,
72             NodeRef childRef,
73             boolean isPrimary,
74             int nthSibling)
75     {
76         this.assocTypeQName = assocTypeQName;
77         this.parentRef = parentRef;
78         this.childQName = childQName;
79         this.childRef = childRef;
80         this.isPrimary = isPrimary;
81         this.nthSibling = nthSibling;
82
83         // check
84
if (childRef == null)
85         {
86             throw new IllegalArgumentException JavaDoc("Child reference may not be null");
87         }
88     }
89
90     /**
91      * Constructs a <b>non-primary</b>, -1th sibling parent-child association
92      * reference.
93      *
94      * @see ChildAssociationRef#ChildAssocRef(QName, NodeRef, QName, NodeRef, boolean, int)
95      */

96     public ChildAssociationRef(QName assocTypeQName, NodeRef parentRef, QName childQName, NodeRef childRef)
97     {
98         this(assocTypeQName, parentRef, childQName, childRef, false, -1);
99     }
100
101     /**
102      * Compares:
103      * <ul>
104      * <li>{@link #assocTypeQName}</li>
105      * <li>{@link #parentRef}</li>
106      * <li>{@link #childRef}</li>
107      * <li>{@link #childQName}</li>
108      * </ul>
109      */

110     public boolean equals(Object JavaDoc o)
111     {
112         if (this == o)
113         {
114             return true;
115         }
116         if (!(o instanceof ChildAssociationRef))
117         {
118             return false;
119         }
120         ChildAssociationRef other = (ChildAssociationRef) o;
121
122         return (EqualsHelper.nullSafeEquals(this.assocTypeQName, other.assocTypeQName)
123                 && EqualsHelper.nullSafeEquals(this.parentRef, other.parentRef)
124                 && EqualsHelper.nullSafeEquals(this.childQName, other.childQName)
125                 && EqualsHelper.nullSafeEquals(this.childRef, other.childRef));
126     }
127
128     public int hashCode()
129     {
130         int hashCode = ((getTypeQName() == null) ? 0 : getTypeQName().hashCode());
131         hashCode = 37 * hashCode + ((getParentRef() == null) ? 0 : getParentRef().hashCode());
132         hashCode = 37 * hashCode + ((getQName() == null) ? 0 : getQName().hashCode());
133         hashCode = 37 * hashCode + getChildRef().hashCode();
134         return hashCode;
135     }
136
137     /**
138      * @see #setNthSibling(int)
139      */

140     public int compareTo(ChildAssociationRef another)
141     {
142         int thisVal = this.nthSibling;
143         int anotherVal = another.nthSibling;
144         return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
145     }
146
147     public String JavaDoc toString()
148     {
149         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
150         sb.append("[").append(getTypeQName()).append("]");
151         sb.append(getParentRef());
152         sb.append(" --- ").append(getQName()).append(" ---> ");
153         sb.append(getChildRef());
154         return sb.toString();
155     }
156     
157     /**
158      * Get the qualified name of the association type
159      *
160      * @return Returns the qualified name of the parent-child association type
161      * as defined in the data dictionary. It may be null if this is the
162      * imaginary association to the root node.
163      */

164     public QName getTypeQName()
165     {
166         return assocTypeQName;
167     }
168
169     /**
170      * Get the qualified name of the parent-child association
171      *
172      * @return Returns the qualified name of the parent-child association. It
173      * may be null if this is the imaginary association to a root node.
174      */

175     public QName getQName()
176     {
177         return childQName;
178     }
179
180     /**
181      * @return Returns the child node reference - never null
182      */

183     public NodeRef getChildRef()
184     {
185         return childRef;
186     }
187
188     /**
189      * @return Returns the parent node reference, which may be null if this
190      * represents the imaginary reference to the root node
191      */

192     public NodeRef getParentRef()
193     {
194         return parentRef;
195     }
196
197     /**
198      * @return Returns true if this represents a primary association
199      */

200     public boolean isPrimary()
201     {
202         return isPrimary;
203     }
204
205     /**
206      * @return Returns the nth sibling required
207      */

208     public int getNthSibling()
209     {
210         return nthSibling;
211     }
212
213     /**
214      * Allows post-creation setting of the ordering index. This is a helper
215      * so that sorted sets and lists can be easily sorted.
216      * <p>
217      * This index is <b>in no way absolute</b> and should change depending on
218      * the results that appear around this instance. Therefore, the sibling
219      * number cannot be used to construct, say, sibling number 5. Sibling
220      * number 5 will exist only in results where there are siblings 1 - 4.
221      *
222      * @param nthSibling the sibling index
223      */

224     public void setNthSibling(int nthSibling)
225     {
226         this.nthSibling = nthSibling;
227     }
228 }
229
Popular Tags