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   *    http://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.ldap.model.schema.LdapComparator;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  
28  /**
29   * A comparator for TelephoneNumber.
30   * 
31   * The rules for matching are identical to those for caseIgnoreMatch, except that 
32   * all space and "-" characters are skipped during the comparison. 
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public class TelephoneNumberComparator extends LdapComparator<String>
37  {
38      /** The serial version UID */
39      private static final long serialVersionUID = 2L;
40  
41      /** A logger for this class */
42      private static final Logger LOG = LoggerFactory.getLogger( TelephoneNumberComparator.class );
43  
44  
45      /**
46       * The TelephoneNumberComparator constructor. Its OID is the TelephoneNumberMatch matching
47       * rule OID.
48       * 
49       * @param oid The Comparator's OID
50       */
51      public TelephoneNumberComparator( String oid )
52      {
53          super( oid );
54      }
55  
56  
57      /**
58       * Remove all spaces and '-' from the telephone number
59       */
60      private String strip( String telephoneNumber )
61      {
62          char[] telephoneNumberArray = telephoneNumber.toCharArray();
63          int pos = 0;
64  
65          for ( char c : telephoneNumberArray )
66          {
67              if ( ( c == ' ' ) || ( c == '-' ) )
68              {
69                  continue;
70              }
71  
72              telephoneNumberArray[pos++] = c;
73          }
74  
75          return new String( telephoneNumberArray, 0, pos );
76      }
77  
78  
79      /**
80       * {@inheritDoc}
81       */
82      public int compare( String telephoneNumber1, String telephoneNumber2 )
83      {
84          LOG.debug( "comparing TelephoneNumber objects '{}' with '{}'", telephoneNumber1, telephoneNumber2 );
85  
86          // -------------------------------------------------------------------
87          // Handle some basis cases
88          // -------------------------------------------------------------------
89          if ( telephoneNumber1 == null )
90          {
91              return ( telephoneNumber2 == null ) ? 0 : -1;
92          }
93  
94          if ( telephoneNumber2 == null )
95          {
96              return 1;
97          }
98  
99          // -------------------------------------------------------------------
100         // Remove all spaces and '-'
101         // -------------------------------------------------------------------
102         String strippedTelephoneNumber1 = strip( telephoneNumber1 );
103         String strippedTelephoneNumber2 = strip( telephoneNumber2 );
104 
105         return strippedTelephoneNumber1.compareToIgnoreCase( strippedTelephoneNumber2 );
106     }
107 }