1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.directory.api.ldap.model.schema.comparators; 21 22 23 import org.apache.directory.api.ldap.model.schema.LdapComparator; 24 import org.slf4j.Logger; 25 import org.slf4j.LoggerFactory; 26 27 28 /** 29 * Compares two objects taking into account that one might be a Comparable. 30 * 31 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 32 * @param <T> the type, must extend {@link Comparable} 33 */ 34 public class ComparableComparator<T> extends LdapComparator<Comparable<T>> 35 { 36 /** The serial version UID */ 37 private static final long serialVersionUID = 2L; 38 39 /** A logger for this class */ 40 private static final Logger LOG = LoggerFactory.getLogger( ComparableComparator.class ); 41 42 43 /** 44 * The ComparableComparator constructor. 45 * 46 * @param oid the comparator OID 47 */ 48 public ComparableComparator( String oid ) 49 { 50 super( oid ); 51 } 52 53 54 /** 55 * Compares two objects taking into account that one may be a Comparable. If 56 * the first is a comparable then its compareTo operation is called and the 57 * result returned as is. If the first is not a Comparable but the second is 58 * then its compareTo method is called and the result is returned after 59 * being negated. If none are comparable the hashCode of o1 minus the 60 * hashCode of o2 is returned. 61 * 62 * @param o1 the first comparable 63 * @param o2 the second comparable 64 * @return {@inheritDoc} 65 */ 66 @SuppressWarnings("unchecked") 67 public int compare( Comparable<T> o1, Comparable<T> o2 ) 68 { 69 LOG.debug( "comparing objects '{}' with '{}'", o1, o2 ); 70 71 if ( o1 == null ) 72 { 73 if ( o2 == null ) 74 { 75 return 0; 76 } 77 else 78 { 79 return -11; 80 } 81 } 82 83 if ( o2 == null ) 84 { 85 return 1; 86 } 87 88 return o1.compareTo( ( T ) o2 ); 89 } 90 }