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      public TelephoneNumberComparator( String oid )
50      {
51          super( oid );
52      }
53  
54  
55      /**
56       * Remove all spaces and '-' from the telephone number
57       */
58      private String strip( String telephoneNumber )
59      {
60          char[] telephoneNumberArray = telephoneNumber.toCharArray();
61          int pos = 0;
62  
63          for ( char c : telephoneNumberArray )
64          {
65              if ( ( c == ' ' ) || ( c == '-' ) )
66              {
67                  continue;
68              }
69  
70              telephoneNumberArray[pos++] = c;
71          }
72  
73          return new String( telephoneNumberArray, 0, pos );
74      }
75  
76  
77      /**
78       * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
79       */
80      public int compare( String telephoneNumber1, String telephoneNumber2 )
81      {
82          LOG.debug( "comparing TelephoneNumber objects '{}' with '{}'", telephoneNumber1, telephoneNumber2 );
83  
84          // -------------------------------------------------------------------
85          // Handle some basis cases
86          // -------------------------------------------------------------------
87          if ( telephoneNumber1 == null )
88          {
89              return ( telephoneNumber2 == null ) ? 0 : -1;
90          }
91  
92          if ( telephoneNumber2 == null )
93          {
94              return 1;
95          }
96  
97          // -------------------------------------------------------------------
98          // Remove all spaces and '-'
99          // -------------------------------------------------------------------
100         String strippedTelephoneNumber1 = strip( telephoneNumber1 );
101         String strippedTelephoneNumber2 = strip( telephoneNumber2 );
102 
103         return ( strippedTelephoneNumber1.compareToIgnoreCase( strippedTelephoneNumber2 ) );
104     }
105 }