KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.collection;
22
23 import org.omg.CosCollection.*;
24 import org.jacorb.collection.util.*;
25 import java.util.*;
26 import org.omg.PortableServer.POA JavaDoc;
27 import org.omg.PortableServer.Servant JavaDoc;
28 import org.omg.CORBA.Any JavaDoc;
29 import org.omg.CORBA.AnyHolder JavaDoc;
30 import org.omg.CORBA.BooleanHolder JavaDoc;
31
32 class KeySortedIteratorImpl extends OrderedIteratorImpl
33                             implements KeySortedIteratorOperations {
34     KeyNode test_key = new KeyNode();
35     KeySortedCollectionImpl key_collection;
36 /* ========================================================================= */
37     KeySortedIteratorImpl( KeySortedCollectionImpl collection ){
38         super( collection );
39         key_collection = collection;
40     }
41 /* ------------------------------------------------------------------------- */
42     KeySortedIteratorImpl( KeySortedCollectionImpl collection, boolean read_only ){
43         super( collection, read_only );
44         key_collection = collection;
45     }
46 /* ------------------------------------------------------------------------- */
47     KeySortedIteratorImpl( KeySortedCollectionImpl collection, boolean read_only, boolean reverse ){
48         super( collection, read_only, reverse );
49         key_collection = collection;
50     }
51 /* ========================================================================= */
52 /* ------------------------------------------------------------------------- */
53 // ---- Key Iterator
54
/* ------------------------------------------------------------------------- */
55     public boolean set_to_element_with_key( Any JavaDoc key ) throws KeyInvalid {
56         synchronized( collection ){
57             key_collection.check_key( key );
58             test_key.key = key;
59             try {
60                 int pos = key_collection.keys.indexOf( key );
61                 if( pos >=0 ){
62                     KeyNode node = (KeyNode)key_collection.keys.elementAt( pos );
63                     set_pos( node.start_position );
64                     set_in_between( false );
65                     return true;
66                 }
67                 invalidate();
68                 return false;
69             } catch ( ObjectInvalid e ) {
70                 e.printStackTrace( System.out );
71                 throw new org.omg.CORBA.INTERNAL JavaDoc();
72             }
73         }
74     }
75 /* ------------------------------------------------------------------------- */
76     public boolean set_to_next_element_with_key( Any JavaDoc key ) throws IteratorInvalid,KeyInvalid {
77         synchronized( collection ){
78             check_invalid();
79             key_collection.check_key( key );
80             test_key.key = key;
81             try {
82                 int pos = key_collection.keys.indexOf( key );
83                 if( pos >=0 ){
84                     KeyNode node = (KeyNode)key_collection.keys.elementAt( pos );
85                     int start_pos = is_in_between()?get_pos():get_pos()+1;
86                     if( start_pos <= node.start_position || start_pos < node.start_position + node.count ){
87                         set_pos( start_pos );
88                         set_in_between( false );
89                         return true;
90                     }
91                 }
92                 invalidate();
93                 return false;
94             } catch ( ObjectInvalid e ) {
95                 e.printStackTrace( System.out );
96                 throw new org.omg.CORBA.INTERNAL JavaDoc();
97             }
98         }
99     }
100 /* ------------------------------------------------------------------------- */
101     public boolean set_to_next_element_with_different_key() throws IteratorInBetween, IteratorInvalid {
102         synchronized( collection ){
103             check_iterator();
104             Any JavaDoc key = collection.ops.key( (Any JavaDoc)collection.data.elementAt( get_pos() ) );
105             test_key.key = key;
106             try {
107                 int pos = key_collection.keys.indexOf( key );
108                 if( pos >=0 && pos < key_collection.keys.size()-1 ){
109                     pos++;
110                     KeyNode node = (KeyNode)key_collection.keys.elementAt( pos );
111                     set_pos( node.start_position );
112                     set_in_between( false );
113                     return true;
114                 }
115                 invalidate();
116                 return false;
117             } catch ( ObjectInvalid e ) {
118                 e.printStackTrace( System.out );
119                 throw new org.omg.CORBA.INTERNAL JavaDoc();
120             }
121         }
122     }
123 /* ------------------------------------------------------------------------- */
124     public boolean retrieve_key( AnyHolder JavaDoc key ) throws IteratorInBetween, IteratorInvalid {
125         synchronized( collection ){
126             check_iterator();
127             key.value = collection.ops.key( (Any JavaDoc)collection.data.elementAt( get_pos() ) );
128             return true;
129         }
130     }
131 /* ------------------------------------------------------------------------- */
132     public boolean retrieve_next_n_keys( AnySequenceHolder keys ) throws IteratorInBetween, IteratorInvalid {
133         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
134     }
135 /* ------------------------------------------------------------------------- */
136 // ---- Key Sorted Iterator
137
/* ------------------------------------------------------------------------- */
138     public boolean set_to_first_element_with_key( Any JavaDoc key, LowerBoundStyle style) throws KeyInvalid {
139         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
140     }
141 /* ------------------------------------------------------------------------- */
142     public boolean set_to_last_element_with_key( Any JavaDoc key, UpperBoundStyle style) throws KeyInvalid {
143         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
144     }
145 /* ------------------------------------------------------------------------- */
146     public boolean set_to_previous_element_with_key( Any JavaDoc key ) throws IteratorInvalid, KeyInvalid {
147         synchronized( collection ){
148             check_invalid();
149             key_collection.check_key( key );
150             test_key.key = key;
151             try {
152                 int pos = key_collection.keys.indexOf( key );
153                 if( pos >=0 ){
154                     KeyNode node = (KeyNode)key_collection.keys.elementAt( pos );
155                     int start_pos = get_pos()-1;
156                     if( start_pos <= node.start_position || start_pos < node.start_position + node.count ){
157                         set_pos( start_pos );
158                         set_in_between( false );
159                         return true;
160                     }
161                 }
162                 invalidate();
163                 return false;
164             } catch ( ObjectInvalid e ) {
165                 e.printStackTrace( System.out );
166                 throw new org.omg.CORBA.INTERNAL JavaDoc();
167             }
168         }
169     }
170 /* ------------------------------------------------------------------------- */
171     public boolean set_to_previous_element_with_different_key() throws IteratorInBetween, IteratorInvalid {
172         synchronized( collection ){
173             check_iterator();
174             Any JavaDoc key = collection.ops.key( (Any JavaDoc)collection.data.elementAt( get_pos() ) );
175             test_key.key = key;
176             try {
177                 int pos = key_collection.keys.indexOf( key );
178                 if( pos > 0 ){
179                     pos--;
180                     KeyNode node = (KeyNode)key_collection.keys.elementAt( pos );
181                     set_pos( node.start_position );
182                     set_in_between( false );
183                     return true;
184                 }
185                 invalidate();
186                 return false;
187             } catch ( ObjectInvalid e ) {
188                 e.printStackTrace( System.out );
189                 throw new org.omg.CORBA.INTERNAL JavaDoc();
190             }
191         }
192     }
193 /* ------------------------------------------------------------------------- */
194     public boolean retrieve_previous_n_keys( AnySequenceHolder keys ) throws IteratorInBetween, IteratorInvalid {
195         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
196     }
197
198 }
199
200
201
202
203
Popular Tags