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 * Compares two objects taking into account that one might be a Comparable.
031 * 
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 * @param <T> the type, must extend {@link Comparable}
034 */
035public class ComparableComparator<T> extends LdapComparator<Comparable<T>>
036{
037    /** The serial version UID */
038    private static final long serialVersionUID = 2L;
039
040    /** A logger for this class */
041    private static final Logger LOG = LoggerFactory.getLogger( ComparableComparator.class );
042
043
044    /**
045     * The ComparableComparator constructor.
046     *
047     * @param oid the comparator OID
048     */
049    public ComparableComparator( String oid )
050    {
051        super( oid );
052    }
053
054
055    /**
056     * Compares two objects taking into account that one may be a Comparable. If
057     * the first is a comparable then its compareTo operation is called and the
058     * result returned as is. If the first is not a Comparable but the second is
059     * then its compareTo method is called and the result is returned after
060     * being negated. If none are comparable the hashCode of o1 minus the
061     * hashCode of o2 is returned.
062     *
063     * @param o1 the first comparable
064     * @param o2 the second comparable
065     * @return {@inheritDoc}
066     */
067    @SuppressWarnings("unchecked")
068    public int compare( Comparable<T> o1, Comparable<T> o2 )
069    {
070        if ( LOG.isDebugEnabled() )
071        {
072            LOG.debug( I18n.msg( I18n.MSG_13755_COMPARING_OBJECTS, o1, o2 ) );
073        }
074
075        if ( o1 == null )
076        {
077            if ( o2 == null )
078            {
079                return 0;
080            }
081            else
082            {
083                return -1;
084            }
085        }
086
087        if ( o2 == null )
088        {
089            return 1;
090        }
091
092        return o1.compareTo( ( T ) o2 );
093    }
094}