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 */
020 package org.apache.directory.shared.ldap.schema.comparators;
021
022
023 import java.util.Comparator;
024
025
026 /**
027 * A comparator for TelephoneNumber.
028 *
029 * The rules for matching are identical to those for caseIgnoreMatch, except that
030 * all space and "-" characters are skipped during the comparison.
031 *
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 * @version $Rev$
034 */
035 public class TelephoneNumberComparator implements Comparator<String>
036 {
037 /** A static instance of this comparator */
038 public static final Comparator<String> INSTANCE = new TelephoneNumberComparator();
039
040
041 /**
042 * Remove all spaces and '-' from the telephone number
043 */
044 private String strip( String telephoneNumber )
045 {
046 char[] telephoneNumberArray = telephoneNumber.toCharArray();
047 int pos = 0;
048
049 for ( char c:telephoneNumberArray )
050 {
051 if ( ( c == ' ' ) || ( c == '-' ) )
052 {
053 continue;
054 }
055
056 telephoneNumberArray[pos++] = c;
057 }
058
059 return new String( telephoneNumberArray, 0, pos );
060 }
061
062
063 /**
064 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
065 */
066 public int compare( String telephoneNumber1, String telephoneNumber2 )
067 {
068 // -------------------------------------------------------------------
069 // Handle some basis cases
070 // -------------------------------------------------------------------
071
072 if ( telephoneNumber1 == null )
073 {
074 return ( telephoneNumber2 == null ) ? 0 : -1;
075 }
076
077 if ( telephoneNumber2 == null )
078 {
079 return 1;
080 }
081
082 // -------------------------------------------------------------------
083 // Remove all spaces and '-'
084 // -------------------------------------------------------------------
085 telephoneNumber1 = strip( telephoneNumber1 );
086 telephoneNumber2 = strip( telephoneNumber2 );
087
088 return ( telephoneNumber1.compareToIgnoreCase( telephoneNumber2 ) );
089 }
090 }