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