KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > model > mapping > impl > MappingElementImpl


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  * MappingElementImpl.java
26  *
27  * Created on March 24, 2000, 10:06 AM
28  */

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

46 public abstract class MappingElementImpl implements MappingElement
47 {
48     /** I18N message handler */
49     private static final ResourceBundle JavaDoc _messages = I18NHelper.loadBundle(
50         "com.sun.jdo.api.persistence.model.Bundle", // NOI18N
51
MappingElementImpl.class.getClassLoader());
52
53     /** Property change support */
54     private PropertyChangeSupport _support;
55
56     /** Vetoable change support */
57     private transient VetoableChangeSupport _vetoableSupport;
58
59     String JavaDoc _name;
60
61     /** Create new MappingElementImpl with no corresponding name. This
62      * constructor should only be used for cloning and archiving.
63      */

64     public MappingElementImpl ()
65     {
66         this(null);
67     }
68
69     /** Creates new MappingElementImpl with the corresponding name
70      * @param name the name of the element
71      */

72     public MappingElementImpl (String JavaDoc name)
73     {
74         super();
75         _name = name;
76     }
77
78     /** @return I18N message handler for this element
79      */

80     protected static final ResourceBundle JavaDoc getMessages () { return _messages; }
81
82     /** Overrides Object's <code>toString</code> method to return the name
83      * of this mapping element.
84      * @return a string representation of the object
85      */

86     public String JavaDoc toString () { return getName(); }
87
88     /** Overrides Object's <code>equals</code> method by comparing the name of this mapping element
89      * with the name of the argument obj. The method returns <code>false</code> if obj does not have
90      * the same dynamic type as this mapping element.
91      * @return <code>true</code> if this object is the same as the obj argument; <code>false</code> otherwise.
92      * @param obj the reference object with which to compare.
93      */

94     public boolean equals(Object JavaDoc obj)
95     {
96         if (obj == null)
97             return false;
98         if (obj == this)
99             return true;
100
101         // check for the right class and then do the name check by calling compareTo.
102
return (getClass() == obj.getClass()) && (compareTo(obj) == 0);
103     }
104     
105     /** Overrides Object's <code>hashCode</code> method to return the hashCode of this mapping element's name.
106      * @return a hash code value for this object.
107      */

108     public int hashCode()
109     {
110         return (getName()==null) ? 0 : getName().hashCode();
111     }
112
113     /** Fires property change event.
114      * @param name property name
115      * @param o old value
116      * @param n new value
117      */

118     protected void firePropertyChange (String JavaDoc name, Object JavaDoc o, Object JavaDoc n)
119     {
120         if (_support != null)
121             _support.firePropertyChange(name, o, n);
122     }
123
124     /** Fires vetoable change event.
125      * @param name property name
126      * @param o old value
127      * @param n new value
128      * @exception PropertyVetoException when the change is vetoed by a listener
129      */

130     protected void fireVetoableChange (String JavaDoc name, Object JavaDoc o, Object JavaDoc n)
131         throws PropertyVetoException
132     {
133         if (_vetoableSupport != null)
134             _vetoableSupport.fireVetoableChange(name, o, n);
135     }
136
137     //================= implementation of MappingElement ================
138

139     /** Add a property change listener.
140      * @param l the listener to add
141      */

142     public synchronized void addPropertyChangeListener
143         (PropertyChangeListener l)
144     {
145         if (_support == null)
146         {
147             synchronized(this)
148             {
149                 // new test under synchronized block
150
if (_support == null)
151                     _support = new PropertyChangeSupport(this);
152             }
153         }
154
155         _support.addPropertyChangeListener(l);
156     }
157
158     /** Remove a property change listener.
159      * @param l the listener to remove
160      */

161     public void removePropertyChangeListener (PropertyChangeListener l)
162     {
163         if (_support != null)
164             _support.removePropertyChangeListener(l);
165     }
166
167     /** Add a vetoable change listener.
168      * @param l the listener to add
169      */

170     public synchronized void addVetoableChangeListener
171         (VetoableChangeListener l)
172     {
173         if (_vetoableSupport == null)
174             _vetoableSupport = new VetoableChangeSupport(this);
175
176         _vetoableSupport.addVetoableChangeListener(l);
177     }
178
179     /** Remove a vetoable change listener.
180      * @param l the listener to remove
181      */

182     public synchronized void removeVetoableChangeListener (
183         VetoableChangeListener l)
184     {
185         if (_vetoableSupport != null)
186             _vetoableSupport.removeVetoableChangeListener(l);
187     }
188
189     /** Get the name of this mapping element.
190      * @return the name
191      */

192     public String JavaDoc getName () { return _name; }
193
194     /** Set the name of this mapping element.
195      * @param name the name
196      * @exception ModelException if impossible
197      */

198     public void setName (String JavaDoc name) throws ModelException
199     {
200         String JavaDoc old = getName();
201         
202         try
203         {
204             fireVetoableChange(PROP_NAME, old, name);
205             _name = name;
206             firePropertyChange(PROP_NAME, old, name);
207         }
208         catch (PropertyVetoException e)
209         {
210             throw new ModelVetoException(e);
211         }
212     }
213
214     //================= implementation of Comparable ================
215

216     /** Compares this object with the specified object for order. Returns a negative integer, zero,
217      * or a positive integer as this object is less than, equal to, or greater than the specified object.
218      * The specified object must be mapping element, meaning it must be an instance of class
219      * MappingElementImpl or any subclass. If not a ClassCastException is thrown.
220      * The order of MappingElementImpl objects is defined by the order of their names.
221      * Mapping elements without name are considered to be less than any named mapping element.
222      * @param o the Object to be compared.
223      * @return a negative integer, zero, or a positive integer as this object is less than, equal to,
224      * or greater than the specified object.
225      * @exception ClassCastException - if the specified object is null or is not an instance of MappingElementImpl
226      */

227     public int compareTo(Object JavaDoc o)
228     {
229         // null is not allowed
230
if (o == null)
231             throw new ClassCastException JavaDoc();
232         if (o == this)
233             return 0;
234
235         String JavaDoc thisName = getName();
236         // the following statement throws a ClassCastException if o is not a MappingElementImpl
237
String JavaDoc otherName = ((MappingElementImpl)o).getName();
238         // if this does not have a name it should compare less than any named object
239
if (thisName == null)
240             return (otherName == null) ? 0 : -1;
241         // if this is named and o does not have a name it should compare greater
242
if (otherName == null)
243             return 1;
244         // now we know that this and o are named mapping elements =>
245
// use locale-sensitive String comparison
246
int ret = Collator.getInstance().compare(thisName, otherName);
247         // if both names are equal, both objects might have different types.
248
// If so order both objects by their type names (necessary to be consistent with equals)
249
if ((ret == 0) && (getClass() != o.getClass()))
250             ret = getClass().getName().compareTo(o.getClass().getName());
251         return ret;
252     }
253     
254 }
255
256
Popular Tags