/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */
package org.apache.directory.shared.ldap.schema;


import java.util.Comparator;
import java.io.Serializable;


/**
 * Compares two objects taking into account that one might be a Comparable.
 * 
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 * @version $Rev$
 */
public class ComparableComparator implements Comparator, Serializable
{
    static final long serialVersionUID = -5295278271807198471L;


    /**
     * Compares two objects taking into account that one may be a Comparable. If
     * the first is a comparable then its compareTo operation is called and the
     * result returned as is. If the first is not a Comparable but the second is
     * then its compareTo method is called and the result is returned after
     * being negated. If none are comparables the hashCode of o1 minus the
     * hashCode of o2 is returned.
     * 
     * @see Comparator#compare(Object, Object)
     */
    public int compare( Object o1, Object o2 )
    {
        if ( o1 instanceof Comparable )
        {
            return ( ( Comparable ) o1 ).compareTo( o2 );
        }

        if ( o2 instanceof Comparable )
        {
            return -( ( Comparable ) o2 ).compareTo( o1 );
        }

        return o1.hashCode() - o2.hashCode();
    }
}

@@ -1124,16 +1124,47 @@
 ev$%0A */%0A
+@SuppressWarnings(%22unchecked%22)%0A
 public c
@@ -2096,43 +2096,362 @@
     
-return o1.hashCode() - o2.hashCode(
+// before https://issues.apache.org/jira/browse/DIRSERVER-928 it was%0A        // return o1.hashCode() - o2.hashCode();%0A        %0A        // now we will blow a stack trace if none of the objects are Comparable%0A        throw new IllegalArgumentException( %22None of the arguments are Comparable objects:%5Cn%22 %0A            + %22%5Ctarg1 = %22 + o1 + %22%5Cn%5Ctarg2 = %22 + o2 
 );%0A 

fix for DIRSERVER-928: ComparableComparator in shared yeild incorrect results when two objects compared are not