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.shared.ldap.model.schema.comparators;
021
022
023import org.apache.directory.shared.ldap.model.schema.LdapComparator;
024import org.apache.directory.shared.util.Strings;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028
029/**
030 * A comparator for byte[]s.
031 * 
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 */
034public class ByteArrayComparator extends LdapComparator<byte[]>
035{
036    /** The serial version UID */
037    private static final long serialVersionUID = 2L;
038
039    /** A logger for this class */
040    private static final Logger LOG = LoggerFactory.getLogger( ByteArrayComparator.class );
041
042    /**
043     * The ByteArrayComparator constructor. Its OID is the OctetStringMatch matching
044     * rule OID.
045     */
046    public ByteArrayComparator( String oid )
047    {
048        super( oid );
049    }
050
051    /**
052     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
053     */
054    public int compare( byte[] b1, byte[] b2 )
055    {
056        LOG.debug( "comparing OctetString objects '{}' with '{}'", 
057            Strings.dumpBytes(b1), Strings.dumpBytes(b2) );
058
059        // -------------------------------------------------------------------
060        // Handle some basis cases
061        // -------------------------------------------------------------------
062
063        if ( b1 == null )
064        {
065            return ( b2 == null ) ? 0 : -1;
066        }
067        
068        if ( b2 == null )
069        {
070            return 1;
071        }
072        
073        if ( b1.length == b2.length )
074        {
075            for ( int i = 0; i < b1.length; i++ )
076            {
077                if ( b1[i] > b2[i] )
078                {
079                    return 1;
080                }
081                else if ( b1[i] < b2[i] )
082                {
083                    return -1;
084                }
085            }
086            
087            return 0;
088        }
089        
090        int minLength = Math.min( b1.length, b2.length );
091        
092        for ( int i = 0; i < minLength; i++ )
093        {
094            if ( b1[i] > b2[i] )
095            {
096                return 1;
097            }
098            else if ( b1[i] < b2[i] )
099            {
100                return -1;
101            }
102        }
103        
104        // b2 is longer w/ b1 as prefix 
105        if ( b1.length == minLength )
106        {
107            return -1;
108        }
109        
110        // b1 is longer w/ b2 as prefix
111        if ( b2.length == minLength )
112        {
113            return 1;
114        }
115        
116        return 0;
117    }
118}