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       * @param oid The Comparator's OID
53       */
54      public IntegerComparator( String oid )
55      {
56          super( oid );
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public int compare( Object v1, Object v2 )
65      {
66          if ( v1 instanceof String )
67          {
68              return compare( ( String ) v1, ( String ) v2 );
69          }
70          else
71          {
72              return compare( ( Long ) v1, ( Long ) v2 );
73          }
74      }
75  
76  
77      /**
78       * Implementation of the Compare method
79       */
80      private int compare( Long backendValue, Long assertValue )
81      {
82          LOG.debug( "comparing Integer objects '{}' with '{}'", backendValue, assertValue );
83  
84          // First, shortcut the process by comparing
85          // references. If they are equals, then o1 and o2
86          // reference the same object
87          if ( backendValue == assertValue )
88          {
89              return 0;
90          }
91  
92          // Then, deal with one of o1 or o2 being null
93          // Both can't be null, because then they would
94          // have been caught by the previous test
95          if ( ( backendValue == null ) || ( assertValue == null ) )
96          {
97              return backendValue == null ? -1 : 1;
98          }
99  
100         return backendValue.compareTo( assertValue );
101     }
102 
103 
104     /**
105      * Implementation of the Compare method
106      */
107     private int compare( String backendValue, String assertValue )
108     {
109         LOG.debug( "comparing Integer objects '{}' with '{}'", backendValue, assertValue );
110 
111         // First, shortcut the process by comparing
112         // references. If they are equals, then o1 and o2
113         // reference the same object
114         if ( backendValue == assertValue )
115         {
116             return 0;
117         }
118 
119         // Then, deal with one of o1 or o2 being null
120         // Both can't be null, because then they would
121         // have been caught by the previous test
122         if ( ( backendValue == null ) || ( assertValue == null ) )
123         {
124             return backendValue == null ? -1 : 1;
125         }
126 
127         // Both objects must be stored as String for numeric.
128         // But we need to normalize the values first.
129         try
130         {
131             backendValue = PrepareString.normalize( backendValue, PrepareString.StringType.NUMERIC_STRING );
132         }
133         catch ( IOException ioe )
134         {
135             throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, backendValue ), ioe );
136         }
137         try
138         {
139             assertValue = PrepareString.normalize( assertValue, PrepareString.StringType.NUMERIC_STRING );
140         }
141         catch ( IOException ioe )
142         {
143             throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, assertValue ), ioe );
144         }
145 
146         BigInteger b1 = new BigInteger( backendValue );
147         BigInteger b2 = new BigInteger( assertValue );
148 
149         return b1.compareTo( b2 );
150     }
151 }