KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > model > jdo > PersistenceElement


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * PersistenceElement.java
26  *
27  * Created on February 28, 2000, 3:37 PM
28  */

29
30 package com.sun.jdo.api.persistence.model.jdo;
31
32 import java.util.ResourceBundle JavaDoc;
33 import java.beans.PropertyChangeListener JavaDoc;
34 import java.beans.VetoableChangeListener JavaDoc;
35 import java.text.Collator JavaDoc;
36
37 import com.sun.jdo.api.persistence.model.ModelException;
38 import com.sun.jdo.spi.persistence.utility.I18NHelper;
39
40 /**
41  *
42  * @author raccah
43  * @version %I%
44  */

45 public abstract class PersistenceElement extends Object JavaDoc
46     implements PersistenceElementProperties, Comparable JavaDoc
47 {
48     /** I18N message handler */
49     private static final ResourceBundle JavaDoc _messages = I18NHelper.loadBundle(
50         "com.sun.jdo.api.persistence.model.Bundle", // NOI18N
51
PersistenceElement.class.getClassLoader());
52
53     /** Implementation */
54     Impl _impl;
55
56     /** Create new PersistenceElement with no implementation.
57      * This constructor should only be used for cloning and archiving.
58      */

59     public PersistenceElement ()
60     {
61         this(null);
62     }
63
64     /** Create new PersistenceElement with the provided implementation. The
65      * implementation is responsible for storing all properties of the object.
66      * @param impl the implementation to use
67      */

68     protected PersistenceElement (PersistenceElement.Impl impl)
69     {
70         setImpl(impl);
71     }
72
73     /** @return implemetation factory for this element
74      */

75     public final Impl getImpl () { return _impl; }
76
77     /** @return I18N message handler for this element
78      */

79     protected static final ResourceBundle JavaDoc getMessages () { return _messages; }
80
81     /** Add a property change listener.
82      * @param l the listener to add
83      * @see PersistenceElementProperties
84      */

85     public final void addPropertyChangeListener (PropertyChangeListener JavaDoc l)
86     {
87         getImpl().addPropertyChangeListener(l);
88     }
89
90     /** Remove a property change listener.
91      * @param l the listener to remove
92      * @see PersistenceElementProperties
93      */

94     public final void removePropertyChangeListener (PropertyChangeListener JavaDoc l)
95     {
96         getImpl().removePropertyChangeListener(l);
97     }
98
99     /** Add a vetoable change listener.
100      * @param l the listener to add
101      * @see PersistenceElementProperties
102      */

103     public final void addVetoableChangeListener (VetoableChangeListener JavaDoc l)
104     {
105         getImpl().addVetoableChangeListener(l);
106     }
107
108     /** Remove a vetoable change listener.
109      * @param l the listener to remove
110      * @see PersistenceElementProperties
111      */

112     public final void removeVetoableChangeListener (VetoableChangeListener JavaDoc l)
113     {
114         getImpl().removeVetoableChangeListener(l);
115     }
116
117     /** Get the name of this persistence element.
118      * @return the name
119      */

120     public String JavaDoc getName() { return getImpl().getName(); }
121
122     /** Set the name of this persistence element.
123      * @param name the name
124      * @exception ModelException if impossible
125      */

126     public void setName (String JavaDoc name) throws ModelException
127     {
128         getImpl().setName(name);
129     }
130     
131     /** Overrides Object's <code>toString</code> method to return the name
132      * of this persistence element.
133      * @return a string representation of the object
134      */

135     public String JavaDoc toString () { return getName(); }
136
137     /** Overrides Object's <code>equals</code> method by comparing the name of this persistence element
138      * with the name of the argument obj. The method returns <code>false</code> if obj does not have
139      * the same dynamic type as this persistence element.
140      * @return <code>true</code> if this object is the same as the obj argument; <code>false</code> otherwise.
141      * @param obj the reference object with which to compare.
142      */

143     public boolean equals(Object JavaDoc obj)
144     {
145         if (obj == null)
146             return false;
147         if (obj == this)
148             return true;
149
150         // check for the right class and then do the name check by calling compareTo.
151
return (getClass() == obj.getClass()) && (compareTo(obj) == 0);
152     }
153     
154     /** Overrides Object's <code>hashCode</code> method to return the hashCode of this persistence element's name.
155      * @return a hash code value for this object.
156      */

157     public int hashCode()
158     {
159         return (getName()==null) ? 0 : getName().hashCode();
160     }
161
162     //================= implementation of Comparable ================
163

164     /** Compares this object with the specified object for order. Returns a negative integer, zero,
165      * or a positive integer as this object is less than, equal to, or greater than the specified object.
166      * The specified object must be persistence element, meaning it must be an instance of class
167      * PersistenceElement or any subclass. If not a ClassCastException is thrown.
168      * The order of PersistenceElement objects is defined by the order of their names.
169      * Persistence elements without name are considered to be less than any named persistence element.
170      * @param o the Object to be compared.
171      * @return a negative integer, zero, or a positive integer as this object is less than, equal to,
172      * or greater than the specified object.
173      * @exception ClassCastException - if the specified object is null or is not an instance of PersistenceElement
174      */

175     public int compareTo(Object JavaDoc o)
176     {
177         // null is not allowed
178
if (o == null)
179             throw new ClassCastException JavaDoc();
180         if (o == this)
181             return 0;
182
183         String JavaDoc thisName = getName();
184         // the following statement throws a ClassCastException if o is not a PersistenceElement
185
String JavaDoc otherName = ((PersistenceElement)o).getName();
186         // if this does not have a name it should compare less than any named object
187
if (thisName == null)
188             return (otherName == null) ? 0 : -1;
189         // if this is named and o does not have a name it should compare greater
190
if (otherName == null)
191             return 1;
192         // now we know that this and o are named persistence elements =>
193
// use locale-sensitive String comparison
194
int ret = Collator.getInstance().compare(thisName, otherName);
195         // if both names are equal, both objects might have different types.
196
// If so order both objects by their type names (necessary to be consistent with equals)
197
if ((ret == 0) && (getClass() != o.getClass()))
198             ret = getClass().getName().compareTo(o.getClass().getName());
199         return ret;
200     }
201     
202     //=============== extra set methods needed for xml archiver ==============
203

204     /** Set the implementation factory of this persistence element.
205      * This method should only be used internally and for cloning
206      * and archiving.
207      * @param impl the implementation to use
208      */

209     public void setImpl (PersistenceElement.Impl impl)
210     {
211         _impl = impl;
212
213         if (_impl != null)
214             getImpl().attachToElement(this);
215     }
216
217     /** Pluggable implementation of the storage of element properties.
218      * @see PersistenceElement#PersistenceElement
219      */

220     public interface Impl
221     {
222         /** Add some items. */
223         public static final int ADD = 1;
224         /** Remove some items. */
225         public static final int REMOVE = -1;
226         /** Set some items, replacing the old ones. */
227         public static final int SET = 0;
228         
229         /** Called to attach the implementation to a specific
230          * element. Will be called in the element's constructor.
231          * Allows implementors of this interface to store a reference to the
232          * holder class, useful for implementing the property change listeners.
233          *
234          * @param element the element to attach to
235          */

236         public void attachToElement (PersistenceElement element);
237
238         /** Add a property change listener.
239          * @param l the listener to add
240          */

241         public void addPropertyChangeListener (PropertyChangeListener JavaDoc l);
242
243         /** Remove a property change listener.
244          * @param l the listener to remove
245          */

246         public void removePropertyChangeListener (PropertyChangeListener JavaDoc l);
247
248         /** Add a vetoable change listener.
249          * @param l the listener to add
250          */

251         public void addVetoableChangeListener (VetoableChangeListener JavaDoc l);
252
253         /** Remove a vetoable change listener.
254          * @param l the listener to remove
255          */

256         public void removeVetoableChangeListener (VetoableChangeListener JavaDoc l);
257
258         /** Get the name of this persistence element.
259          * @return the name
260          */

261         public String JavaDoc getName ();
262
263         /** Set the name of this persistence element.
264          * @param name the name
265          * @exception ModelException if impossible
266          */

267         public void setName (String JavaDoc name) throws ModelException;
268     }
269 }
270
Popular Tags