001    /**
002     * 
003     */
004    package org.apache.directory.shared.ldap.schema.comparators;
005    
006    
007    import org.apache.directory.shared.ldap.schema.LdapComparator;
008    import org.apache.directory.shared.ldap.util.StringTools;
009    
010    
011    /**
012     * A comparator that compares the objectClass type with values: AUXILIARY,
013     * ABSTRACT, and STRUCTURAL.
014     * 
015     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
016     * @version $Rev$
017     */
018    public class ObjectClassTypeComparator<T> extends LdapComparator<T> 
019    {
020        private static final long serialVersionUID = 1L;
021    
022        
023        public ObjectClassTypeComparator( String oid )
024        {
025            super( oid );
026        }
027        
028        public int compare( T o1, T o2 )
029        {
030            String s1 = getString( o1 );
031            String s2 = getString( o2 );
032            
033            if ( s1 == null && s2 == null )
034            {
035                return 0;
036            }
037            
038            if ( s1 == null )
039            {
040                return -1;
041            }
042            
043            if ( s2 == null )
044            {
045                return 1;
046            }
047            
048            return s1.compareTo( s2 );
049        }
050        
051        
052        String getString( T obj )
053        {
054            String strValue;
055    
056            if ( obj == null )
057            {
058                return null;
059            }
060            
061            if ( obj instanceof String )
062            {
063                strValue = ( String ) obj;
064            }
065            else if ( obj instanceof byte[] )
066            {
067                strValue = StringTools.utf8ToString( ( byte[] ) obj ); 
068            }
069            else
070            {
071                strValue = obj.toString();
072            }
073    
074            return strValue;
075        }
076    }