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