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 comparator for TelephoneNumber.
030 * 
031 * The rules for matching are identical to those for caseIgnoreMatch, except that 
032 * all space and "-" characters are skipped during the comparison. 
033 * 
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public class TelephoneNumberComparator extends LdapComparator<String>
037{
038    /** The serial version UID */
039    private static final long serialVersionUID = 2L;
040
041    /** A logger for this class */
042    private static final Logger LOG = LoggerFactory.getLogger( TelephoneNumberComparator.class );
043
044    /**
045     * The TelephoneNumberComparator constructor. Its OID is the TelephoneNumberMatch matching
046     * rule OID.
047     */
048    public TelephoneNumberComparator( String oid )
049    {
050        super( oid );
051    }
052
053    
054    /**
055     * Remove all spaces and '-' from the telephone number
056     */
057    private String strip( String telephoneNumber )
058    {
059        char[] telephoneNumberArray = telephoneNumber.toCharArray();
060        int pos = 0;
061        
062        for ( char c:telephoneNumberArray )
063        {
064            if ( ( c == ' ' ) || ( c == '-' ) )
065            { 
066                continue;
067            }
068            
069            telephoneNumberArray[pos++] = c;
070        }
071        
072        return new String( telephoneNumberArray, 0, pos );
073    }
074
075    
076    /**
077     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
078     */
079    public int compare( String telephoneNumber1, String telephoneNumber2 )
080    {
081        LOG.debug( "comparing TelephoneNumber objects '{}' with '{}'", telephoneNumber1, telephoneNumber2 );
082
083        // -------------------------------------------------------------------
084        // Handle some basis cases
085        // -------------------------------------------------------------------
086        if ( telephoneNumber1 == null )
087        {
088            return ( telephoneNumber2 == null ) ? 0 : -1;
089        }
090        
091        if ( telephoneNumber2 == null )
092        {
093            return 1;
094        }
095        
096        // -------------------------------------------------------------------
097        // Remove all spaces and '-'
098        // -------------------------------------------------------------------
099        String strippedTelephoneNumber1 = strip( telephoneNumber1 );
100        String strippedTelephoneNumber2 = strip( telephoneNumber2 );
101        
102        return ( strippedTelephoneNumber1.compareToIgnoreCase( strippedTelephoneNumber2 ) );
103    }
104}