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