KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > collection > CollectionImpl


1 package org.jacorb.collection;
2
3
4
5 /*
6
7  * JacORB collection service
8
9  *
10
11  * Copyright (C) 1999-2004 LogicLand group, Viacheslav Tararin.
12
13  *
14
15  * This library is free software; you can redistribute it and/or
16
17  * modify it under the terms of the GNU Library General Public
18
19  * License as published by the Free Software Foundation; either
20
21  * version 2 of the License, or (at your option) any later version.
22
23  *
24
25  * This library is distributed in the hope that it will be useful,
26
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30
31  * Library General Public License for more details.
32
33  *
34
35  * You should have received a copy of the GNU Library General Public
36
37  * License along with this library; if not, write to the Free
38
39  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
40
41  */

42
43
44
45 import org.omg.CosCollection.*;
46
47 import org.jacorb.collection.util.*;
48
49 import java.util.*;
50 import org.omg.PortableServer.POA JavaDoc;
51
52 import org.omg.PortableServer.Servant JavaDoc;
53
54 import org.omg.CORBA.Any JavaDoc;
55
56 import org.omg.CORBA.AnyHolder JavaDoc;
57
58
59
60 class CollectionImpl
61
62 {
63
64     protected DynArray iterators = new DynArray();
65
66     protected POA JavaDoc poa;
67
68     protected OperationsOperations ops;
69
70     protected IteratorFactory iterator_factory;
71
72     protected SortedVector data;
73
74     private Servant JavaDoc srvnt = null;
75
76
77
78 /* ========================================================================= */
79
80     CollectionImpl( OperationsOperations ops, POA JavaDoc poa, IteratorFactory iterator_factory ){
81
82         this.poa = poa;
83
84         this.ops = ops;
85
86         this.iterator_factory = iterator_factory;
87
88     };
89
90 /* ------------------------------------------------------------------------- */
91
92     public synchronized org.omg.CORBA.TypeCode JavaDoc element_type() {
93
94         return ops.element_type();
95
96     };
97
98 /* ------------------------------------------------------------------------- */
99
100     public synchronized boolean add_element(Any JavaDoc element) throws ElementInvalid {
101
102         element_add( element );
103
104         return true;
105
106     };
107
108 /* ------------------------------------------------------------------------- */
109
110     public synchronized boolean add_element_set_iterator(Any JavaDoc element, org.omg.CosCollection.Iterator where) throws IteratorInvalid,ElementInvalid {
111
112         PositionalIteratorImpl i = check_iterator( where );
113
114         int pos = element_add( element );
115
116         i.set_pos( pos );
117
118         i.set_in_between( false );
119
120         return true;
121
122     };
123
124 /* ------------------------------------------------------------------------- */
125
126     public synchronized void add_all_from(org.omg.CosCollection.Collection collector)
127
128     throws ElementInvalid
129
130     {
131
132         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
133
134     };
135
136 /* ------------------------------------------------------------------------- */
137
138     public synchronized void remove_element_at(org.omg.CosCollection.Iterator where)
139
140     throws IteratorInvalid,IteratorInBetween
141
142     {
143
144         PositionalIteratorImpl i = check_iterator( where );
145
146         if( i.is_in_between() ){
147
148             throw new IteratorInBetween();
149
150         }
151
152         int pos = i.get_pos();
153
154         try {
155
156             element_remove( pos );
157
158         } catch ( PositionInvalid e ){
159
160             i.invalidate();
161
162             throw new IteratorInvalid( IteratorInvalidReason.is_invalid );
163
164         } catch ( EmptyCollection e ){
165
166             i.invalidate();
167
168             throw new IteratorInvalid( IteratorInvalidReason.is_invalid );
169
170         }
171
172     };
173
174 /* ------------------------------------------------------------------------- */
175
176     public synchronized int remove_all() {
177
178         Enumeration enumeration = iterators.elements();
179
180         while( enumeration.hasMoreElements() ){
181
182             PositionalIteratorImpl i = (PositionalIteratorImpl)enumeration.nextElement();
183
184             i.invalidate();
185
186         }
187
188         int count = data.size();
189
190         data.removeAllElements();
191
192         return count;
193
194     };
195
196 /* ------------------------------------------------------------------------- */
197
198     public synchronized void replace_element_at(org.omg.CosCollection.Iterator where, Any JavaDoc element) throws IteratorInvalid,IteratorInBetween,ElementInvalid {
199
200         PositionalIteratorImpl i = check_iterator( where );
201
202         if( i.is_in_between() ){
203
204             throw new IteratorInBetween();
205
206         }
207
208         int pos = i.get_pos();
209
210         try {
211
212             element_replace( pos, element );
213
214         } catch ( PositionInvalid e ){
215
216             i.invalidate();
217
218             throw new IteratorInvalid( IteratorInvalidReason.is_invalid );
219
220         }
221
222     };
223
224 /* ------------------------------------------------------------------------- */
225
226     public synchronized boolean retrieve_element_at(org.omg.CosCollection.Iterator where, AnyHolder JavaDoc element) throws IteratorInvalid,IteratorInBetween {
227
228         PositionalIteratorImpl i = check_iterator( where );
229
230         if( i.is_in_between() ){
231
232             throw new IteratorInBetween();
233
234         }
235
236         int pos = i.get_pos();
237
238         try{
239
240             element.value = element_retrieve( pos );
241
242             return true;
243
244         } catch ( PositionInvalid e ){
245
246             i.invalidate();
247
248             throw new IteratorInvalid( IteratorInvalidReason.is_invalid );
249
250         }
251
252     };
253
254 /* ------------------------------------------------------------------------- */
255
256     public synchronized boolean all_elements_do(Command what) {
257
258         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
259
260     };
261
262 /* ------------------------------------------------------------------------- */
263
264     public synchronized int number_of_elements() {
265
266         return data.size();
267
268     };
269
270 /* ------------------------------------------------------------------------- */
271
272     public synchronized boolean is_empty() {
273
274         return data.size() == 0;
275
276     };
277
278 /* ------------------------------------------------------------------------- */
279
280     public synchronized void destroy() {
281
282         Enumeration enumeration = iterators.elements();
283
284         while( enumeration.hasMoreElements() ){
285
286             PositionalIteratorImpl i = (PositionalIteratorImpl)enumeration.nextElement();
287
288             i.destroy();
289
290         };
291
292         try {
293
294             byte[] ObjID = poa.servant_to_id( srvnt );
295
296             poa.deactivate_object( ObjID );
297
298         } catch ( Exception JavaDoc e ) {
299
300             System.out.println( "Internal error: Can not deactivate object" );
301
302             e.printStackTrace( System.out );
303
304             throw new org.omg.CORBA.INTERNAL JavaDoc();
305
306         }
307
308     };
309
310 /* ------------------------------------------------------------------------- */
311
312     public synchronized org.omg.CosCollection.Iterator create_iterator(boolean read_only) {
313
314         PositionalIteratorImpl iter = iterator_factory.create_iterator( this, read_only );
315
316         IteratorPOATie servant = new IteratorPOATie( iter );
317
318         try {
319
320             org.omg.CosCollection.Iterator i = IteratorHelper.narrow( poa.servant_to_reference( servant ));
321
322             iter.set_servant( servant );
323
324             return i;
325
326         } catch ( Exception JavaDoc e ){
327
328             e.printStackTrace( System.out );
329
330             throw new org.omg.CORBA.INTERNAL JavaDoc();
331
332         }
333
334     };
335
336 /* ========================================================================= */
337
338     public synchronized int element_add( Any JavaDoc element ) throws ElementInvalid {
339
340         check_element( element );
341
342         try {
343
344             int pos = data.addElement( element );
345
346             element_inserted( pos );
347
348             return pos;
349
350         } catch ( ObjectInvalid e ){
351
352             e.printStackTrace( System.out );
353
354             throw new org.omg.CORBA.INTERNAL JavaDoc();
355
356         }
357
358     };
359
360 /* ------------------------------------------------------------------------- */
361
362     public synchronized void element_remove( int pos ) throws PositionInvalid, EmptyCollection {
363
364         if( data.size() == 0 ){
365
366             throw new EmptyCollection();
367
368         }
369
370         if( pos < 0 || pos >= data.size() ){
371
372             throw new PositionInvalid();
373
374         }
375
376         Any JavaDoc old = (Any JavaDoc)data.elementAt( pos );
377
378         data.removeElementAt( pos );
379
380         element_removed( pos, old );
381
382     };
383
384 /* ------------------------------------------------------------------------- */
385
386     public synchronized int element_replace( int pos, Any JavaDoc element ) throws PositionInvalid, ElementInvalid {
387
388         if( pos < 0 || pos >= data.size() ){
389
390             throw new PositionInvalid();
391
392         }
393
394         check_element( element );
395
396         try {
397
398             Any JavaDoc old = (Any JavaDoc)data.elementAt( pos );
399
400             data.setElementAt( element, pos );
401
402             element_replaced( pos, old );
403
404         } catch ( ObjectInvalid e ) {
405
406             throw new ElementInvalid( ElementInvalidReason.positioning_property_invalid );
407
408         }
409
410         return pos;
411
412     };
413
414 /* ------------------------------------------------------------------------- */
415
416     public synchronized Any JavaDoc element_retrieve( int pos ) throws PositionInvalid {
417
418         if( pos < 0 || pos >= data.size() ){
419
420             throw new PositionInvalid();
421
422         }
423
424         return (Any JavaDoc)data.elementAt( pos );
425
426     };
427
428 /* ------------------------------------------------------------------------- */
429
430     public synchronized PositionalIteratorImpl check_iterator( org.omg.CosCollection.Iterator iter ) throws IteratorInvalid {
431
432         PositionalIteratorImpl i = null;
433
434         Enumeration enumeration=iterators.elements();
435
436         while( enumeration.hasMoreElements() ){
437
438             i=(PositionalIteratorImpl)enumeration.nextElement();
439
440             try {
441
442                 if( i.get_servant() == ( poa.reference_to_servant( iter ) ) ){
443
444                     return (PositionalIteratorImpl)i;
445
446                 }
447
448             } catch ( Exception JavaDoc e ){
449
450                 System.out.println( "Internal error: Invalid POA policy or POA internal error" );
451
452                 e.printStackTrace();
453
454                 throw new org.omg.CORBA.INTERNAL JavaDoc();
455
456             }
457
458         }
459
460         throw new IteratorInvalid( IteratorInvalidReason.is_not_for_collection );
461
462     };
463
464 /* ------------------------------------------------------------------------- */
465
466     public synchronized boolean is_this_you( org.omg.CosCollection.Collection col ) {
467
468         try {
469
470             return srvnt == poa.reference_to_servant( col );
471
472         } catch ( Exception JavaDoc e ){
473
474             System.out.println("InternalError: Can not test Object equality");
475
476             e.printStackTrace( System.out );
477
478             throw new org.omg.CORBA.INTERNAL JavaDoc();
479
480         }
481
482     };
483
484 /* ------------------------------------------------------------------------- */
485
486     public synchronized void destroy_me( PositionalIteratorImpl i ) {
487
488         if( iterators.removeElement( i ) ){
489
490             try {
491
492                 byte [] ObjID = poa.servant_to_id( i.get_servant() );
493
494                 poa.deactivate_object( ObjID );
495
496             } catch ( Exception JavaDoc e ){
497
498                 System.out.println("Internal error: Attempt destroy not my Iterator");
499
500                 e.printStackTrace( System.out );
501
502                 throw new org.omg.CORBA.INTERNAL JavaDoc();
503
504             }
505
506         } else {
507
508             System.out.println("Internal error: Attempt destroy not my Iterator");
509
510             throw new org.omg.CORBA.INTERNAL JavaDoc();
511
512         };
513
514     };
515
516 /* ------------------------------------------------------------------------- */
517
518     public void check_element( Any JavaDoc element ) throws ElementInvalid {
519
520         if( !ops.check_element_type(element) ){
521
522             throw new ElementInvalid( ElementInvalidReason.element_type_invalid );
523
524         }
525
526     };
527
528 /* ========================================================================= */
529
530     synchronized void set_servant( Servant JavaDoc srvnt ){
531
532         this.srvnt = srvnt;
533
534     };
535
536 /* ------------------------------------------------------------------------- */
537
538     protected void element_inserted( int pos ) {
539
540         Enumeration enumeration = iterators.elements();
541
542         while( enumeration.hasMoreElements() ){
543
544             PositionalIteratorImpl i = (PositionalIteratorImpl)enumeration.nextElement();
545
546             if( i.is_valid() ){
547
548                 int p = i.get_pos();
549
550                 if( p >= pos ) {
551
552                     i.set_pos( pos+1 );
553
554                 };
555
556             };
557
558         }
559
560     };
561
562 /* ------------------------------------------------------------------------- */
563
564     protected void element_removed( int pos, Any JavaDoc old ){
565
566         Enumeration enumeration = iterators.elements();
567
568         while( enumeration.hasMoreElements() ){
569
570             PositionalIteratorImpl i = (PositionalIteratorImpl)enumeration.nextElement();
571
572             if( i.is_valid() ){
573
574                 int p = i.get_pos();
575
576                 if( p == pos ){
577
578                     i.set_in_between( true );
579
580                 } else if ( p > pos ){
581
582                     i.set_pos( p-1 );
583
584                 }
585
586             }
587
588         }
589
590     };
591
592 /* ------------------------------------------------------------------------- */
593
594     protected void element_replaced( int pos, Any JavaDoc old ){
595
596     };
597
598
599
600 };
601
602
603
604
605
606
607
608
609
610
611
612
613
614
Popular Tags