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  
24  import org.apache.directory.api.i18n.I18n;
25  import org.apache.directory.api.ldap.model.exception.LdapException;
26  import org.apache.directory.api.ldap.model.schema.LdapComparator;
27  import org.apache.directory.api.ldap.model.schema.normalizers.NumericNormalizer;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  
32  /**
33   * A class for the numericStringOrderingMatch matchingRule (RFC 4517, par. 4.2.23)
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public class NumericStringComparator 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( NumericStringComparator.class );
44  
45  
46      /**
47       * The IntegerComparator constructor. Its OID is the numericStringOrderingMatch matching
48       * rule OID.
49       * 
50       * @param oid The Comparator's OID
51       */
52      public NumericStringComparator( String oid )
53      {
54          super( oid );
55      }
56  
57  
58      /**
59       * {@inheritDoc}
60       */
61      public int compare( String backendValue, String assertValue )
62      {
63          if ( LOG.isDebugEnabled() )
64          {
65              LOG.debug( I18n.msg( I18n.MSG_13754_COMPARING_NUMERIC_STRING_ORDERING, backendValue, assertValue ) );
66          }
67  
68          // First, shortcut the process by comparing
69          // references. If they are equals, then o1 and o2
70          // reference the same object
71          if ( backendValue == assertValue )
72          {
73              return 0;
74          }
75  
76          // Then, deal with one of o1 or o2 being null
77          // Both can't be null, because then they would
78          // have been caught by the previous test
79          if ( ( backendValue == null ) || ( assertValue == null ) )
80          {
81              return backendValue == null ? -1 : 1;
82          }
83  
84          // Both objects must be stored as String for numeric.
85          // But we need to normalize the values first.
86          NumericNormalizer normalizer = new NumericNormalizer();
87  
88          try
89          {
90              backendValue = normalizer.normalize( backendValue );
91          }
92          catch ( LdapException le )
93          {
94              throw new IllegalArgumentException( I18n.err( I18n.ERR_13724_INVALID_VALUE, backendValue ), le );
95          }
96          try
97          {
98              assertValue = normalizer.normalize( assertValue );
99          }
100         catch ( LdapException le )
101         {
102             throw new IllegalArgumentException( I18n.err( I18n.ERR_13724_INVALID_VALUE, assertValue ), le );
103         }
104 
105         return backendValue.compareTo( assertValue );
106     }
107 }