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.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027
028/**
029 * A class for the bitStringMatch matchingRule (RFC 4517, par. 4.2.1)
030 * 
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class BitStringComparator extends LdapComparator<String>
034{
035    /** The serial version UID */
036    private static final long serialVersionUID = 2L;
037
038    /** A logger for this class */
039    private static final Logger LOG = LoggerFactory.getLogger( BitStringComparator.class );
040
041
042    /**
043     * The BitStringComparator constructor. Its OID is the IntegerOrderingMatch matching
044     * rule OID.
045     * 
046     * @param oid The Comparator's OID
047     */
048    public BitStringComparator( String oid )
049    {
050        super( oid );
051    }
052
053
054    /**
055     * {@inheritDoc}
056     */
057    public int compare( String bs1, String bs2 )
058    {
059        LOG.debug( "comparing BitString objects '{}' with '{}'", bs1, bs2 );
060
061        // First, shortcut the process by comparing
062        // references. If they are equals, then bs1 and bs2
063        // reference the same object
064        if ( bs1 == bs2 )
065        {
066            return 0;
067        }
068
069        // Then, deal with one of bs1 or bs2 being null
070        // Both can't be null, because then they would
071        // have been caught by the previous test
072        if ( ( bs1 == null ) || ( bs2 == null ) )
073        {
074            return bs1 == null ? -1 : 1;
075        }
076
077        // We have to get rid of 0 from left of each BitString
078        char[] array1 = bs1.toCharArray();
079        char[] array2 = bs2.toCharArray();
080
081        int pos1 = bs1.indexOf( '1' );
082        int pos2 = bs2.indexOf( '1' );
083
084        if ( pos1 == -1 )
085        {
086            if ( pos2 == -1 )
087            {
088                return 0;
089            }
090            else
091            {
092                return -1;
093            }
094        }
095        else if ( pos2 == -1 )
096        {
097            return 1;
098        }
099
100        int length1 = array1.length - pos1;
101        int length2 = array2.length - pos2;
102
103        if ( length1 == length2 )
104        {
105            for ( int i = 0; i < length1; i++ )
106            {
107                int i1 = i + pos1;
108                int i2 = i + pos2;
109
110                if ( array1[i1] < array2[i2] )
111                {
112                    return -1;
113                }
114                else if ( array1[i1] > array2[i2] )
115                {
116                    return 1;
117                }
118            }
119
120            return 0;
121        }
122
123        if ( length1 < length2 )
124        {
125            return -1;
126        }
127        else
128        {
129            return 1;
130        }
131    }
132}