KickJava   Java API By Example, From Geeks To Geeks.

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


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
31 class EqualityKeySortedCollectionImpl
32     extends KeySortedCollectionImpl
33     implements EqualityKeySortedCollectionOperations
34 {
35
36     /* ========================================================================= */
37     EqualityKeySortedCollectionImpl( OperationsOperations ops,
38                      POA JavaDoc poa,
39                      IteratorFactory iterator_factory )
40     {
41         super( ops, poa, iterator_factory );
42     }
43
44     /* ========================================================================= */
45     public synchronized boolean contains_element(Any JavaDoc element)
46     throws ElementInvalid
47     {
48         check_element( element );
49         try
50         {
51             return data.indexOf( element ) >= 0;
52         }
53         catch ( ObjectInvalid e )
54         {
55             throw new ElementInvalid( ElementInvalidReason.element_type_invalid );
56         }
57     }
58
59     /* ------------------------------------------------------------------------- */
60
61     public synchronized boolean contains_all_from(org.omg.CosCollection.Collection collector)
62     throws ElementInvalid
63     {
64         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
65     }
66
67     /* ------------------------------------------------------------------------- */
68     public synchronized boolean locate_or_add_element(Any JavaDoc element) throws ElementInvalid{
69         check_element( element );
70         try {
71             if( data.indexOf( element ) < 0 ){
72                 element_add( element );
73                 return false;
74             }
75             return true;
76         } catch ( ObjectInvalid e ){
77             throw new ElementInvalid( ElementInvalidReason.element_type_invalid );
78         }
79     }
80     /* ------------------------------------------------------------------------- */
81     public synchronized boolean locate_or_add_element_set_iterator(Any JavaDoc element, org.omg.CosCollection.Iterator where) throws ElementInvalid,IteratorInvalid{
82         check_element( element );
83         PositionalIteratorImpl i = check_iterator( where );
84         try {
85             int pos = data.indexOf( element );
86             if( pos < 0 ){
87                 pos = element_add( element );
88                 i.set_pos( pos );
89                 i.set_in_between( false );
90                 return false;
91             } else {
92                 i.set_pos( pos );
93                 i.set_in_between( false );
94                 return true;
95             }
96         } catch ( ObjectInvalid e ){
97             throw new ElementInvalid( ElementInvalidReason.element_type_invalid );
98         }
99     }
100     /* ------------------------------------------------------------------------- */
101     public synchronized boolean locate_element(Any JavaDoc element, org.omg.CosCollection.Iterator where) throws ElementInvalid,IteratorInvalid{
102         check_element( element );
103         PositionalIteratorImpl i = check_iterator( where );
104         try {
105             int pos = data.indexOf( element );
106             if( pos >= 0 ){
107                 i.set_pos( pos );
108                 i.set_in_between( false );
109                 return true;
110             } else {
111                 i.invalidate();
112                 return false;
113             }
114         } catch ( ObjectInvalid e ){
115             throw new ElementInvalid( ElementInvalidReason.element_type_invalid );
116         }
117     }
118     /* ------------------------------------------------------------------------- */
119     public synchronized boolean locate_next_element(Any JavaDoc element, org.omg.CosCollection.Iterator where) throws ElementInvalid,IteratorInvalid{
120         check_element( element );
121         PositionalIteratorImpl i = check_iterator( where );
122         i.check_invalid();
123         try {
124             int pos = data.indexOf( element );
125             if( pos >= 0 ){
126                 int new_pos = i.is_in_between()?pos:pos+1;
127                 while( new_pos < data.size() && ops.compare( element, (Any JavaDoc)data.elementAt(new_pos) ) == 0 ){
128                     if( ops.equal( element, (Any JavaDoc)data.elementAt( new_pos ) ) ){
129                         i.set_pos( new_pos );
130                         i.set_in_between( false );
131                         return true;
132                     }
133                     new_pos++;
134                 }
135             }
136             i.invalidate();
137             return false;
138         } catch ( ObjectInvalid e ){
139             throw new ElementInvalid( ElementInvalidReason.element_type_invalid );
140         }
141     }
142     /* ------------------------------------------------------------------------- */
143     public synchronized boolean locate_next_different_element(org.omg.CosCollection.Iterator where)
144         throws IteratorInvalid,IteratorInBetween
145     {
146         PositionalIteratorImpl i = check_iterator( where );
147         i.check_iterator();
148         int pos = i.get_pos();
149         Any JavaDoc element = (Any JavaDoc)data.elementAt( pos );
150         if( pos >= 0 ){
151             int new_pos = pos+1;
152             while( new_pos < data.size() ){
153                 if( ops.compare( element, (Any JavaDoc)data.elementAt( new_pos ) ) != 0
154                     || !ops.equal( element, (Any JavaDoc)data.elementAt( new_pos ) ) ){
155                     i.set_pos( new_pos );
156                     i.set_in_between( false );
157                     return true;
158                 }
159                 new_pos++;
160             }
161         }
162         i.invalidate();
163         return false;
164     }
165     /* ------------------------------------------------------------------------- */
166     public synchronized boolean remove_element(Any JavaDoc element)
167         throws ElementInvalid
168     {
169         check_element( element );
170         try {
171             int pos = data.indexOf( element );
172             if( pos >= 0 ){
173                 element_remove( 0 );
174                 return true;
175             }
176             return false;
177         } catch ( ObjectInvalid e ){
178             throw new ElementInvalid( ElementInvalidReason.element_type_invalid );
179         } catch ( Exception JavaDoc e ){
180             e.printStackTrace( System.out );
181             throw new org.omg.CORBA.INTERNAL JavaDoc();
182         }
183     }
184
185     /* ------------------------------------------------------------------------- */
186     public synchronized int remove_all_occurrences(Any JavaDoc element) throws ElementInvalid{
187         check_element( element );
188         try {
189             int pos = data.indexOf( element );
190             int count = 0;
191             while( pos < data.size() && ops.equal( element, (Any JavaDoc)data.elementAt( pos ) ) ){
192                 element_remove( pos );
193                 count++;
194             }
195             return count;
196         } catch ( ObjectInvalid e ){
197             throw new ElementInvalid( ElementInvalidReason.element_type_invalid );
198         } catch ( Exception JavaDoc e ){
199             e.printStackTrace( System.out );
200             throw new org.omg.CORBA.INTERNAL JavaDoc();
201         }
202     }
203
204     /* ------------------------------------------------------------------------ */
205     public synchronized int number_of_different_elements(){
206         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
207     }
208
209     /* ------------------------------------------------------------------------- */
210     public synchronized int number_of_occurrences(Any JavaDoc element) throws ElementInvalid{
211         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
212     }
213
214     /* ========================================================================= */
215     /* Overrided */
216     /* ========================================================================= */
217
218     public synchronized org.omg.CosCollection.Iterator create_iterator(boolean read_only)
219     {
220         return create_ordered_iterator( read_only, false );
221     }
222
223
224 }
225
226
Popular Tags