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  import java.io.Serializable;
25  import java.math.BigInteger;
26  
27  import org.apache.directory.api.i18n.I18n;
28  import org.apache.directory.api.ldap.model.schema.LdapComparator;
29  import org.apache.directory.api.ldap.model.schema.PrepareString;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * A class for the integerOrderingMatch matchingRule (RFC 4517, par. 4.2.20)
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class IntegerComparator extends LdapComparator<Object> implements Serializable
40  {
41      /** The serial version UID */
42      private static final long serialVersionUID = 2L;
43  
44      /** A logger for this class */
45      private static final Logger LOG = LoggerFactory.getLogger( IntegerComparator.class );
46  
47  
48      /**
49       * The IntegerComparator constructor. Its OID is the IntegerOrderingMatch matching
50       * rule OID.
51       */
52      public IntegerComparator( String oid )
53      {
54          super( oid );
55      }
56  
57  
58      /**
59       * Implementation of the Compare method
60       */
61      public int compare( Object v1, Object v2 )
62      {
63          if ( v1 instanceof String )
64          {
65              return compare( ( String ) v1, ( String ) v2 );
66          }
67          else
68          {
69              return compare( ( Long ) v1, ( Long ) v2 );
70          }
71      }
72  
73  
74      /**
75       * Implementation of the Compare method
76       */
77      @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RC_REF_COMPARISON",
78          justification = "false positive")
79      private int compare( Long backendValue, Long assertValue )
80      {
81          LOG.debug( "comparing Integer objects '{}' with '{}'", backendValue, assertValue );
82  
83          // First, shortcut the process by comparing
84          // references. If they are equals, then o1 and o2
85          // reference the same object
86          if ( backendValue == assertValue )
87          {
88              return 0;
89          }
90  
91          // Then, deal with one of o1 or o2 being null
92          // Both can't be null, because then they would
93          // have been caught by the previous test
94          if ( ( backendValue == null ) || ( assertValue == null ) )
95          {
96              return ( backendValue == null ? -1 : 1 );
97          }
98  
99          return backendValue.compareTo( assertValue );
100     }
101 
102 
103     /**
104      * Implementation of the Compare method
105      */
106     @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "ES_COMPARING_PARAMETER_STRING_WITH_EQ",
107         justification = "false positive")
108     private int compare( String backendValue, String assertValue )
109     {
110         LOG.debug( "comparing Integer objects '{}' with '{}'", backendValue, assertValue );
111 
112         // First, shortcut the process by comparing
113         // references. If they are equals, then o1 and o2
114         // reference the same object
115         if ( backendValue == assertValue )
116         {
117             return 0;
118         }
119 
120         // Then, deal with one of o1 or o2 being null
121         // Both can't be null, because then they would
122         // have been caught by the previous test
123         if ( ( backendValue == null ) || ( assertValue == null ) )
124         {
125             return ( backendValue == null ? -1 : 1 );
126         }
127 
128         // Both objects must be stored as String for numeric.
129         // But we need to normalize the values first.
130         try
131         {
132             backendValue = PrepareString.normalize( backendValue, PrepareString.StringType.NUMERIC_STRING );
133         }
134         catch ( IOException e )
135         {
136             throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, backendValue ) );
137         }
138         try
139         {
140             assertValue = PrepareString.normalize( assertValue, PrepareString.StringType.NUMERIC_STRING );
141         }
142         catch ( IOException e )
143         {
144             throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, assertValue ) );
145         }
146 
147         BigInteger b1 = new BigInteger( backendValue );
148         BigInteger b2 = new BigInteger( assertValue );
149 
150         return b1.compareTo( b2 );
151     }
152 }