KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > sco > ArrayList


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  * sco.ArrayList.java
26  */

27
28 package com.sun.jdo.spi.persistence.support.sqlstore.sco;
29
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33
34 import com.sun.jdo.api.persistence.support.JDOUserException;
35 import com.sun.jdo.api.persistence.support.JDOUnsupportedOptionException;
36 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceCapable;
37 import com.sun.jdo.spi.persistence.support.sqlstore.SCOCollection;
38 import com.sun.jdo.spi.persistence.support.sqlstore.StateManager;
39 import com.sun.jdo.spi.persistence.utility.I18NHelper;
40
41
42 /**
43  * A mutable 2nd class object date.
44  * @author Marina Vatkina
45  * @version 1.0
46  * @see java.util.ArrayList
47  */

48 public class ArrayList
49     extends java.util.ArrayList JavaDoc
50     implements SCOCollection
51 {
52
53     private transient PersistenceCapable owner;
54
55     private transient String JavaDoc fieldName;
56
57     private transient Class JavaDoc elementType;
58  
59     private transient boolean allowNulls;
60
61     private transient java.util.Vector JavaDoc added = new java.util.Vector JavaDoc();
62
63     private transient java.util.Vector JavaDoc removed = new java.util.Vector JavaDoc();
64
65     /**
66      * I18N message handlers
67      */

68     private final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
69                              "com.sun.jdo.spi.persistence.support.sqlstore.impl.Bundle", // NOI18N
70
ArrayList.class.getClassLoader());
71
72     private final static ResourceBundle JavaDoc messages1 = I18NHelper.loadBundle(
73                              "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", // NOI18N
74
ArrayList.class.getClassLoader());
75
76
77     /**
78      * Constructs an empty vector so that its internal data array
79      * has size <tt>10</tt> and its standard capacity increment is
80      * zero. Assigns owning object and field name
81      * @param owner the owning object
82      * @param fieldName the owning field name
83      * @param elementType the element types allowed
84      * @param allowNulls true if nulls are allowed
85      */

86     public ArrayList(Object JavaDoc owner, String JavaDoc fieldName, Class JavaDoc elementType, boolean allowNulls)
87     {
88     super();
89     if (owner instanceof PersistenceCapable)
90         {
91         this.owner = (PersistenceCapable)owner;
92         this.fieldName = fieldName;
93         }
94     this.elementType = elementType;
95     this.allowNulls = allowNulls;
96     }
97
98     /**
99      * Constructs an empty vector with the specified initial capacity and
100      * with its capacity increment equal to zero. Assigns owning object and field name
101      *
102      * @param owner the owning object
103      * @param fieldName the owning field name
104      * @param elementType the element types allowed
105      * @param allowNulls true if nulls are allowed
106      * @param initialCapacity the initial capacity of the vector.
107      * @exception IllegalArgumentException if the specified initial capacity
108      * is negative
109      */

110     public ArrayList(Object JavaDoc owner, String JavaDoc fieldName,
111             Class JavaDoc elementType, boolean allowNulls, int initialCapacity)
112     {
113     super(initialCapacity);
114     if (owner instanceof PersistenceCapable)
115         {
116         this.owner = (PersistenceCapable)owner;
117         this.fieldName = fieldName;
118         }
119     this.elementType = elementType;
120     this.allowNulls = allowNulls;
121     }
122
123     /** ------------------Public Methods----------------*/
124
125     /**
126      * Replaces the element at the specified position in this ArrayList with the
127      * specified element.
128      *
129      * @param index index of element to replace.
130      * @param element element to be stored at the specified position.
131      * @return the element previously at the specified position.
132      * @exception IndexOutOfBoundsException index out of range
133      * (index &lt; 0 || index &gt;= size()).
134      * @exception IllegalArgumentException fromIndex &gt; toIndex.
135      * @see java.util.ArrayList
136      */

137     public Object JavaDoc set(int index, Object JavaDoc element) {
138
139     throwUnsupportedOption();
140
141         if (element == null)
142         {
143                 if (allowNulls == false)
144                 {
145                         throw new JDOUserException(I18NHelper.getMessage(messages,
146                                 "sco.nulls_not_allowed")); // NOI18N
147
}
148                 // It is actualy remove
149
return this.remove(index);
150         }
151
152         if (elementType == null || elementType.isAssignableFrom(element.getClass()))
153         {
154         // Mark the field as dirty
155
StateManager stateManager = this.makeDirty();
156         
157         Object JavaDoc o = super.set(index, element);
158
159                 if (added.remove(o) == false)
160                         removed.add(o);
161
162                 if (removed.remove(element) == false)
163                         added.add(element);
164
165         // Apply updates
166
this.applyUpdates(stateManager, true);
167         
168             return o;
169     } else {
170         throw new JDOUserException(I18NHelper.getMessage(messages,
171                                 "sco.classcastexception", elementType.getName()), // NOI18N
172
new ClassCastException JavaDoc(), new Object JavaDoc[] {element});
173     }
174
175     }
176
177
178     /**
179      * Appends the specified element to the end of this ArrayList.
180      *
181      * @param o element to be appended to this ArrayList.
182      * @return true (as per the general contract of Collection.add).
183      * @see java.util.ArrayList
184      */

185     public boolean add(Object JavaDoc o) {
186     if (allowNulls == false && o == null)
187         {
188                 throw new JDOUserException(I18NHelper.getMessage(messages,
189                         "sco.nulls_not_allowed")); // NOI18N
190
}
191
192     if (elementType == null || elementType.isAssignableFrom(o.getClass()))
193         {
194         // Mark the field as dirty
195
StateManager stateManager = this.makeDirty();
196         
197                 if (removed.remove(o) == false)
198             added.add(o);
199
200             boolean modified = super.add(o);
201
202         // Apply updates
203
this.applyUpdates(stateManager, modified);
204
205         return modified;
206
207     } else {
208                 throw new JDOUserException(I18NHelper.getMessage(messages,
209                                 "sco.classcastexception", elementType.getName()), // NOI18N
210
new ClassCastException JavaDoc(), new Object JavaDoc[] {o});
211     }
212     }
213
214     /**
215      * Removes the first occurrence of the specified element in this ArrayList
216      * If the ArrayList does not contain the element, it is unchanged.
217      *
218      * @param o element to be removed from this ArrayList, if present.
219      * @return true if the ArrayList contained the specified element.
220      * @see java.util.ArrayList
221      */

222     public boolean remove(Object JavaDoc o) {
223
224     // Because java.util.AbstractCollection.remove(Object) delegates remove() to remove(int)
225
// which is not supported, we cannot rely on jdk. We need to process remove here.
226

227         // Mark the field as dirty
228
StateManager stateManager = this.makeDirty();
229
230     int i = super.indexOf(o);
231         Object JavaDoc obj = null;
232     if (i > -1) {
233         obj = super.remove(i);
234
235             if (added.remove(obj) == false)
236                     removed.add(obj);
237
238             // Apply updates
239
this.applyUpdates(stateManager, true);
240         return true;
241     }
242     return false;
243     }
244
245     /**
246      * Inserts the specified element at the specified position in this ArrayList.
247      *
248      * @param index index at which the specified element is to be inserted.
249      * @param element element to be inserted.
250      * @exception IndexOutOfBoundsException index is out of range
251      * (index &lt; 0 || index &gt; size()).
252      * @see java.util.ArrayList
253      */

254     public void add(int index, Object JavaDoc element) {
255     if (allowNulls == false && element == null)
256         {
257                 throw new JDOUserException(I18NHelper.getMessage(messages,
258                         "sco.nulls_not_allowed")); // NOI18N
259
}
260
261         if (elementType == null || elementType.isAssignableFrom(element.getClass()))
262         {
263         // Mark the field as dirty
264
StateManager stateManager = this.makeDirty();
265         
266                 super.add(index, element);
267                 if (removed.remove(element) == false)
268                         added.add(element);
269
270         // Apply updates
271
this.applyUpdates(stateManager, true);
272     
273         } else {
274                 throw new JDOUserException(I18NHelper.getMessage(messages,
275                                 "sco.classcastexception", elementType.getName()), // NOI18N
276
new ClassCastException JavaDoc(), new Object JavaDoc[] {element});
277         }
278
279     }
280
281     /**
282      * Removes the element at the specified position in this ArrayList.
283      * shifts any subsequent elements to the left (subtracts one from their
284      * indices). Returns the element that was removed from the ArrayList.
285      *
286      * @param index the index of the element to removed.
287      * @exception IndexOutOfBoundsException index out of range (index
288      * &lt; 0 || index &gt;= size()).
289      * @see java.util.ArrayList
290      */

291     public Object JavaDoc remove(int index) {
292
293     throwUnsupportedOption();
294
295     // Mark the field as dirty
296
StateManager stateManager = this.makeDirty();
297         
298         Object JavaDoc obj = super.remove(index);
299
300         if (added.remove(obj) == false)
301         removed.add(obj);
302
303     // Apply updates
304
this.applyUpdates(stateManager, true);
305
306         return obj;
307     }
308
309     /**
310      * Removes all of the elements from this ArrayList. The ArrayList will
311      * be empty after this call returns (unless it throws an exception).
312      *
313      * @see java.util.ArrayList
314      */

315     public void clear() {
316     // Mark the field as dirty
317
StateManager stateManager = this.makeDirty();
318         
319         for (Iterator JavaDoc iter = super.iterator(); iter.hasNext();) {
320                 Object JavaDoc o = iter.next();
321                 if (added.remove(o) == false)
322                         removed.add(o);
323         }
324         added.clear();
325         super.clear();
326
327     // Apply updates
328
this.applyUpdates(stateManager, true);
329     }
330
331     /**
332      * Appends all of the elements in the specified Collection to the end of
333      * this ArrayList, in the order that they are returned by the specified
334      * Collection's Iterator.
335      *
336      * @param c elements to be inserted into this ArrayList.
337      * @exception IndexOutOfBoundsException index out of range (index
338      * &lt; 0 || index &gt; size()).
339      * @see java.util.ArrayList
340      */

341     public boolean addAll(Collection JavaDoc c) {
342     if (allowNulls == false && c.contains(null))
343         {
344                 throw new JDOUserException(I18NHelper.getMessage(messages,
345                         "sco.nulls_not_allowed")); // NOI18N
346
}
347
348     java.util.Vector JavaDoc errc = new java.util.Vector JavaDoc();
349     if (elementType != null)
350     {
351         // iterate the collection and make a list of wrong elements.
352
Iterator JavaDoc i = c.iterator();
353         while (i.hasNext())
354         {
355             Object JavaDoc o = i.next();
356             if (!elementType.isAssignableFrom(o.getClass()))
357                 errc.add(o);
358         }
359     }
360     if (errc != null && errc.size() > 0)
361     {
362         throw new JDOUserException(I18NHelper.getMessage(messages,
363                                 "sco.classcastexception", elementType.getName()), // NOI18N
364
new ClassCastException JavaDoc(), errc.toArray());
365     }
366
367     // Mark the field as dirty
368
StateManager stateManager = this.makeDirty();
369         
370         removed.removeAll(c);
371     added.addAll(c);
372
373     boolean modified = super.addAll(c);
374
375     // Apply updates
376
this.applyUpdates(stateManager, modified);
377
378     return modified;
379     }
380
381     /**
382      * Removes from this ArrayList all of its elements that are contained in the
383      * specified Collection.
384      *
385      * @return true if this ArrayList changed as a result of the call.
386      * @see java.util.ArrayList
387      */

388     public boolean removeAll(Collection JavaDoc c) {
389     boolean modified = false;
390     // Mark the field as dirty
391
StateManager stateManager = this.makeDirty();
392         
393         Iterator JavaDoc e = c.iterator();
394         while (e.hasNext()) {
395         Object JavaDoc o = e.next();
396                 if(super.contains(o)) {
397                     removeInternal(o);
398             if (added.remove(o) == false)
399                         removed.add(o);
400                     modified = true;
401                 }
402         }
403
404     // Apply updates
405
this.applyUpdates(stateManager, modified);
406
407         return modified;
408     }
409
410     /**
411      * Inserts all of the elements in in the specified Collection into this
412      * ArrayList at the specified position. Shifts the element currently at
413      * that position (if any) and any subsequent elements to the right
414      * (increases their indices). The new elements will appear in the ArrayList
415      * in the order that they are returned by the specified Collection's
416      * iterator.
417      *
418      * @param index index at which to insert first element
419      * from the specified collection.
420      * @param c elements to be inserted into this ArrayList.
421      * @exception IndexOutOfBoundsException index out of range (index
422      * &lt; 0 || index &gt; size()).
423      * @see java.util.ArrayList
424      */

425     public boolean addAll(int index, Collection JavaDoc c) {
426     if (allowNulls == false && c.contains(null))
427         {
428                 throw new JDOUserException(I18NHelper.getMessage(messages,
429                         "sco.nulls_not_allowed")); // NOI18N
430
}
431
432     java.util.Vector JavaDoc errc = new java.util.Vector JavaDoc();
433         if (elementType != null)
434         {
435                 // iterate the collection and make a list of wrong elements.
436
Iterator JavaDoc i = c.iterator();
437                 while (i.hasNext())
438                 {
439                         Object JavaDoc o = i.next();
440                         if (!elementType.isAssignableFrom(o.getClass()))
441                                 errc.add(o);
442                 }
443         }
444         if (errc != null && errc.size() > 0)
445         {
446                 throw new JDOUserException(I18NHelper.getMessage(messages,
447                                 "sco.classcastexception", elementType.getName()), // NOI18N
448
new ClassCastException JavaDoc(), errc.toArray());
449         }
450
451     // Mark the field as dirty
452
StateManager stateManager = this.makeDirty();
453         
454         removed.removeAll(c);
455         added.addAll(c);
456
457         boolean modified = super.addAll(index, c);
458
459     // Apply updates
460
this.applyUpdates(stateManager, modified);
461
462         return modified;
463     }
464
465     /**
466      * Retains only the elements in this ArrayList that are contained in the
467      * specified Collection.
468      *
469      * @return true if this ArrayList changed as a result of the call.
470      * @see java.util.ArrayList
471      */

472     public boolean retainAll(Collection JavaDoc c)
473     {
474     boolean modified = false;
475     java.util.Vector JavaDoc v = new java.util.Vector JavaDoc();
476
477     // Mark the field as dirty
478
StateManager stateManager = this.makeDirty();
479         
480         for (Iterator JavaDoc iter = super.iterator(); iter.hasNext();)
481         {
482                 Object JavaDoc o = iter.next();
483                 if (!c.contains(o))
484                 {
485             v.add(o);
486                     if (added.remove(o) == false)
487                 removed.add(o);
488
489             modified = true;
490         }
491         }
492
493     // Now remove the rest (stored in "v")
494
for (Iterator JavaDoc iter = v.iterator(); iter.hasNext();)
495         {
496             removeInternal(iter.next());
497     }
498
499     // Apply updates
500
this.applyUpdates(stateManager, modified);
501
502         return modified;
503     }
504
505     /**
506      * Creates and returns a copy of this object.
507      *
508      * <P>Mutable Second Class Objects are required to provide a public
509      * clone method in order to allow for copying PersistenceCapable
510      * objects. In contrast to Object.clone(), this method must not throw a
511      * CloneNotSupportedException.
512      */

513     public Object JavaDoc clone()
514     {
515         ArrayList obj = (ArrayList)super.clone();
516     obj.unsetOwner();
517
518         return obj;
519     }
520
521     /**
522      * Creates and returns a copy of this object without resetting the owner and field value.
523      *
524      */

525     public Object JavaDoc cloneInternal()
526     {
527         return super.clone();
528     }
529
530     /**
531      * Cleans removed and added lists
532      */

533     public void reset()
534     {
535         added.clear();
536         removed.clear();
537     }
538
539     public void markDeferred()
540     {
541     }
542
543     public boolean isDeferred()
544     {
545         return false;
546     }
547
548     public void applyDeferredUpdates(Collection JavaDoc c)
549     {
550         super.addAll(c);
551     }
552
553     /**
554      * Adds an object to the list without recording changes
555      */

556     public void addInternal(Object JavaDoc o)
557     {
558         super.add(o);
559     }
560
561     /**
562      * Adds a Collection to the list without recording changes
563      */

564     public void addAllInternal(Collection JavaDoc c)
565     {
566         super.addAll(c);
567     }
568
569     /**
570      * @inheritDoc
571      */

572     public void addToBaseCollection(Object JavaDoc o)
573     {
574         super.add(o);
575     }
576
577     /**
578      * Removes from this collection without recording changes
579      */

580     public void removeAllInternal(Collection JavaDoc c)
581     {
582         super.removeAll(c);
583     }
584  
585     /**
586      * Returns added collection
587      *
588      * @return added collection of added elements
589      */

590     public Collection JavaDoc getAdded()
591     {
592         return (Collection JavaDoc)added;
593     }
594
595     /**
596      * Returns removed collection
597      *
598      * @return removed collection of removed elements
599      */

600     public Collection JavaDoc getRemoved()
601     {
602         return (Collection JavaDoc)removed;
603     }
604
605
606     /**
607      * Clears Collection without notifing the owner
608      */

609     public void clearInternal()
610     {
611         super.clear();
612         this.reset();
613     }
614
615     /**
616      * Removes an element without notifing the owner
617      */

618     public void removeInternal(Object JavaDoc o)
619     {
620     int i = super.indexOf(o);
621         super.remove(i);
622     }
623
624     /**
625      * Nullifies references to the owner Object and Field
626      */

627     public void unsetOwner()
628     {
629     this.owner = null;
630     this.fieldName = null;
631     this.elementType = null;
632         added.clear();
633         removed.clear();
634     }
635
636     /**
637      * Returns the owner object of the SCO instance
638      *
639      * @return owner object
640      */

641     public Object JavaDoc getOwner()
642     {
643     return this.owner;
644     }
645
646     /**
647      * Returns the field name
648      *
649      * @return field name as java.lang.String
650      */

651     public String JavaDoc getFieldName()
652     {
653     return this.fieldName;
654     }
655
656     /**
657      * Marks object dirty
658      */

659     public StateManager makeDirty()
660     {
661     if (owner != null)
662         {
663                 StateManager stateManager = owner.jdoGetStateManager();
664                 if (stateManager != null)
665                 {
666                         stateManager.makeDirty(fieldName);
667                 }
668                   
669                 return stateManager;
670     }
671         return null;
672      }
673     /**
674      * Apply changes (can be a no-op)
675      */

676     public void applyUpdates(StateManager sm, boolean modified)
677     {
678
679         if (modified && sm != null)
680         {
681                 sm.applyUpdates(fieldName, this);
682         }
683     }
684
685     /**
686      * Throw JDOUnsupportedOptionException
687      */

688     private void throwUnsupportedOption()
689     {
690     // Index operation that changes the underline collection
691
// is not supported in 2_beta. Bug 4370474
692
throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
693                                 "sco.not_supported")); //NOI18N
694
}
695     
696     /**
697      * Set the owner if this instance is not owned.
698      * @see SCOCollection#setOwner
699      * @param owner the new owner.
700      * @param fieldName the new field name.
701      * @param elementType the new element type as Class, or null if type
702      * is not to be checked.
703      */

704     public void setOwner(Object JavaDoc owner, String JavaDoc fieldName, Class JavaDoc elementType) {
705
706         if (this.owner != null) {
707             throw new JDOUserException(I18NHelper.getMessage(
708                 messages1, "core.statemanager.anotherowner"), // NOI18N
709
new Object JavaDoc[]{this.owner, this.fieldName});
710         }
711         if (owner instanceof PersistenceCapable) {
712                 this.owner = (PersistenceCapable)owner;
713                 this.fieldName = fieldName;
714                 this.elementType = elementType;
715         }
716     }
717 }
718
Popular Tags