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    import org.apache.directory.shared.ldap.schema.LdapComparator;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    
026    
027    /**
028     * A class for the BooleanComparator matchingRule (RFC 4517, par. 4.2.2)
029     * 
030     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031     * @version $Rev: 437007 $
032     */
033    public class BooleanComparator extends LdapComparator<String>
034    {
035        /** A logger for this class */
036        private static final Logger LOG = LoggerFactory.getLogger( BooleanComparator.class );
037    
038        /** The serialVersionUID */
039        private static final long serialVersionUID = 1L;
040    
041        /**
042         * The BooleanComparator constructor. Its OID is the BooleanMatch matching
043         * rule OID.
044         */
045        public BooleanComparator( String oid )
046        {
047            super( oid );
048        }
049    
050        /**
051         * Implementation of the Compare method
052         */
053        public int compare( String b1, String b2 ) 
054        {
055            LOG.debug( "comparing boolean objects '{}' with '{}'", b1, b2 );
056            
057            // First, shortcut the process by comparing
058            // references. If they are equals, then o1 and o2
059            // reference the same object
060            if ( b1 == b2 )
061            {
062                return 0;
063            }
064            
065            // Then, deal with one of o1 or o2 being null
066            // Both can't be null, because then they would 
067            // have been catched by the previous test
068            if ( ( b1 == null ) || ( b2 == null ) )
069            {
070                return ( b1 == null ? -1 : 1 );
071            }
072    
073            // The boolean should have been stored as 'TRUE' or 'FALSE'
074            // into the server, and the compare method will be called
075            // with normalized booleans, so no need to uppercase them.
076            // We don't need to check the assertion value, because we
077            // are dealing with booleans.
078            return ( b1.equals( "TRUE" ) ? 1 : -1 );
079        }
080    }