KickJava   Java API By Example, From Geeks To Geeks.

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


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.OperationsOperations;
24 import org.omg.CORBA.Any JavaDoc;
25 import org.jacorb.collection.util.*;
26
27 class SortedRelationComparator implements ObjectComparator {
28     private OperationsOperations ops;
29     private Any JavaDoc current = null;
30     private Any JavaDoc current_key = null;
31 /* ------------------------------------------------------------------------- */
32     SortedRelationComparator( OperationsOperations ops ) {
33         this.ops = ops;
34     }
35 /* ------------------------------------------------------------------------- */
36     public synchronized int compare( Object JavaDoc obj1, Object JavaDoc obj2 ) throws ObjectInvalid {
37         if( obj1 == null || obj2 == null ){
38             throw new ObjectInvalid();
39         }
40         check_object( obj1 );
41         check_object( obj2 );
42         Any JavaDoc key1 = ops.key( (Any JavaDoc)obj1 );
43         Any JavaDoc key2 = ops.key( (Any JavaDoc)obj2 );
44         int result = ops.key_compare( key1, key2 );
45         if( result == 0 ){
46             result = ops.compare( (Any JavaDoc)obj1, (Any JavaDoc)obj2 );
47         }
48         return result;
49     }
50 /* ------------------------------------------------------------------------- */
51     public synchronized void element( Object JavaDoc obj ) throws ObjectInvalid {
52         check_object( obj );
53         current = (Any JavaDoc) obj;
54         if( current != null ){
55             current_key = ops.key( current );
56         } else {
57             current_key = null;
58         }
59     }
60 /* ------------------------------------------------------------------------- */
61     public synchronized Object JavaDoc element() {
62         return current;
63     }
64 /* ------------------------------------------------------------------------- */
65     public synchronized int compare_with( Object JavaDoc obj ) throws ObjectInvalid {
66         if( obj == null || current == null ) {
67             throw new ObjectInvalid();
68         }
69         check_object( obj );
70         Any JavaDoc key = ops.key( (Any JavaDoc)obj );
71         int result = ops.key_compare( current_key, key );
72         if( result == 0 ){
73             result = ops.compare( current, (Any JavaDoc)obj );
74         }
75         return result;
76     }
77 /* ------------------------------------------------------------------------- */
78     public synchronized boolean equal( Object JavaDoc obj1, Object JavaDoc obj2 ) throws ObjectInvalid {
79         if( obj1 == null || obj2 == null ){
80             throw new ObjectInvalid();
81         }
82         check_object( obj1 );
83         check_object( obj2 );
84         return ops.equal( (Any JavaDoc)obj1, (Any JavaDoc)obj2 );
85     }
86 /* ------------------------------------------------------------------------- */
87     public synchronized boolean equal( Object JavaDoc obj1 ) throws ObjectInvalid {
88         if( obj1 == null || current == null ) {
89             throw new ObjectInvalid();
90         }
91         check_object( obj1 );
92         return ops.equal( current, (Any JavaDoc)obj1 );
93     }
94 /* ------------------------------------------------------------------------- */
95     private void check_object( Object JavaDoc obj ) throws ObjectInvalid {
96         if( !( obj instanceof Any JavaDoc )
97             || !((Any JavaDoc)obj).type().equal( ops.element_type() ) ){
98             throw new ObjectInvalid();
99         }
100     }
101 }
102
103
104
105
106
107
108
109
110
Popular Tags