001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.mavibot.btree.comparator; 021 022 023import java.util.Comparator; 024 025 026/** 027 * Compares boolean arrays. A boolean is considered as below the other one if the first boolean 028 * is false when the second one is true. 029 * 030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 031 */ 032public class BooleanArrayComparator implements Comparator<boolean[]> 033{ 034 /** A static instance of a BooleanArrayComparator */ 035 public static final BooleanArrayComparator INSTANCE = new BooleanArrayComparator(); 036 037 /** 038 * A private constructor of the BooleanArrayComparator class 039 */ 040 private BooleanArrayComparator() 041 { 042 } 043 044 045 /** 046 * Compare two boolean arrays. 047 * 048 * @param booleanArray1 First boolean array 049 * @param booleanArray2 Second boolean array 050 * @return 1 if booleanArray1 > booleanArray2, 0 if booleanArray1 == booleanArray2, -1 if booleanArray1 < booleanArray2 051 */ 052 public int compare( boolean[] booleanArray1, boolean[] booleanArray2 ) 053 { 054 if ( booleanArray1 == booleanArray2 ) 055 { 056 return 0; 057 } 058 059 if ( booleanArray1 == null ) 060 { 061 return -1; 062 } 063 064 if ( booleanArray2 == null ) 065 { 066 return 1; 067 } 068 069 if ( booleanArray1.length < booleanArray2.length ) 070 { 071 return -1; 072 } 073 074 if ( booleanArray1.length > booleanArray2.length ) 075 { 076 return 1; 077 } 078 079 for ( int pos = 0; pos < booleanArray1.length; pos++ ) 080 { 081 int comp = compare( booleanArray1[pos], booleanArray2[pos] ); 082 083 if ( comp != 0 ) 084 { 085 return comp; 086 } 087 } 088 089 return 0; 090 } 091 092 093 private int compare( boolean boolean1, boolean boolean2 ) 094 { 095 if ( boolean1 == boolean2 ) 096 { 097 return 0; 098 } 099 100 if ( boolean1 ) 101 { 102 return 1; 103 } 104 else 105 { 106 return -1; 107 } 108 } 109}