001: package org.jacorb.collection;
002:
003: /*
004:
005: * JacORB collection service
006:
007: *
008:
009: * Copyright (C) 1999-2004 LogicLand group, Viacheslav Tararin.
010:
011: *
012:
013: * This library is free software; you can redistribute it and/or
014:
015: * modify it under the terms of the GNU Library General Public
016:
017: * License as published by the Free Software Foundation; either
018:
019: * version 2 of the License, or (at your option) any later version.
020:
021: *
022:
023: * This library is distributed in the hope that it will be useful,
024:
025: * but WITHOUT ANY WARRANTY; without even the implied warranty of
026:
027: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
028:
029: * Library General Public License for more details.
030:
031: *
032:
033: * You should have received a copy of the GNU Library General Public
034:
035: * License along with this library; if not, write to the Free
036:
037: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
038:
039: */
040:
041: import org.omg.CosCollection.*;
042:
043: import org.omg.PortableServer.Servant;
044:
045: import org.omg.CORBA.Any;
046:
047: import org.omg.CORBA.AnyHolder;
048:
049: import org.jacorb.collection.util.*;
050:
051: import java.util.*;
052:
053: class PositionalIteratorImpl
054:
055: implements org.omg.CosCollection.IteratorOperations
056:
057: {
058:
059: private Servant srvnt = null;
060:
061: protected int pos = -1;
062:
063: protected boolean in_between = false;
064:
065: protected boolean read_only = true;
066:
067: protected CollectionImpl collection;
068:
069: /* ========================================================================= */
070:
071: PositionalIteratorImpl(CollectionImpl collection) {
072:
073: this .collection = collection;
074:
075: };
076:
077: /* ------------------------------------------------------------------------- */
078:
079: PositionalIteratorImpl(CollectionImpl collection, boolean read_only) {
080:
081: this .collection = collection;
082:
083: this .read_only = read_only;
084:
085: };
086:
087: /* ========================================================================= */
088:
089: public boolean set_to_first_element() {
090:
091: synchronized (collection) {
092:
093: check_servant();
094:
095: if (collection.is_empty()) {
096:
097: invalidate();
098:
099: } else {
100:
101: set_pos(0);
102:
103: }
104: ;
105:
106: }
107: ;
108:
109: return get_pos() == 0;
110:
111: };
112:
113: /* ------------------------------------------------------------------------- */
114:
115: public boolean set_to_next_element() throws IteratorInvalid {
116:
117: return set_to_next_nth_element(1);
118:
119: };
120:
121: /* ------------------------------------------------------------------------- */
122:
123: public boolean set_to_next_nth_element(int n)
124: throws IteratorInvalid {
125:
126: synchronized (collection) {
127:
128: check_invalid();
129:
130: int new_pos = get_pos() + n;
131:
132: if (is_in_between()) {
133:
134: new_pos = get_pos() + n - 1;
135:
136: }
137:
138: if (collection.number_of_elements() > new_pos
139: && new_pos >= 0) {
140:
141: set_pos(new_pos);
142:
143: } else {
144:
145: invalidate();
146:
147: }
148:
149: in_between = false;
150:
151: return exist_next();
152:
153: }
154:
155: };
156:
157: /* ------------------------------------------------------------------------- */
158:
159: public boolean retrieve_element(AnyHolder element)
160: throws IteratorInvalid, IteratorInBetween {
161:
162: synchronized (collection) {
163:
164: check_iterator();
165:
166: try {
167:
168: element.value = collection.element_retrieve(get_pos());
169:
170: return true;
171:
172: } catch (PositionInvalid e) {
173:
174: set_pos(-1);
175:
176: throw new IteratorInvalid(
177: IteratorInvalidReason.is_invalid);
178:
179: }
180:
181: }
182:
183: };
184:
185: /* ------------------------------------------------------------------------- */
186:
187: public boolean retrieve_element_set_to_next(AnyHolder element,
188: org.omg.CORBA.BooleanHolder more) throws IteratorInvalid,
189: IteratorInBetween {
190:
191: synchronized (collection) {
192:
193: boolean rc = retrieve_element(element);
194:
195: set_to_next_element();
196:
197: more.value = exist_next();
198:
199: return rc;
200:
201: }
202:
203: };
204:
205: /* ------------------------------------------------------------------------- */
206:
207: public boolean retrieve_next_n_elements(int n,
208: AnySequenceHolder result, org.omg.CORBA.BooleanHolder more)
209: throws IteratorInvalid, IteratorInBetween {
210:
211: Vector v = new Vector(n);
212:
213: int i = 0;
214:
215: synchronized (collection) {
216:
217: check_iterator();
218:
219: boolean b = true;
220:
221: AnyHolder a = new AnyHolder();
222:
223: more.value = true;
224:
225: for (i = 0; (i < n || n == 0) && get_pos() != -1; i++) {
226:
227: try {
228:
229: retrieve_element_set_to_next(a, more);
230:
231: v.addElement(a.value);
232:
233: } catch (IteratorInvalid e) {
234:
235: more.value = false;
236:
237: break;
238:
239: }
240:
241: }
242:
243: }
244:
245: Any[] anies = new Any[v.size()];
246:
247: v.copyInto((java.lang.Object[]) anies);
248:
249: result.value = anies;
250:
251: return i > 0;
252:
253: };
254:
255: /* ------------------------------------------------------------------------- */
256:
257: public boolean not_equal_retrieve_element_set_to_next(
258: org.omg.CosCollection.Iterator test, AnyHolder element)
259: throws IteratorInvalid, IteratorInBetween {
260:
261: synchronized (collection) {
262:
263: check_iterator();
264:
265: if (is_equal(test)) {
266:
267: retrieve_element(element);
268:
269: return false;
270:
271: } else {
272:
273: retrieve_element(element);
274:
275: set_to_next_element();
276:
277: return true;
278:
279: }
280:
281: }
282:
283: }
284:
285: /* ------------------------------------------------------------------------- */
286:
287: public void remove_element() throws IteratorInvalid,
288: IteratorInBetween {
289:
290: synchronized (collection) {
291:
292: check_iterator();
293:
294: check_read_only();
295:
296: try {
297:
298: collection.element_remove(get_pos());
299:
300: } catch (PositionInvalid e) {
301:
302: invalidate();
303:
304: throw new IteratorInvalid(
305: IteratorInvalidReason.is_invalid);
306:
307: } catch (EmptyCollection e) {
308:
309: invalidate();
310:
311: throw new IteratorInvalid(
312: IteratorInvalidReason.is_invalid);
313:
314: }
315: ;
316:
317: }
318: ;
319:
320: };
321:
322: /* ------------------------------------------------------------------------- */
323:
324: public boolean remove_element_set_to_next() throws IteratorInvalid,
325: IteratorInBetween {
326:
327: synchronized (collection) {
328:
329: remove_element();
330:
331: return set_to_next_element();
332:
333: }
334:
335: };
336:
337: /* ------------------------------------------------------------------------- */
338:
339: public boolean remove_next_n_elements(int n,
340: org.omg.CORBA.IntHolder actual_number)
341: throws IteratorInvalid, IteratorInBetween {
342:
343: synchronized (collection) {
344:
345: int count = 0;
346:
347: for (int i = 0; (i < n || n == 0) && get_pos() != -1; i++, count++) {
348:
349: remove_element_set_to_next();
350:
351: }
352:
353: actual_number.value = count;
354:
355: return exist_next();
356:
357: }
358:
359: }
360:
361: /* ------------------------------------------------------------------------- */
362:
363: public boolean not_equal_remove_element_set_to_next(
364: org.omg.CosCollection.Iterator test)
365:
366: throws IteratorInvalid, IteratorInBetween {
367:
368: synchronized (collection) {
369:
370: check_iterator();
371:
372: if (is_equal(test)) {
373:
374: remove_element();
375:
376: return false;
377:
378: } else {
379:
380: remove_element_set_to_next();
381:
382: return true;
383:
384: }
385:
386: }
387:
388: };
389:
390: /* ------------------------------------------------------------------------- */
391:
392: public void replace_element(Any element) throws IteratorInvalid,
393: IteratorInBetween, ElementInvalid {
394:
395: synchronized (collection) {
396:
397: check_iterator();
398:
399: check_read_only();
400:
401: try {
402:
403: collection.element_replace(get_pos(), element);
404:
405: } catch (PositionInvalid e) {
406:
407: invalidate();
408:
409: throw new IteratorInvalid(
410: IteratorInvalidReason.is_invalid);
411:
412: }
413:
414: }
415:
416: };
417:
418: /* ------------------------------------------------------------------------- */
419:
420: public boolean replace_element_set_to_next(Any element)
421: throws IteratorInvalid, IteratorInBetween, ElementInvalid {
422:
423: synchronized (collection) {
424:
425: replace_element(element);
426:
427: return set_to_next_element();
428:
429: }
430:
431: };
432:
433: /* ------------------------------------------------------------------------- */
434:
435: public boolean replace_next_n_elements(Any[] elements,
436: org.omg.CORBA.IntHolder actual_number)
437: throws IteratorInvalid, IteratorInBetween, ElementInvalid {
438:
439: synchronized (collection) {
440:
441: actual_number.value = 0;
442:
443: for (int i = 0; i < elements.length && is_valid(); i++, actual_number.value++) {
444:
445: replace_element_set_to_next(elements[i]);
446:
447: }
448:
449: return exist_next();
450:
451: }
452:
453: };
454:
455: /* ------------------------------------------------------------------------- */
456:
457: public boolean not_equal_replace_element_set_to_next(
458: org.omg.CosCollection.Iterator test, Any element)
459: throws IteratorInvalid, IteratorInBetween, ElementInvalid {
460:
461: synchronized (collection) {
462:
463: check_iterator();
464:
465: if (is_equal(test)) {
466:
467: replace_element(element);
468:
469: return false;
470:
471: } else {
472:
473: replace_element_set_to_next(element);
474:
475: return true;
476:
477: }
478:
479: }
480:
481: }
482:
483: /* ------------------------------------------------------------------------- */
484:
485: public boolean add_element_set_iterator(Any element)
486: throws ElementInvalid {
487:
488: synchronized (collection) {
489:
490: pos = collection.element_add(element);
491:
492: set_pos(pos);
493:
494: in_between = false;
495:
496: return true;
497:
498: }
499:
500: };
501:
502: /* ------------------------------------------------------------------------- */
503:
504: public boolean add_n_elements_set_iterator(Any[] elements,
505: org.omg.CORBA.IntHolder actual_number)
506: throws ElementInvalid {
507:
508: synchronized (collection) {
509:
510: actual_number.value = 0;
511:
512: int pos[] = new int[elements.length];
513:
514: for (int i = 0; i < elements.length; i++) {
515:
516: collection.check_element(elements[i]);
517:
518: }
519:
520: for (int i = 0; i < elements.length; i++, actual_number.value++) {
521:
522: add_element_set_iterator(elements[i]);
523:
524: }
525:
526: return true;
527:
528: }
529:
530: };
531:
532: /* ------------------------------------------------------------------------- */
533:
534: public synchronized void invalidate() {
535:
536: pos = -1;
537:
538: };
539:
540: /* ------------------------------------------------------------------------- */
541:
542: public synchronized boolean is_valid() {
543:
544: return pos != -1;
545:
546: };
547:
548: /* ------------------------------------------------------------------------- */
549:
550: public synchronized boolean is_in_between() {
551:
552: return in_between;
553:
554: };
555:
556: /* ------------------------------------------------------------------------- */
557:
558: public synchronized boolean is_for(
559: org.omg.CosCollection.Collection collector) {
560:
561: return collection.is_this _you(collector);
562:
563: };
564:
565: /* ------------------------------------------------------------------------- */
566:
567: public synchronized boolean is_const() {
568:
569: return read_only;
570:
571: };
572:
573: /* ------------------------------------------------------------------------- */
574:
575: public boolean is_equal(org.omg.CosCollection.Iterator test)
576: throws IteratorInvalid {
577:
578: synchronized (collection) {
579:
580: PositionalIteratorImpl iter = collection
581: .check_iterator(test);
582:
583: if (!is_valid()) {
584:
585: throw new IteratorInvalid(
586: IteratorInvalidReason.is_invalid);
587:
588: }
589:
590: return get_pos() == iter.get_pos();
591:
592: }
593:
594: }
595:
596: /* ------------------------------------------------------------------------- */
597:
598: public org.omg.CosCollection.Iterator _clone() {
599:
600: synchronized (collection) {
601:
602: org.omg.CosCollection.Iterator iter = collection
603: .create_iterator(read_only);
604:
605: try {
606:
607: PositionalIteratorImpl i = collection
608: .check_iterator(iter);
609:
610: i.set_pos(get_pos());
611:
612: return iter;
613:
614: } catch (IteratorInvalid e) {
615:
616: e.printStackTrace(System.out);
617:
618: throw new org.omg.CORBA.INTERNAL();
619:
620: }
621:
622: }
623:
624: }
625:
626: /* ------------------------------------------------------------------------- */
627:
628: public void assign(org.omg.CosCollection.Iterator from_where)
629: throws IteratorInvalid {
630:
631: synchronized (collection) {
632:
633: PositionalIteratorImpl i = collection
634: .check_iterator(from_where);
635:
636: i.set_pos(get_pos());
637:
638: i.set_in_between(is_in_between());
639:
640: }
641: ;
642:
643: };
644:
645: /* ------------------------------------------------------------------------- */
646:
647: public synchronized void destroy() {
648:
649: synchronized (collection) {
650:
651: check_servant();
652:
653: collection.destroy_me(this );
654:
655: }
656:
657: };
658:
659: /* ========================================================================= */
660:
661: public synchronized int get_pos() {
662:
663: check_servant();
664:
665: return pos;
666:
667: };
668:
669: /* ------------------------------------------------------------------------- */
670:
671: public synchronized void set_pos(int pos) {
672:
673: check_servant();
674:
675: this .pos = pos;
676:
677: };
678:
679: /* ------------------------------------------------------------------------- */
680:
681: public synchronized void set_in_between(boolean in_between) {
682:
683: check_servant();
684:
685: this .in_between = in_between;
686:
687: };
688:
689: /* ------------------------------------------------------------------------- */
690:
691: public synchronized void set_servant(Servant srvnt) {
692:
693: if (srvnt != null) {
694:
695: System.out.println("Error: Servant setted before!");
696:
697: throw new org.omg.CORBA.INTERNAL();
698:
699: }
700:
701: };
702:
703: /* ------------------------------------------------------------------------- */
704:
705: public synchronized Servant get_servant() {
706:
707: if (srvnt != null) {
708:
709: System.out.println("Error: Servant must be setted before!");
710:
711: throw new org.omg.CORBA.INTERNAL();
712:
713: }
714:
715: return srvnt;
716:
717: };
718:
719: /* ========================================================================= */
720:
721: protected void check_servant() {
722:
723: if (srvnt == null) {
724:
725: System.out.println("Error: Servant must be setted before!");
726:
727: throw new org.omg.CORBA.INTERNAL();
728:
729: }
730:
731: }
732:
733: /* -------------------------------------------------------------------------- */
734:
735: protected void check_invalid() throws IteratorInvalid {
736:
737: check_servant();
738:
739: if (pos == -1) {
740:
741: throw new IteratorInvalid(IteratorInvalidReason.is_invalid);
742:
743: }
744:
745: }
746:
747: /* -------------------------------------------------------------------------- */
748:
749: protected void check_in_between() throws IteratorInBetween {
750:
751: if (in_between) {
752:
753: throw new IteratorInBetween();
754:
755: }
756:
757: }
758:
759: /* -------------------------------------------------------------------------- */
760:
761: protected void check_iterator() throws IteratorInBetween,
762: IteratorInvalid {
763:
764: check_invalid();
765:
766: check_in_between();
767:
768: };
769:
770: /* -------------------------------------------------------------------------- */
771:
772: protected void check_read_only() throws IteratorInvalid {
773:
774: if (read_only) {
775:
776: throw new IteratorInvalid(IteratorInvalidReason.is_const);
777:
778: }
779:
780: }
781:
782: /* -------------------------------------------------------------------------- */
783:
784: protected synchronized boolean exist_next() {
785:
786: return is_valid()
787: && collection.number_of_elements() - 1 > get_pos();
788:
789: };
790:
791: };
|