001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2004 Gerald Brose
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: *
020: */
021: package org.jacorb.collection;
022:
023: import org.omg.CosCollection.*;
024:
025: import org.jacorb.collection.util.*;
026:
027: import java.util.*;
028: import org.omg.PortableServer.POA;
029:
030: import org.omg.PortableServer.Servant;
031:
032: import org.omg.CORBA.Any;
033:
034: import org.omg.CORBA.AnyHolder;
035:
036: import org.omg.CORBA.BooleanHolder;
037:
038: class OrderedIteratorImpl extends PositionalIteratorImpl
039:
040: implements OrderedIteratorOperations {
041:
042: /* ========================================================================= */
043:
044: protected boolean reverse; // reverse now not supperted
045:
046: /* ========================================================================= */
047:
048: OrderedIteratorImpl(OrderedCollectionImpl collection) {
049:
050: super (collection);
051:
052: reverse = false;
053:
054: };
055:
056: /* ------------------------------------------------------------------------- */
057:
058: OrderedIteratorImpl(OrderedCollectionImpl collection,
059: boolean read_only) {
060:
061: super (collection, read_only);
062:
063: reverse = false;
064:
065: };
066:
067: /* ------------------------------------------------------------------------- */
068:
069: OrderedIteratorImpl(OrderedCollectionImpl collection,
070: boolean read_only, boolean reverse) {
071:
072: super (collection, read_only);
073:
074: if (reverse) {
075:
076: throw new org.omg.CORBA.NO_IMPLEMENT();
077:
078: }
079:
080: this .reverse = reverse;
081:
082: };
083:
084: /* ========================================================================= */
085:
086: public boolean set_to_last_element() {
087:
088: synchronized (collection) {
089:
090: set_pos(collection.data.size() - 1);
091:
092: set_in_between(false);
093:
094: return is_valid();
095:
096: }
097:
098: };
099:
100: /* ------------------------------------------------------------------------- */
101:
102: public boolean set_to_previous_element() throws IteratorInvalid {
103:
104: return set_to_nth_previous_element(1);
105:
106: };
107:
108: /* ------------------------------------------------------------------------- */
109:
110: public boolean set_to_nth_previous_element(int n)
111: throws IteratorInvalid {
112:
113: synchronized (collection) {
114:
115: check_invalid();
116:
117: int new_pos = get_pos() - n;
118:
119: if (collection.number_of_elements() > new_pos
120: && new_pos >= 0) {
121:
122: set_pos(new_pos);
123:
124: } else {
125:
126: invalidate();
127:
128: throw new IteratorInvalid(
129: IteratorInvalidReason.is_invalid);
130:
131: }
132:
133: set_in_between(false);
134:
135: return is_valid();
136:
137: }
138:
139: };
140:
141: /* ------------------------------------------------------------------------- */
142:
143: public void set_to_position(int position) throws PositionInvalid {
144:
145: synchronized (collection) {
146:
147: if (position < 0 || position >= collection.data.size()) {
148:
149: throw new PositionInvalid();
150:
151: }
152:
153: set_pos(position);
154:
155: set_in_between(false);
156:
157: }
158: ;
159:
160: };
161:
162: /* ------------------------------------------------------------------------- */
163:
164: public int position() throws IteratorInvalid {
165:
166: synchronized (collection) {
167:
168: check_invalid();
169:
170: return get_pos();
171:
172: }
173:
174: }
175:
176: /* ------------------------------------------------------------------------- */
177:
178: public boolean retrieve_element_set_to_previous(AnyHolder element,
179: BooleanHolder more) throws IteratorInvalid,
180: IteratorInBetween {
181:
182: synchronized (collection) {
183:
184: check_iterator();
185:
186: try {
187:
188: element.value = collection.element_retrieve(pos);
189:
190: } catch (PositionInvalid e) {
191:
192: invalidate();
193:
194: throw new IteratorInvalid(
195: IteratorInvalidReason.is_invalid);
196:
197: }
198: ;
199:
200: more.value = (get_pos() > 0);
201:
202: return set_to_previous_element();
203:
204: }
205:
206: };
207:
208: /* ------------------------------------------------------------------------- */
209:
210: public boolean retrieve_previous_n_elements(int n,
211: AnySequenceHolder result, BooleanHolder more)
212: throws IteratorInvalid, IteratorInBetween {
213:
214: synchronized (collection) {
215:
216: check_iterator();
217:
218: Vector v = new Vector(n);
219:
220: AnyHolder a = new AnyHolder();
221:
222: for (int i = 0; (i < n || n == 0) && is_valid(); i++) {
223:
224: try {
225:
226: a.value = collection.element_retrieve(get_pos());
227:
228: set_pos(get_pos() - 1);
229:
230: } catch (PositionInvalid e) {
231:
232: invalidate();
233:
234: throw new IteratorInvalid(
235: IteratorInvalidReason.is_invalid);
236:
237: }
238: ;
239:
240: v.addElement(a.value);
241:
242: a.value = null;
243:
244: }
245:
246: more.value = (get_pos() > 0);
247:
248: if (v.size() > 0) {
249:
250: result.value = new Any[v.size()];
251:
252: v.copyInto((java.lang.Object[]) result.value);
253:
254: return true;
255:
256: } else {
257:
258: return false;
259:
260: }
261:
262: }
263:
264: };
265:
266: /* ------------------------------------------------------------------------- */
267:
268: public boolean not_equal_retrieve_element_set_to_previous(
269: org.omg.CosCollection.Iterator test,
270:
271: AnyHolder element)
272:
273: throws IteratorInvalid, IteratorInBetween
274:
275: {
276:
277: synchronized (collection) {
278:
279: check_iterator();
280:
281: PositionalIteratorImpl iter = collection
282: .check_iterator(test);
283:
284: iter.check_iterator();
285:
286: if (is_equal(test)) {
287:
288: retrieve_element(element);
289:
290: return false;
291:
292: } else {
293:
294: retrieve_element_set_to_previous(element,
295: new org.omg.CORBA.BooleanHolder());
296:
297: return true;
298:
299: }
300:
301: }
302:
303: };
304:
305: /* ------------------------------------------------------------------------- */
306:
307: public boolean remove_element_set_to_previous()
308: throws IteratorInvalid, IteratorInBetween {
309:
310: synchronized (collection) {
311:
312: remove_element();
313:
314: return set_to_previous_element();
315:
316: }
317:
318: };
319:
320: /* ------------------------------------------------------------------------- */
321:
322: public boolean remove_previous_n_elements(int n,
323: org.omg.CORBA.IntHolder actual_number)
324: throws IteratorInvalid, IteratorInBetween {
325:
326: synchronized (collection) {
327:
328: actual_number.value = 0;
329:
330: for (int i = 0; (i < n || n == 0) && is_valid(); i++, actual_number.value++) {
331:
332: remove_element_set_to_previous();
333:
334: }
335:
336: return is_valid();
337:
338: }
339:
340: };
341:
342: /* ------------------------------------------------------------------------- */
343:
344: public boolean not_equal_remove_element_set_to_previous(
345: org.omg.CosCollection.Iterator test)
346:
347: throws IteratorInvalid, IteratorInBetween
348:
349: {
350:
351: synchronized (collection) {
352:
353: check_iterator();
354:
355: PositionalIteratorImpl iter = collection
356: .check_iterator(test);
357:
358: iter.check_iterator();
359:
360: if (is_equal(test)) {
361:
362: remove_element();
363:
364: return false;
365:
366: } else {
367:
368: remove_element_set_to_previous();
369:
370: return true;
371:
372: }
373:
374: }
375:
376: };
377:
378: /* ------------------------------------------------------------------------- */
379:
380: public boolean replace_element_set_to_previous(
381: org.omg.CORBA.Any element) throws IteratorInvalid,
382: IteratorInBetween, ElementInvalid {
383:
384: synchronized (collection) {
385:
386: replace_element(element);
387:
388: return set_to_previous_element();
389:
390: }
391:
392: };
393:
394: /* ------------------------------------------------------------------------- */
395:
396: public boolean replace_previous_n_elements(
397: org.omg.CORBA.Any[] elements,
398: org.omg.CORBA.IntHolder actual_number)
399: throws IteratorInvalid, IteratorInBetween, ElementInvalid {
400:
401: synchronized (collection) {
402:
403: actual_number.value = 0;
404:
405: for (int i = 0; i < elements.length && is_valid(); i++, actual_number.value++) {
406:
407: replace_element_set_to_previous(elements[i]);
408:
409: }
410:
411: return is_valid();
412:
413: }
414:
415: };
416:
417: /* ------------------------------------------------------------------------- */
418:
419: public boolean not_equal_replace_element_set_to_previous(
420: org.omg.CosCollection.Iterator test, Any element)
421: throws IteratorInvalid, IteratorInBetween, ElementInvalid {
422:
423: synchronized (collection) {
424:
425: check_iterator();
426:
427: PositionalIteratorImpl iter = collection
428: .check_iterator(test);
429:
430: iter.check_iterator();
431:
432: if (is_equal(test)) {
433:
434: replace_element(element);
435:
436: return false;
437:
438: } else {
439:
440: replace_element_set_to_previous(element);
441:
442: return true;
443:
444: }
445:
446: }
447:
448: };
449:
450: /* ------------------------------------------------------------------------- */
451:
452: public boolean is_first() {
453:
454: return get_pos() == 0;
455:
456: };
457:
458: /* ------------------------------------------------------------------------- */
459:
460: public boolean is_last() {
461:
462: synchronized (collection) {
463:
464: return collection.data.size() - 1 == get_pos();
465:
466: }
467:
468: };
469:
470: /* ------------------------------------------------------------------------- */
471:
472: public boolean is_for_same(org.omg.CosCollection.Iterator test) {
473:
474: synchronized (collection) {
475:
476: try {
477:
478: collection.check_iterator(test);
479:
480: return true;
481:
482: } catch (IteratorInvalid e) {
483:
484: return false;
485:
486: }
487:
488: }
489:
490: };
491:
492: /* ------------------------------------------------------------------------- */
493:
494: public synchronized boolean is_reverse() {
495:
496: return reverse;
497:
498: };
499:
500: /* ========================================================================= */
501:
502: };
|