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