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.shared.ldap.model.schema.comparators;
021
022
023import org.apache.directory.shared.ldap.model.schema.LdapComparator;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027
028/**
029 * A class for the BooleanComparator matchingRule (RFC 4517, par. 4.2.2)
030 * 
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class BooleanComparator 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( BooleanComparator.class );
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    /**
052     * Implementation of the Compare method
053     */
054    public int compare( String b1, String b2 )
055    {
056        LOG.debug( "comparing boolean objects '{}' with '{}'", b1, b2 );
057
058        // First, shortcut the process by comparing
059        // references. If they are equals, then o1 and o2
060        // reference the same object
061        if ( b1 == b2 )
062        {
063            return 0;
064        }
065
066        // Then, deal with one of o1 or o2 being null
067        // Both can't be null, because then they would 
068        // have been catched by the previous test
069        if ( ( b1 == null ) || ( b2 == null ) )
070        {
071            return ( b1 == null ? -1 : 1 );
072        }
073
074        // The boolean should have been stored as 'TRUE' or 'FALSE'
075        // into the server, and the compare method will be called
076        // with normalized booleans, so no need to upper case them.
077        // We don't need to check the assertion value, because we
078        // are dealing with booleans.
079        return ( b1.equals( "TRUE" ) ? 1 : -1 );
080    }
081}