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.ldap.model.schema.LdapComparator;
024import org.apache.directory.api.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    /**
044     * The ByteArrayComparator constructor. Its OID is the OctetStringMatch matching
045     * rule OID.
046     */
047    public ByteArrayComparator( String oid )
048    {
049        super( oid );
050    }
051
052
053    /**
054     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
055     */
056    public int compare( byte[] b1, byte[] b2 )
057    {
058        LOG.debug( "comparing OctetString objects '{}' with '{}'",
059            Strings.dumpBytes( b1 ), Strings.dumpBytes( b2 ) );
060
061        // -------------------------------------------------------------------
062        // Handle some basis cases
063        // -------------------------------------------------------------------
064
065        if ( b1 == null )
066        {
067            return ( b2 == null ) ? 0 : -1;
068        }
069
070        if ( b2 == null )
071        {
072            return 1;
073        }
074
075        if ( b1.length == b2.length )
076        {
077            for ( int i = 0; i < b1.length; i++ )
078            {
079                if ( b1[i] > b2[i] )
080                {
081                    return 1;
082                }
083                else if ( b1[i] < b2[i] )
084                {
085                    return -1;
086                }
087            }
088
089            return 0;
090        }
091
092        int minLength = Math.min( b1.length, b2.length );
093
094        for ( int i = 0; i < minLength; i++ )
095        {
096            if ( b1[i] > b2[i] )
097            {
098                return 1;
099            }
100            else if ( b1[i] < b2[i] )
101            {
102                return -1;
103            }
104        }
105
106        // b2 is longer w/ b1 as prefix 
107        if ( b1.length == minLength )
108        {
109            return -1;
110        }
111
112        // b1 is longer w/ b2 as prefix
113        if ( b2.length == minLength )
114        {
115            return 1;
116        }
117
118        return 0;
119    }
120}