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 org.apache.directory.shared.ldap.schema.LdapComparator;
024    import org.apache.directory.shared.ldap.util.StringTools;
025    import org.slf4j.Logger;
026    import 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     * @version $Rev$
034     */
035    public class ByteArrayComparator extends LdapComparator<byte[]>
036    {
037        /** A logger for this class */
038        private static final Logger LOG = LoggerFactory.getLogger( ByteArrayComparator.class );
039    
040        /** The serialVersionUID */
041        private static final long serialVersionUID = 1L;
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         * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
054         */
055        public int compare( byte[] b1, byte[] b2 )
056        {
057            LOG.debug( "comparing OctetString objects '{}' with '{}'", 
058                StringTools.dumpBytes( b1 ), StringTools.dumpBytes( b2 ) );
059    
060            // -------------------------------------------------------------------
061            // Handle some basis cases
062            // -------------------------------------------------------------------
063    
064            if ( b1 == null )
065            {
066                return ( b2 == null ) ? 0 : -1;
067            }
068            
069            if ( b2 == null )
070            {
071                return 1;
072            }
073            
074            if ( b1.length == b2.length )
075            {
076                for ( int i = 0; i < b1.length; i++ )
077                {
078                    if ( b1[i] > b2[i] )
079                    {
080                        return 1;
081                    }
082                    else if ( b1[i] < b2[i] )
083                    {
084                        return -1;
085                    }
086                }
087                
088                return 0;
089            }
090            
091            int minLength = Math.min( b1.length, b2.length );
092            
093            for ( int i = 0; i < minLength; i++ )
094            {
095                if ( b1[i] > b2[i] )
096                {
097                    return 1;
098                }
099                else if ( b1[i] < b2[i] )
100                {
101                    return -1;
102                }
103            }
104            
105            // b2 is longer w/ b1 as prefix 
106            if ( b1.length == minLength )
107            {
108                return -1;
109            }
110            
111            // b1 is longer w/ b2 as prefix
112            if ( b2.length == minLength )
113            {
114                return 1;
115            }
116            
117            return 0;
118        }
119    }