KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > AbstractReference


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam;
21
22 /**
23  * Represents reference to a component. On writing, this indirection help serialize
24  * the referenced component as an attribute string value. On reading, the referenced
25  * can be resolved on demand.
26  * <p>
27  * Note: Client code should always check for brokeness before access the referenced.
28  *
29  * @author rico
30  * @author Nam Nguyen
31  * @author Chris Webster
32  */

33 public abstract class AbstractReference<T extends Referenceable> implements Reference<T> {
34
35     private T referenced;
36     private Class JavaDoc<T> classType;
37     private AbstractComponent parent;
38     protected String JavaDoc refString;
39     
40     /**
41      * Constructor for writing.
42      * @param referenced the component being referenced
43      * @param referencedType type of the referenced component
44      * @param parent referencing component on which the referenced is serialized
45      * as an attribute string value.
46      */

47     public AbstractReference(T referenced, Class JavaDoc<T> referencedType, AbstractComponent parent) {
48         if (referenced == null) {
49             throw new IllegalArgumentException JavaDoc("Referenced component null"); //NOI18N
50
}
51         checkParentAndType(parent, referencedType);
52         this.referenced = referenced;
53         this.classType = referencedType;
54         this.parent = parent;
55     }
56     
57     /**
58      * Constructor for reading.
59      * @param referencedType type of the referenced component
60      * @param parent referencing component on which the referenced is serialized
61      * as an attribute string value.
62      * @param ref the string value used in resolving.
63      */

64     public AbstractReference(Class JavaDoc<T> referencedType, AbstractComponent parent, String JavaDoc ref){
65         checkParentAndType(parent, referencedType);
66         this.refString = ref;
67         this.classType = referencedType;
68         this.parent = parent;
69     }
70
71     /**
72      * Access method for referenced.
73      */

74     protected T getReferenced() {
75         return referenced;
76     }
77     /**
78      * Accessor method for referenced.
79      */

80     protected void setReferenced(T referenced) {
81         this.referenced = referenced;
82     }
83
84     /**
85      * Returns type of the referenced component
86      */

87     public Class JavaDoc<T> getType() {
88         return classType;
89     }
90     
91     /**
92      * Returns true if the reference cannot be resolved in the current document
93      */

94     public boolean isBroken() {
95         try {
96             return get() == null;
97         } catch(IllegalStateException JavaDoc ise) {
98             referenced = null;
99             return false;
100         }
101     }
102     
103     /**
104      * Returns true if this reference refers to the target component.
105      */

106     public boolean references(T target) {
107         return get() == target;
108     }
109     
110     /**
111      * @return string to use in persiting the reference as attribute value of
112      * the containing component
113      */

114     public String JavaDoc getRefString() {
115         return refString;
116     }
117
118     protected AbstractComponent getParent() {
119         return parent;
120     }
121     
122     private void checkParentAndType(AbstractComponent parent, Class JavaDoc<T> classType) {
123         if (parent == null) {
124             throw new IllegalArgumentException JavaDoc("parent == null"); //NOI18N
125
}
126         if (classType == null) {
127             throw new IllegalArgumentException JavaDoc("classType == null"); //NOI18N
128
}
129     }
130     
131     @Override JavaDoc
132     public boolean equals(Object JavaDoc obj) {
133         if (obj == null) return false;
134         if (obj instanceof AbstractReference) {
135             AbstractReference<T> that = (AbstractReference<T>) obj;
136             return refString.equals(that.getRefString()) &&
137                    parent.equals(that.parent) &&
138                    getType().equals(that.getType());
139         } else {
140             return super.equals(obj);
141         }
142     }
143     
144     @Override JavaDoc
145     public int hashCode() {
146         return parent.hashCode();
147     }
148
149 }
150
Popular Tags