View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.comparators;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.schema.LdapComparator;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  
29  /**
30   * A comparator for TelephoneNumber.
31   * 
32   * The rules for matching are identical to those for caseIgnoreMatch, except that 
33   * all space and "-" characters are skipped during the comparison. 
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public class TelephoneNumberComparator extends LdapComparator<String>
38  {
39      /** The serial version UID */
40      private static final long serialVersionUID = 2L;
41  
42      /** A logger for this class */
43      private static final Logger LOG = LoggerFactory.getLogger( TelephoneNumberComparator.class );
44  
45  
46      /**
47       * The TelephoneNumberComparator constructor. Its OID is the TelephoneNumberMatch matching
48       * rule OID.
49       * 
50       * @param oid The Comparator's OID
51       */
52      public TelephoneNumberComparator( String oid )
53      {
54          super( oid );
55      }
56  
57  
58      /**
59       * Remove all spaces and '-' from the telephone number
60       * 
61       * @param telephoneNumber The telephoneNumber to strip
62       * @return teh stripped telephoneNumber
63       */
64      private String strip( String telephoneNumber )
65      {
66          char[] telephoneNumberArray = telephoneNumber.toCharArray();
67          int pos = 0;
68  
69          for ( char c : telephoneNumberArray )
70          {
71              if ( ( c == ' ' ) || ( c == '-' ) )
72              {
73                  continue;
74              }
75  
76              telephoneNumberArray[pos++] = c;
77          }
78  
79          return new String( telephoneNumberArray, 0, pos );
80      }
81  
82  
83      /**
84       * {@inheritDoc}
85       */
86      public int compare( String telephoneNumber1, String telephoneNumber2 )
87      {
88          if ( LOG.isDebugEnabled() )
89          {
90              LOG.debug( I18n.msg( I18n.MSG_13750_COMPARING_TELEPHONE_NUMBER, telephoneNumber1, telephoneNumber2 ) );
91          }
92  
93          // -------------------------------------------------------------------
94          // Handle some basis cases
95          // -------------------------------------------------------------------
96          if ( telephoneNumber1 == null )
97          {
98              return ( telephoneNumber2 == null ) ? 0 : -1;
99          }
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 }