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 java.io.IOException;
24  
25  import org.apache.directory.api.i18n.I18n;
26  import org.apache.directory.api.ldap.model.schema.LdapComparator;
27  import org.apache.directory.api.ldap.model.schema.PrepareString;
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          LOG.debug( "comparing numericStringOrdering objects '{}' with '{}'", backendValue, assertValue );
64  
65          // First, shortcut the process by comparing
66          // references. If they are equals, then o1 and o2
67          // reference the same object
68          if ( backendValue == assertValue )
69          {
70              return 0;
71          }
72  
73          // Then, deal with one of o1 or o2 being null
74          // Both can't be null, because then they would
75          // have been caught by the previous test
76          if ( ( backendValue == null ) || ( assertValue == null ) )
77          {
78              return backendValue == null ? -1 : 1;
79          }
80  
81          // Both objects must be stored as String for numeric.
82          // But we need to normalize the values first.
83          try
84          {
85              backendValue = PrepareString.normalize( backendValue, PrepareString.StringType.NUMERIC_STRING );
86          }
87          catch ( IOException ioe )
88          {
89              throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, backendValue ), ioe );
90          }
91          try
92          {
93              assertValue = PrepareString.normalize( assertValue, PrepareString.StringType.NUMERIC_STRING );
94          }
95          catch ( IOException ioe )
96          {
97              throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, assertValue ), ioe );
98          }
99  
100         return backendValue.compareTo( assertValue );
101     }
102 }