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.mavibot.btree.comparator;
21  
22  
23  import java.util.Comparator;
24  
25  
26  /**
27   * Compares boolean arrays. A boolean is considered as below the other one if the first boolean
28   * is false when the second one is true.
29   * 
30   * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
31   */
32  public class BooleanArrayComparator implements Comparator<boolean[]>
33  {
34      /**
35       * Compare two boolean arrays.
36       * 
37       * @param booleanArray1 First boolean array
38       * @param booleanArray2 Second boolean array
39       * @return 1 if booleanArray1 > booleanArray2, 0 if booleanArray1 == booleanArray2, -1 if booleanArray1 < booleanArray2
40       */
41      public int compare( boolean[] booleanArray1, boolean[] booleanArray2 )
42      {
43          if ( booleanArray1 == booleanArray2 )
44          {
45              return 0;
46          }
47  
48          if ( booleanArray1 == null )
49          {
50              return -1;
51          }
52  
53          if ( booleanArray2 == null )
54          {
55              return 1;
56          }
57  
58          if ( booleanArray1.length < booleanArray2.length )
59          {
60              return -1;
61          }
62  
63          if ( booleanArray1.length > booleanArray2.length )
64          {
65              return 1;
66          }
67  
68          for ( int pos = 0; pos < booleanArray1.length; pos++ )
69          {
70              int comp = compare( booleanArray1[pos], booleanArray2[pos] );
71  
72              if ( comp != 0 )
73              {
74                  return comp;
75              }
76          }
77  
78          return 0;
79      }
80  
81  
82      private int compare( boolean boolean1, boolean boolean2 )
83      {
84          if ( boolean1 == boolean2 )
85          {
86              return 0;
87          }
88  
89          if ( boolean1 )
90          {
91              return 1;
92          }
93          else
94          {
95              return -1;
96          }
97      }
98  }