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  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.schema.LdapComparator;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  
29  /**
30   * A class for the BooleanComparator matchingRule (RFC 4517, par. 4.2.2)
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public class BooleanComparator extends LdapComparator<String>
35  {
36      /** The serial version UID */
37      private static final long serialVersionUID = 2L;
38  
39      /** A logger for this class */
40      private static final Logger LOG = LoggerFactory.getLogger( BooleanComparator.class );
41  
42  
43      /**
44       * The BooleanComparator constructor. Its OID is the BooleanMatch matching
45       * rule OID.
46       * 
47       * @param oid The Comparator's OID
48       */
49      public BooleanComparator( String oid )
50      {
51          super( oid );
52      }
53  
54  
55      /**
56       * {@inheritDoc}
57       */
58      public int compare( String b1, String b2 )
59      {
60          if ( LOG.isDebugEnabled() )
61          {
62              LOG.debug( I18n.msg( I18n.MSG_13752_COMPARING_BOOLEAN, b1, b2 ) );
63          }
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 ( b1 == b2 )
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 catched by the previous test
76          if ( ( b1 == null ) || ( b2 == null ) )
77          {
78              return b1 == null ? -1 : 1;
79          }
80  
81          // The boolean should have been stored as 'TRUE' or 'FALSE'
82          // into the server, and the compare method will be called
83          // with normalized booleans, so no need to upper case them.
84          // We don't need to check the assertion value, because we
85          // are dealing with booleans.
86          boolean boolean1 = Boolean.parseBoolean( b1 );
87          boolean boolean2 = Boolean.parseBoolean( b2 );
88  
89          if ( boolean1 == boolean2 )
90          {
91              return 0;
92          }
93  
94          return boolean1 ? 1 : -1;
95      }
96  }