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        LOG.debug( "comparing objects '{}' with '{}'", o1, o2 );
071
072        if ( ( o1 == null ) && ( o2 == null ) )
073        {
074            return 0;
075        }
076
077        if ( o1 instanceof Comparable<?> )
078        {
079            if ( o2 == null )
080            {
081                return -1;
082            }
083            else
084            {
085                // TODO: check type parameter
086                return o1.compareTo( ( T ) o2 );
087            }
088        }
089
090        if ( o2 == null )
091        {
092            return 1;
093        }
094        else if ( o2 instanceof Comparable<?> )
095        {
096            if ( o1 == null )
097            {
098                return -1;
099            }
100            else
101            {
102                // TODO: check type parameter
103                return -o2.compareTo( ( T ) o1 );
104            }
105        }
106
107        // before https://issues.apache.org/jira/browse/DIRSERVER-928 it was
108        // return o1.hashCode() - o2.hashCode();
109
110        // now we will blow a stack trace if none of the objects are Comparable
111        throw new IllegalArgumentException( I18n.err( I18n.ERR_04217, o1, o2 ) );
112    }
113}