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 java.util.Comparator;
024    
025    /**
026     * A class for the BooleanComparator matchingRule (RFC 4517, par. 4.2.2)
027     * 
028     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
029     * @version $Rev: 437007 $
030     */
031    public class BooleanComparator implements Comparator<Object>
032    {
033        /**
034         * Implementation of the Compare method
035         */
036        public int compare( Object backendValue, Object assertValue ) 
037        {
038            // First, shortcut the process by comparing
039            // references. If they are equals, then o1 and o2
040            // reference the same object
041            if ( backendValue == assertValue )
042            {
043                return 0;
044            }
045            
046            // Then, deal with one of o1 or o2 being null
047            // Both can't be null, because then they would 
048            // have been catched by the previous test
049            if ( ( backendValue == null ) || ( assertValue == null ) )
050            {
051                return ( backendValue == null ? -1 : 1 );
052            }
053    
054            // Both object must be stored as String for boolean
055            // values. If this is not the case, we have a pb...
056            // However, the method will then throw a ClassCastException
057            String b1 = (String)backendValue;
058    
059            // The boolean should have been stored as 'TRUE' or 'FALSE'
060            // into the server, and the compare method will be called
061            // with normalized booleans, so no need to uppercase them.
062            // We don't need to check the assertion value, because we
063            // are dealing with booleans.
064            return ( b1.equals( "TRUE" ) ? 1 : -1 );
065        }
066    }