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:dev@directory.apache.org">Apache Directory Project</a> 31 */ 32 public class BooleanArrayComparator implements Comparator<boolean[]> 33 { 34 /** A static instance of a BooleanArrayComparator */ 35 public static final BooleanArrayComparator INSTANCE = new BooleanArrayComparator(); 36 37 /** 38 * A private constructor of the BooleanArrayComparator class 39 */ 40 private BooleanArrayComparator() 41 { 42 } 43 44 45 /** 46 * Compare two boolean arrays. 47 * 48 * @param booleanArray1 First boolean array 49 * @param booleanArray2 Second boolean array 50 * @return 1 if booleanArray1 > booleanArray2, 0 if booleanArray1 == booleanArray2, -1 if booleanArray1 < booleanArray2 51 */ 52 public int compare( boolean[] booleanArray1, boolean[] booleanArray2 ) 53 { 54 if ( booleanArray1 == booleanArray2 ) 55 { 56 return 0; 57 } 58 59 if ( booleanArray1 == null ) 60 { 61 return -1; 62 } 63 64 if ( booleanArray2 == null ) 65 { 66 return 1; 67 } 68 69 if ( booleanArray1.length < booleanArray2.length ) 70 { 71 return -1; 72 } 73 74 if ( booleanArray1.length > booleanArray2.length ) 75 { 76 return 1; 77 } 78 79 for ( int pos = 0; pos < booleanArray1.length; pos++ ) 80 { 81 int comp = compare( booleanArray1[pos], booleanArray2[pos] ); 82 83 if ( comp != 0 ) 84 { 85 return comp; 86 } 87 } 88 89 return 0; 90 } 91 92 93 private int compare( boolean boolean1, boolean boolean2 ) 94 { 95 if ( boolean1 == boolean2 ) 96 { 97 return 0; 98 } 99 100 if ( boolean1 ) 101 { 102 return 1; 103 } 104 else 105 { 106 return -1; 107 } 108 } 109 }