KickJava   Java API By Example, From Geeks To Geeks.

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


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  * PersistenceElementCollection.java
26  *
27  * Created on March 6, 2000, 2:20 PM
28  */

29
30 package com.sun.jdo.api.persistence.model.jdo.impl;
31
32 import java.util.*;
33 import java.beans.PropertyVetoException JavaDoc;
34
35 import com.sun.jdo.api.persistence.model.ModelException;
36 import com.sun.jdo.api.persistence.model.ModelVetoException;
37 import com.sun.jdo.api.persistence.model.jdo.*;
38
39 /**
40  *
41  * @author raccah
42  * @version %I%
43  */

44 public class PersistenceElementCollection
45 {
46     /** Owner of the collection. */
47     private PersistenceElementImpl _owner;
48
49     /** Elements of the collection. */
50     private PersistenceElement[] _elements;
51
52     /** Array template for typed returns */
53     private Object JavaDoc[] _template;
54
55     /** Property name. */
56     private String JavaDoc _propertyName;
57
58     /** Create new PersistenceElementCollection with no owner, property, or
59      * template. This constructor should only be used for cloning and
60      * archiving.
61      */

62     public PersistenceElementCollection ()
63     {
64         this(null, null, null);
65     }
66
67     /** Creates new PersistenceElementCollection */
68     public PersistenceElementCollection (PersistenceElementImpl owner,
69         String JavaDoc propertyName, Object JavaDoc[] template)
70     {
71         _owner = owner;
72         _propertyName = propertyName;
73         _template = template;
74     }
75
76     /** Change the set of elements.
77      * @param elements the new elements
78      * @param action {@link PersistenceElement.Impl#ADD},
79      * {@link PersistenceElement.Impl#REMOVE}, or
80      * {@link PersistenceElement.Impl#SET}
81      * @exception ModelException if impossible
82      */

83     public void changeElements (PersistenceElement[] elements, int action)
84         throws ModelException
85     {
86         changeElements(Arrays.asList(elements), action);
87     }
88
89     /** Change the set of elements.
90      * @param elements the new elements
91      * @param action {@link PersistenceElement.Impl#ADD},
92      * {@link PersistenceElement.Impl#REMOVE}, or
93      * {@link PersistenceElement.Impl#SET}
94      * @exception ModelException if impossible
95      */

96     public void changeElements (List elements, int action)
97         throws ModelException
98     {
99         boolean changed = false;
100
101         try
102         {
103             PersistenceElement[] oldElements = getElements();
104             int oldLength = (oldElements == null) ? 0 : oldElements.length;
105             int newLength = (elements == null) ? 0 : elements.size();
106             List list = null;
107
108             switch (action)
109             {
110                 case PersistenceElement.Impl.SET:
111                     list = elements;
112                     changed = true;
113                     break;
114                 case PersistenceElement.Impl.ADD:
115                     if (newLength > 0)
116                     {
117                         list = ((oldLength == 0) ? new ArrayList() :
118                             new ArrayList(Arrays.asList(oldElements)));
119                         list.addAll(elements);
120                         changed = true;
121                     }
122                     break;
123                 case PersistenceElement.Impl.REMOVE:
124                     if ((newLength > 0) && (oldLength > 0))
125                     {
126                         list = new ArrayList(Arrays.asList(oldElements));
127                         list.removeAll(elements);
128                         changed = true;
129                     }
130                     break;
131             }
132             if (changed)
133             {
134                 try
135                 {
136                     _owner.fireVetoableChange(_propertyName, null, null);
137                     _elements = (PersistenceElement[])list.toArray(_template);
138                 }
139                 catch (PropertyVetoException JavaDoc e)
140                 {
141                     throw new ModelVetoException(e);
142                 }
143             }
144         }
145         finally
146         {
147             if (changed)
148                 _owner.firePropertyChange(_propertyName, null, null);
149         }
150     }
151
152     /** Returns the collection of elements maintained by this holder in the form
153      * of an array.
154      * @return the elements maintained by this collection
155      */

156     public PersistenceElement[] getElements () { return _elements; }
157
158     /** Returns the element with the supplied name from the collection of
159      * elements maintained by this collection.
160      * @param name the name to match
161      * @return the element with the supplied name, <code>null</code> if none
162      * exists
163      */

164     public PersistenceElement getElement (String JavaDoc name)
165     {
166         PersistenceElement[] elements = getElements();
167         int i, count = ((elements != null) ? elements.length : 0);
168         
169         for (i = 0; i < count; i++)
170         {
171             PersistenceElement element = elements[i];
172
173             if (name.equals(element.getName()))
174                 return element;
175         }
176
177         return null;
178     }
179
180     //=============== extra methods needed for xml archiver ==============
181

182     /** Returns the owner of this collection. This method should only
183      * be used internally and for cloning and archiving.
184      * @return the owner of this collection
185      */

186     public PersistenceElementImpl getOwner () { return _owner; }
187
188     /** Set the owner of this collection to the supplied implementation.
189      * This method should only be used internally and for cloning and
190      * archiving.
191      * @param owner the owner of this collection
192      */

193     public void setOwner (PersistenceElementImpl owner)
194     {
195         _owner = owner;
196     }
197
198     /** Returns the template for the array of this collection. This method
199      * should only be used internally and for cloning and archiving.
200      * @return the typed template of this collection
201      */

202     public Object JavaDoc[] getTemplate () { return _template; }
203
204     /** Set the template for the array of this collection to the supplied
205      * array. This template is used so the array returned by getElements is
206      * properly typed. This method should only be used internally and
207      * for cloning and archiving.
208      * @param template the typed template of this collection
209      */

210     public void setTemplate (Object JavaDoc[] template) { _template = template; }
211
212     /** Returns the property name of this collection. This method
213      * should only be used internally and for cloning and archiving.
214      * @return the property name for this collection
215      */

216     public String JavaDoc getPropertyName () { return _propertyName; }
217
218     /** Set the property name of this collection to the supplied name.
219      * This name is used to generate the correct property change event on
220      * changes to the collection. This method should only be used
221      * internally and for cloning and archiving.
222      * @param propertyName the property name for this collection
223      */

224     public void setPropertyName (String JavaDoc propertyName)
225     {
226         _propertyName = propertyName;
227     }
228
229     /** Set the collection of elements maintained by this holder to the
230      * supplied array. This method should only be used internally and for
231      * cloning and archiving.
232      * @param elements the collection of elements maintained by this holder
233      */

234     public void setElements (PersistenceElement[] elements)
235     {
236         _elements = elements;
237     }
238 }
239
Popular Tags