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 */
020 package org.apache.directory.shared.ldap.schema.comparators;
021
022
023 import java.util.Comparator;
024
025 import org.apache.directory.shared.i18n.I18n;
026 import org.apache.directory.shared.ldap.schema.LdapComparator;
027 import org.slf4j.Logger;
028 import org.slf4j.LoggerFactory;
029
030
031 /**
032 * Compares two objects taking into account that one might be a Comparable.
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 * @version $Rev: 912436 $
036 */
037 public class ComparableComparator<T> extends LdapComparator<Comparable<T>>
038 {
039 /** A logger for this class */
040 private static final Logger LOG = LoggerFactory.getLogger( ComparableComparator.class );
041
042 /** The serialVersionUID */
043 private static final long serialVersionUID = 1L;
044
045 /**
046 * The BooleanComparator constructor. Its OID is the BooleanMatch matching
047 * rule OID.
048 */
049 public ComparableComparator( String oid )
050 {
051 super( oid );
052 }
053
054 /**
055 * Compares two objects taking into account that one may be a Comparable. If
056 * the first is a comparable then its compareTo operation is called and the
057 * result returned as is. If the first is not a Comparable but the second is
058 * then its compareTo method is called and the result is returned after
059 * being negated. If none are comparables the hashCode of o1 minus the
060 * hashCode of o2 is returned.
061 *
062 * @see Comparator#compare(Object, Object)
063 */
064 public int compare( Comparable<T> o1, Comparable<T> o2 )
065 {
066 LOG.debug( "comparing objects '{}' with '{}'", o1, o2 );
067
068 if ( ( o1 == null ) && ( o2 == null ) )
069 {
070 return 0;
071 }
072
073 if ( o1 instanceof Comparable<?> )
074 {
075 if ( o2 == null )
076 {
077 return -1;
078 }
079 else
080 {
081 return o1.compareTo( ( T ) o2 );
082 }
083 }
084
085 if ( o2 == null )
086 {
087 return 1;
088 }
089 else if ( o2 instanceof Comparable<?> )
090 {
091 if ( o1 == null )
092 {
093 return -1;
094 }
095 else
096 {
097 return - o2.compareTo( ( T ) o1 );
098 }
099 }
100
101 // before https://issues.apache.org/jira/browse/DIRSERVER-928 it was
102 // return o1.hashCode() - o2.hashCode();
103
104 // now we will blow a stack trace if none of the objects are Comparable
105 throw new IllegalArgumentException( I18n.err( I18n.ERR_04217, o1, o2 ) );
106 }
107 }