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.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    /**
046     * The TelephoneNumberComparator constructor. Its OID is the TelephoneNumberMatch matching
047     * rule OID.
048     */
049    public TelephoneNumberComparator( String oid )
050    {
051        super( oid );
052    }
053
054
055    /**
056     * Remove all spaces and '-' from the telephone number
057     */
058    private String strip( String telephoneNumber )
059    {
060        char[] telephoneNumberArray = telephoneNumber.toCharArray();
061        int pos = 0;
062
063        for ( char c : telephoneNumberArray )
064        {
065            if ( ( c == ' ' ) || ( c == '-' ) )
066            {
067                continue;
068            }
069
070            telephoneNumberArray[pos++] = c;
071        }
072
073        return new String( telephoneNumberArray, 0, pos );
074    }
075
076
077    /**
078     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
079     */
080    public int compare( String telephoneNumber1, String telephoneNumber2 )
081    {
082        LOG.debug( "comparing TelephoneNumber objects '{}' with '{}'", telephoneNumber1, telephoneNumber2 );
083
084        // -------------------------------------------------------------------
085        // Handle some basis cases
086        // -------------------------------------------------------------------
087        if ( telephoneNumber1 == null )
088        {
089            return ( telephoneNumber2 == null ) ? 0 : -1;
090        }
091
092        if ( telephoneNumber2 == null )
093        {
094            return 1;
095        }
096
097        // -------------------------------------------------------------------
098        // Remove all spaces and '-'
099        // -------------------------------------------------------------------
100        String strippedTelephoneNumber1 = strip( telephoneNumber1 );
101        String strippedTelephoneNumber2 = strip( telephoneNumber2 );
102
103        return ( strippedTelephoneNumber1.compareToIgnoreCase( strippedTelephoneNumber2 ) );
104    }
105}