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.i18n.I18n;
024import org.apache.directory.api.ldap.model.schema.LdapComparator;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028
029/**
030 * A comparator for the objectIdentifierMatch matchingRule.
031 *
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 */
034public class ObjectIdentifierComparator extends LdapComparator<Object>
035{
036    /** The serial version UID */
037    private static final long serialVersionUID = 2L;
038
039    /** A logger for this class */
040    private static final Logger LOG = LoggerFactory.getLogger( ObjectIdentifierComparator.class );
041
042
043    /**
044     * The ObjectIdentifierComparator constructor. Its OID is the ObjectIdentifierMatch matching
045     * rule OID.
046     * 
047     * @param oid The Comparator's OID
048     */
049    public ObjectIdentifierComparator( String oid )
050    {
051        super( oid );
052    }
053
054
055    /**
056     * {@inheritDoc}
057     */
058    @Override
059    public int compare( Object o1, Object o2 )
060    {
061        if ( LOG.isDebugEnabled() )
062        {
063            LOG.debug( I18n.msg( I18n.MSG_13747_COMPARING_OBJECT_IDENTIFIER, o1, o2 ) );
064        }
065
066        // -------------------------------------------------------------------
067        // Handle some basis cases
068        // -------------------------------------------------------------------
069        if ( o1 == null )
070        {
071            return ( o2 == null ) ? 0 : -1;
072        }
073        else if ( o2 == null )
074        {
075            return 1;
076        }
077
078        if ( o1.equals( o2 ) )
079        {
080            return 0;
081        }
082
083        if ( !( o1 instanceof String && o2 instanceof String ) )
084        {
085            return -1;
086        }
087
088        // Here, we should leverage the SchemaManager to compare the String and the OID
089        return ( ( String ) o1 ).compareToIgnoreCase( ( String ) o2 );
090    }
091}