001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.model.schema.comparators;
021
022
023import org.apache.directory.api.ldap.model.schema.LdapComparator;
024import org.apache.directory.api.util.Strings;
025
026
027/**
028 * A comparator that compares the objectClass type with values: AUXILIARY,
029 * ABSTRACT, and STRUCTURAL.
030 * 
031 * @param <T> The type of object to compare
032 * 
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class ObjectClassTypeComparator<T> extends LdapComparator<T>
036{
037    /** The serial version UID */
038    private static final long serialVersionUID = 2L;
039
040
041    /**
042     * 
043     * Creates a new instance of ObjectClassTypeComparator.
044     *
045     * @param oid The Comparator's OID
046     */
047    public ObjectClassTypeComparator( String oid )
048    {
049        super( oid );
050    }
051
052
053    /**
054     * {@inheritDoc}
055     */
056    @Override
057    public int compare( T o1, T o2 )
058    {
059        String s1 = getString( o1 );
060        String s2 = getString( o2 );
061
062        if ( s1 == null && s2 == null )
063        {
064            return 0;
065        }
066
067        if ( s1 == null )
068        {
069            return -1;
070        }
071
072        if ( s2 == null )
073        {
074            return 1;
075        }
076
077        return s1.compareTo( s2 );
078    }
079
080
081    String getString( T obj )
082    {
083        String strValue;
084
085        if ( obj == null )
086        {
087            return null;
088        }
089
090        if ( obj instanceof String )
091        {
092            strValue = ( String ) obj;
093        }
094        else if ( obj instanceof byte[] )
095        {
096            strValue = Strings.utf8ToString( ( byte[] ) obj );
097        }
098        else
099        {
100            strValue = obj.toString();
101        }
102
103        return strValue;
104    }
105}