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